diff --git a/.gitignore b/.gitignore index bd6608341b..b4751b43b4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ test/tmp *.cache /templates.json coverage +dist diff --git a/.npmignore b/.npmignore index 7031ae4f97..678b0b1d2d 100644 --- a/.npmignore +++ b/.npmignore @@ -6,3 +6,4 @@ test benchmark Makefile .* +dist diff --git a/Makefile b/Makefile index b17df0a5e8..ad9418da50 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ export NODE_ENV = test .PHONY: clean test test-cov test-travis publish bench build clean: - rm -rf coverage templates.json test/tmp build + rm -rf coverage templates.json test/tmp dist test: $(MOCHA_CMD) @@ -16,7 +16,7 @@ bench: node node_modules/matcha/bin/_matcha test-cov: - make clean + rm -rf coverage node $(ISTANBUL_CMD) $(MOCHA_CMD) -- rm -rf test/tmp @@ -25,22 +25,26 @@ test-travis: if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi build: - mkdir build - cd build - browserify lib/6to5/transform.js >6to5.js - uglifyjs 6to5.js >6to5.min.js + rm -rf dist + mkdir dist + + node bin/cache-templates + + browserify lib/6to5/transform.js -s to5 >dist/6to5.js + uglifyjs dist/6to5.js >dist/6to5.min.js + + rm -rf templates.json publish: - make clean - rm -rf node_modules npm install node bin/cache-templates make test + test -f templates.json npm publish # todo - auto-create tag - make clean + rm -rf templates.json diff --git a/README.md b/README.md index 2fb2812cfc..6e31b82e11 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,22 @@ polyfill is also required negating the [polyfill caveat](#polyfill). require("6to5/register"); ``` +## Browser + +You can build a browser version of the compiler by running the following in the +6to5 directory: + + $ make build + +This will output the files `dist/6to5.js` and `dist/6to5.min.js`. + +Just include one of those in the browser and access the transform method via the +global `to5`. + +```javascript +to5("class Test {}").code; +``` + ## Modules 6to5 modules compile straight to CommonJS, because of this various liberties are diff --git a/lib/6to5/util.js b/lib/6to5/util.js index af79c8695c..74225cad0e 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -201,11 +201,11 @@ exports.repeat = function (width, cha) { return new Array(width + 1).join(cha); }; -var templatesCacheLoc = __dirname + "/../../templates.json"; +try { + exports.templates = require("../../templates.json"); +} catch (err) { + if (err.code !== "MODULE_NOT_FOUND") throw err; -if (fs.existsSync(templatesCacheLoc)) { - exports.templates = require(templatesCacheLoc); -} else { exports.templates = {}; var templatesLoc = __dirname + "/templates";