[Babel] babel-plugin-unassert を利用して Production Build から assert を削除する
以前に保留していた疑問点。unassert を利用すると解決する。
[markdown]
> const assert = require(‘assert’); を含めて生成すると、JS が 17,000 行増える。
>
> [Babel: mocha + power-assert + babel-register + babel-preset-power-assert で es6 で書いたテストを実行する | deadwood](https://www.d-wood.com/blog/2016/04/20_7953.html)
## インストール
babel ので、プラグインを利用する。
> * [twada/babel-plugin-unassert: Babel plugin to encourage reliable programming by writing assertions in production code, and compiling them away from release.](https://github.com/twada/babel-plugin-unassert)
“`prettyprinted
% npm install –save-dev babel-plugin-unassert
“`
### babel コマンドで確認
以前に作成した [sandbox-es6](https://github.com/DriftwoodJP/sandbox-es6) で試行する。
`babel` コマンドを実行して比較すると、require などがなくなっていることが分かる。
“`prettyprinted
% $(npm bin)/babel –plugins unassert src/lib/math.es6.js > lib/mathUA.js
% $(npm bin)/babel src/lib/math.es6.js > lib/math.js
“`
## Browserify, Watchify の npm script
.babelrc に設定して、Browserify, Watchify まで落とし込む。
“`:.babelrc
{
“presets”: [
“es2015”
],
“env”: {
“development”: {
“presets”: [
“power-assert”
]
},
“production”: {
“plugins”: [
“babel-plugin-unassert”
]
}
}
}
“`
上記で設定した [env option](http://babeljs.io/docs/usage/babelrc/#env-option) を活かして `npm run-script` 化する。
“`json:package.json
“scripts”: {
“development”: “BABEL_ENV=development watchify ./src/basic.es6.js –debug –transform babelify –outfile ‘exorcist ./lib/basic.js.map > ./lib/basic.js’ –verbose”,
“production”: “BABEL_ENV=production browserify ./src/basic.es6.js –debug –transform babelify | exorcist ./lib/basic.js.map > ./lib/basic.js”,
},
“`
以下にまとめた。
* [DriftwoodJP/sandbox-es6: sandbox es6(2015)](https://github.com/DriftwoodJP/sandbox-es6)
[/markdown]