使用npm脚本部分的webpack来编译es6

using webpack in npm scripts section to transcompile es6

本文关键字:编译 es6 webpack npm 脚本部 使用      更新时间:2023-09-26

使用browserify,我可以通过将es6添加到package.json->脚本中来将其编译为es5。

如何使用webpack和npm脚本?

"build-modular": "browserify index.js -t babelify -o index-bundle.js"

在webpack中,您使用加载程序而不是转换。通常在webpack配置文件中配置加载程序。但是,您也可以在命令行中绑定加载器。

当您想要babelify您的index.js文件时,您需要安装相应的babel加载器和所需的babel包:

npm install --save-dev webpack babel-core babel-loader babel-preset-es2015

现在您可以创建这样的构建脚本条目:

"build-modular": "webpack index.js index-bundle.js  --module-bind 'js=babel?presets[]=es2015'"

运行它:

npm run build-modular