-
-
Notifications
You must be signed in to change notification settings - Fork 219
Add babel-minify-standalone #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| const registerBabelStandaloneTask = require("babel-standalone/src/gulpTasks") | ||
| .registerBabelStandaloneTask; | ||
| const gulp = require("gulp"); | ||
|
|
||
| const version = require("./package.json").version; | ||
| registerBabelStandaloneTask(gulp, "babel-minify", "BabelMinify", __dirname, version); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| babel-minify-standalone | ||
| ======================= | ||
|
|
||
| babel-minify-standalone is a standalone build of [babel-minify](https://github.com/babel/minify) for use in non-Node.js environments, including browsers. | ||
|
|
||
| But why?! | ||
| ========= | ||
|
|
||
| It's true that using Babel (and babel-minify) through Webpack, Browserify or Gulp should be sufficient for most use cases. However, there are some valid use cases for babel-standalone: | ||
|
|
||
| - Sites like [JSFiddle](https://jsfiddle.net/), [JS Bin](https://jsbin.com/), the [REPL on the Babel site](http://babeljs.io/repl/), etc. These sites compile user-provided JavaScript in real-time, and babel-minify could be used to provide on-the-fly minification and related statistics, | ||
| - Apps that embed a JavaScript engine such as V8 directly, and want to use Babel for compilation and minification | ||
| - Apps that want to use JavaScript as a scripting language for extending the app itself, including all the goodies that ES2015 provides. | ||
| - Integration of Babel into a non-Node.js environment ([ReactJS.NET](http://reactjs.net/), [ruby-babel-transpiler](https://github.com/babel/ruby-babel-transpiler), [php-babel-transpiler](https://github.com/talyssonoc/php-babel-transpiler), etc). | ||
|
|
||
| Installation | ||
| ============ | ||
|
|
||
| There are several ways to get a copy of babel-minify. Pick whichever one you like: | ||
|
|
||
| - Use it via Unpkg: https://unpkg.com/babel-minify-standalone@0.x/babel-minify.min.js. This is a simple way to embed it on a webpage without having to do any other setup. | ||
| - Install via Bower: `bower install babili-standalone` | ||
| - Install via NPM: `npm install --save babel-minify` | ||
| - Manually grab `babel-minify.js` and/or `babel-minify.min.js` from the [GitHub releases page](https://github.com/Daniel15/babel-standalone/releases). Every release includes these files. | ||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| Load `babel-minify.js` or `babel-minify.min.js` in your environment, **along with Babel-standalone**. This is important: You need to load Babel too! | ||
|
|
||
| This will load babel-minify's Babel plugins and preset, and expose a simple API in a `BabelMinify` object: | ||
|
|
||
| ```js | ||
| var input = 'class Foo { constructor(bar) { this.bar = bar } }; new Foo()'; | ||
| var output = BabelMinify.transform(input).code; | ||
| // class a{constructor(b){this.bar=b}};new a; | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('babel-standalone'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const BabelMinify = require('../babel-minify'); | ||
|
|
||
| // Basic smoke tests for babel-minify-standalone | ||
| describe('babel-minify-standalone', () => { | ||
| it('works', () => { | ||
| const output = BabelMinify.transform( | ||
| ` | ||
| class Mangler { | ||
| constructor(program) { | ||
| this.program = program; | ||
| } | ||
| } | ||
| new Mangler();`).code; | ||
| expect(output).toBe('class Mangler{constructor(a){this.program=a}}new Mangler;'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <!DOCTYPE html> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the file extension is missing
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's consistent with babel-standalone: https://github.com/babel/babel/blob/7.0/packages/babel-standalone/examples/example.htm Should I change both?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, I thought this extension was dead long before. Its totally up to you :) |
||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>babel-minify-standalone example</title> | ||
| </head> | ||
| <body> | ||
| Input: | ||
| <textarea id="input" style="width: 100%" rows="10"> | ||
| // Example ES2015 Code | ||
| class Mangler { | ||
| constructor(program) { | ||
| this.program = program; | ||
| } | ||
| } | ||
| new Mangler(); | ||
| </textarea> | ||
|
|
||
| Transformed code using babel-minify <strong id="version"></strong>: | ||
| <pre id="output">Loading...</pre> | ||
|
|
||
| <script src="https://unpkg.com/babel-standalone@7.0.0-alpha.19/babel.min.js"></script> | ||
| <script src="../babel-minify.js"></script> | ||
| <script> | ||
| console.log('Babel =', Babel); | ||
| console.log('Babel-Minify =', BabelMinify); | ||
| var inputEl = document.getElementById('input'); | ||
| var outputEl = document.getElementById('output'); | ||
|
|
||
| function transform() { | ||
| try { | ||
| outputEl.innerHTML = BabelMinify.transform(inputEl.value).code; | ||
| } catch (ex) { | ||
| outputEl.innerHTML = 'ERROR: ' + ex.message; | ||
| } | ||
| } | ||
|
|
||
| inputEl.addEventListener('keyup', transform, false); | ||
| transform(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| { | ||
| "name": "babel-minify-standalone", | ||
| "version": "0.2.0", | ||
| "description": "Standalone build of Babili (babel-minify) for use in non-Node.js environments", | ||
| "main": "babili.js", | ||
| "files": [ | ||
| "babili.js", | ||
| "babili.min.js", | ||
| "src" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Daniel15/babel-standalone.git" | ||
| }, | ||
| "keywords": [ | ||
| "babel-preset", | ||
| "babel-standalone", | ||
| "babel", | ||
| "6to5", | ||
| "minify", | ||
| "uglify" | ||
| ], | ||
| "author": "Daniel Lo Nigro <daniel@dan.cx> (http://dan.cx/)", | ||
| "license": "MIT", | ||
| "bugs": { | ||
| "url": "https://github.com/Daniel15/babel-standalone/issues" | ||
| }, | ||
| "homepage": "https://dl.vc/babili-standalone", | ||
| "devDependencies": { | ||
| "babel-core": "^7.0.0-alpha.19", | ||
| "babel-plugin-minify-builtins": "0.2.0", | ||
| "babel-plugin-minify-constant-folding": "0.2.0", | ||
| "babel-plugin-minify-dead-code-elimination": "0.2.0", | ||
| "babel-plugin-minify-flip-comparisons": "0.2.0", | ||
| "babel-plugin-minify-guarded-expressions": "0.2.0", | ||
| "babel-plugin-minify-infinity": "0.2.0", | ||
| "babel-plugin-minify-mangle-names": "0.2.0", | ||
| "babel-plugin-minify-numeric-literals": "0.2.0", | ||
| "babel-plugin-minify-replace": "0.2.0", | ||
| "babel-plugin-minify-simplify": "0.2.0", | ||
| "babel-plugin-minify-type-constructors": "0.2.0", | ||
| "babel-plugin-transform-inline-consecutive-adds": "0.2.0", | ||
| "babel-plugin-transform-inline-environment-variables": "0.2.0", | ||
| "babel-plugin-transform-member-expression-literals": "6.8.5", | ||
| "babel-plugin-transform-merge-sibling-variables": "6.8.6", | ||
| "babel-plugin-transform-minify-booleans": "6.8.3", | ||
| "babel-plugin-transform-node-env-inline": "0.2.0", | ||
| "babel-plugin-transform-property-literals": "6.8.5", | ||
| "babel-plugin-transform-regexp-constructors": "0.2.0", | ||
| "babel-plugin-transform-remove-console": "6.8.5", | ||
| "babel-plugin-transform-remove-debugger": "6.8.5", | ||
| "babel-plugin-transform-remove-undefined": "0.2.0", | ||
| "babel-plugin-transform-simplify-comparison-operators": "6.8.5", | ||
| "babel-plugin-transform-undefined-to-void": "6.8.3", | ||
| "babel-preset-minify": "0.2.0", | ||
| "babel-standalone": "7.0.0-alpha.19" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { | ||
| registerPlugins, | ||
| registerPreset, | ||
| transform as babelTransform, | ||
| } from 'babel-standalone'; | ||
|
|
||
| registerPlugins({ | ||
| 'minify-constant-folding': require('babel-plugin-minify-constant-folding'), | ||
| 'minify-dead-code-elimination': require('babel-plugin-minify-dead-code-elimination'), | ||
| 'minify-flip-comparisons': require('babel-plugin-minify-flip-comparisons'), | ||
| 'minify-guarded-expressions': require('babel-plugin-minify-guarded-expressions'), | ||
| 'minify-infinity': require('babel-plugin-minify-infinity'), | ||
| 'minify-mangle-names': require('babel-plugin-minify-mangle-names'), | ||
| 'minify-replace': require('babel-plugin-minify-replace'), | ||
| 'minify-simplify': require('babel-plugin-minify-simplify'), | ||
| 'minify-type-constructors': require('babel-plugin-minify-type-constructors'), | ||
| 'transform-inline-environment-variables': require('babel-plugin-transform-inline-environment-variables'), | ||
| 'transform-member-expression-literals': require('babel-plugin-transform-member-expression-literals'), | ||
| 'transform-merge-sibling-variables': require('babel-plugin-transform-merge-sibling-variables'), | ||
| 'transform-minify-booleans': require('babel-plugin-transform-minify-booleans'), | ||
| 'transform-node-env-inline': require('babel-plugin-transform-node-env-inline'), | ||
| 'transform-property-literals': require('babel-plugin-transform-property-literals'), | ||
| 'transform-remove-console': require('babel-plugin-transform-remove-console'), | ||
| 'transform-remove-debugger': require('babel-plugin-transform-remove-debugger'), | ||
| 'transform-simplify-comparison-operators': require('babel-plugin-transform-simplify-comparison-operators'), | ||
| 'transform-undefined-to-void': require('babel-plugin-transform-undefined-to-void'), | ||
| }); | ||
| registerPreset('minify', require('babel-preset-minify')); | ||
|
|
||
| export function transform(code, options = {}) { | ||
| return babelTransform(code, Object.assign( | ||
| {}, | ||
| options, | ||
| { | ||
| presets: [ | ||
| ...(options.presets || []), | ||
| 'minify', | ||
| ] | ||
| })); | ||
| } | ||
|
|
||
| export const version = VERSION; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an important/required change for including babel-minify-standalone ?
If so, then all the plugin and preset tests will run on babel7 instead of babel6, and almost everyone using this minifier would be using it with babel6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some build errors with 6.x. I can try again and see if I can get it working. I think it was because the latest
babel-standaloneversions reference babel-core 7.x.Is there a branch of babel-minify for Babel 7.x builds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a open PR for babel 7 upgrade here #487