-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
babili #898
babili #898
Changes from 1 commit
8449de7
2aa9315
3264489
f948dd3
7d4a123
ea8f816
bce0a14
b196d54
14b7ca7
5b1bf68
f94cb99
12d6732
618e895
54a8c21
f66e530
80ba131
ffc0052
1d8e255
f74a818
20c119a
3515f72
6a4e355
2a59a47
605e82d
1891d3f
db9ed7a
45a0dc5
3251f9d
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,100 @@ | ||
--- | ||
layout: post | ||
title: "Releasing Babili (beta)" | ||
author: Henry Zhu | ||
date: 2016-08-26 09:30:00 | ||
categories: announcements | ||
share_text: "Releasing Babili (beta)" | ||
--- | ||
|
||
Babili (babel-minify) | ||
|
||
We released [babili](https://github.com/babel/babili) yesterday! (under an MIT license) | ||
|
||
There seem to be a lot of (valid) questions about why a new minifier is necessary so we want to write a post about some reasons and the future. | ||
|
||
## Why minify? | ||
|
||
## Issues with current minifiers | ||
|
||
Tools such as [uglify](https://github.com/mishoo/UglifyJS2) don't currently support targeting the latest version of Javascript ([yet](https://github.com/mishoo/UglifyJS2/issues/448)). | ||
|
||
We currently to use tools like Babel to compile ES6 code down to ES5 code to support older browsers. Then we use something like uglify to minify our code to cut down on the bundle size. | ||
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. extra " to "? |
||
|
||
However as browsers implement more ES6 features and we drop support for older browser versions, there will be a point where you wouldn't have to compile ES6 code to ES5 because all the support targets already understand ES6. However b ecause uglify cannot parse ES6, you would still have to compile down to ES5 anyway. | ||
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. 😱 I had no idea! I think that saying "there will be a point where you wouldn't have to compile ES6 code to ES5" should maybe be left out for two reasons:
Re: the uglify bit - that needs to be an h1 at the top of the page in big bold block letters. I had no clue and that would have been pretty annoying to find out on my own. 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. I can change the wording but i'm talking specifically about ES6 - I'm not saying you wouldn't use babel anymore since you'll need it for jsx/flow/experimental stuff/whatever the browser doesn't support. I'm just saying we can change the target output to be es5, es6, etc |
||
|
||
## Babili | ||
|
||
That's where Babili comes in. | ||
|
||
Babili is ES6+ aware because it is built using the Babel toolchain. More specifically, it is a set of babel plugins + a preset just like with the `es2015` preset. | ||
|
||
Like described in [Not Born to Die](http://babeljs.io/blog/2015/02/15/not-born-to-die) when Babel was renamed from 6to5, Babel was originally a specific transpiler for ES6 to ES5. A transpiler is just a compiler. The same techniques work to compile ES6 as it is to minify javascript. | ||
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. "As Described in" |
||
|
||
## Example | ||
|
||
When it's possible to only target browsers that support newer ES features, code sizes can be smaller because you don't have to transpile and then minify. | ||
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. This is already bing down for server builds (SSR of React for example) a lot of people use things like babel-preset-es2015-node4. Would be cool to mention that some of thes features could make SSR faster. 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. This point has been covered making this sentence feel redundant. Maybe merge with the next sentence and say: Say we're targeting the latest versions of Chrome, Firefox and Safari -- all of which support ES2015 classes. Then, compiling ES2015 classes to a constructor function and prototype methods (ES5) will result in more code (and potentially lose any optimizations browsers might have for classes). 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. 👍 but |
||
|
||
For example: say we want to minify a file that contains a ES6 class and we want to target the latest version of the chrome. | ||
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. s/contains a ES6/contains an ES6 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. wow so embarassing haha.. need spellcheck 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. you win all the internets. |
||
|
||
```js | ||
class Mangler { | ||
constructor(program) { | ||
this.program = program; | ||
} | ||
} | ||
// need this since otherwise Mangler isn't used | ||
new Mangler(); | ||
``` | ||
|
||
Before, we might run Babel to transpile [the class into a function](http://babeljs.io/docs/plugins/transform-es2015-classes) and run uglify on the compiled code to send to the browser. | ||
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.
|
||
|
||
```js | ||
// ES6 code -> Babel -> Uglify -> Minified ES5 Code | ||
var Mangler=function a(b){_classCallCheck(this,a),this.program=b};Mangler(); | ||
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. This example doesn't seem quite fair because it seems like Uglify here left in 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 even less fair if you don't actually instantiate ( It seems babili can't properly remove class definitions that have a superclass ( 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. class X {
y () {}
} Babili, as long as it can see the code before es2015-classes runs, can eliminate it; but Uglify (with preset es2015's output) has to preserve it. Wrapping the code given to uglify in an IIFE (as would effectively be done by a bundler) to make it non-global does let uglify also mangle the var and the babel helper names, but that's about it. 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. Tangentially related: why is the 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. agreed with @loganfsmyth let's wrap in an IIFE for proper comparison 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. Yeah a bad example (I didn't really think about it) - I just wanted to show an example with minifying es6 vs transpiling + minify 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. I used Babili to do the minification ^ btw I'm just going to use a different example without these issues? 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. // Example ES2015 Code
const { op: a, lhs: { op: b }, rhs: c } = getASTNode(); Before // ES2015+ code -> Babel -> Babili/Uglify -> Minified ES5 Code
;var _getASTNode=getASTNode(),a=_getASTNode.op,b=_getASTNode.lhs.op,c=_getASTNode.rhs; After // ES2015+ code -> Babili -> Minified ES2015+ Code
const{op:a,lhs:{op:b},rhs:c}=getASTNode(); 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. I guess we can include any example from es2015 code golf - http://codegolf.stackexchange.com/questions/37624/tips-for-golfing-in-ecmascript-6 |
||
``` | ||
|
||
With the babel minifier, you can just run the minifier which will work on ES6 code. | ||
|
||
```js | ||
// ES6 code -> Babili -> Minified ES6 Code | ||
class a{constructor(b){this.program=b}}new a; | ||
``` | ||
|
||
Also it's important to note that this isn't specific to ES6. Because Babel updates as ECMAScript updates (with [ES2015, ES2016, and now ES2017](http://babeljs.io/docs/plugins/#official-presets)) and follows the proposal process for experimental features (with our [stage-x presets](http://babeljs.io/docs/plugins/#stage-x-experimental-presets)), the minifier should be able to output whatever version of ECMAScript that is supported. | ||
|
||
## Usage | ||
|
||
If you already use Babel, you can just add the [babili](https://github.com/babel/babili#babel-preset) preset (babel-preset-babili) to your config. | ||
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. Worth noting that most people use Babel with Webpack + browserify which will run it over your owned files and not the final bundle. If you use it this way and simply add it to your |
||
|
||
You probably want to enable this only in production, so use the [env option](http://babeljs.io/docs/usage/babelrc/#env-option) which uses either `process.env.BABEL_ENV` or `process.env.NODE_ENV` | ||
|
||
```bash | ||
$ npm install babel-preset-babili --save-dev | ||
``` | ||
|
||
```js | ||
// previous .babelrc | ||
{ "presets": ["es2015"] } | ||
// .babelrc | ||
{ | ||
"presets": ["es2015"], | ||
"env": { | ||
"production": { | ||
"presets": ["babili"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
If you don't use Babel, you can use our standalone cli tool [`babili`](https://github.com/babel/babili#cli). (Currently it's just a wrapper for babel-cli + the preset) | ||
|
||
```bash | ||
$ npm install babili --save-dev | ||
``` | ||
|
||
```bash | ||
# equivalent to | ||
# babel src -d lib --presets=babili | ||
$ babili src -d lib | ||
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. Just looked at this since I was curious. Do we want this to also pass in 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. Agreed with @loganfsmyth; this surprised/confused me when I first tried babili. 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. Great point, we can certainly change that |
||
``` |
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.
uglify -> Uglify
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.
s/Javascript/JavaScript/
(or maybe it should say ECMAScript?)