babel-plugin-transform-helper
Babel transforms some awesome ES6 features to ES5 with extra code, such as Class,JSX. This plugin makes all generated extra codes to one module which significantly reduces the bundle code size.
Install
npm install babel-plugin-transform-helper --save-dev
Usage
.babelrc
{
...
"plugins":["babel-plugin-transform-helper"]
}
code
babel.transform(code,{
...
plugins:["babel-plugin-transform-helper"]
})
Options
You can specify the helper bundle file path by helperFilename
property.
babel.transform(code,{
...
plugins:[
["babel-plugin-transform-helper",{
helperFilename:'tempHelper.js'
}
]
]
})
How it work
This plugin write all generated helper code to one module and the original codes require the module instead of calling from local.Let's support we have two class file.
//Point.js { super; this; } { return this0 } { return this1 } //Rect.js ; { super; this; } { return this0this1 }
When transfers ES6 class,import,typeof ... to ES5, babel add helper code in each original file, the Point.js
will be transfered to
var _createClass =//..some code {//..some code} {//..some code} {//..some code} var { ; { ; var _this = ; _this; return _this; } ; return Point; }Array
If every file adds these same code the size of bundle file used in browser can be extremely larger than the original source file. The helper codes are the same in every file,so we can group them in a moudle and require the module instead of call it from local, which make the Point.js
like below.
var { ; { ; var _this = ; _this; return _this; } ; return Point; }Array;
And generate a file tempHelper.js
exportsinherits=//..somecode exportscreateClass=//..somecode ...
When Point.js
and Rect.js
bundled together, they require the same module and the code size reduced.The temp module file will be write to the working directory and bundled automatically.