Using Webpack2 with Babel is difficult. Webpack >= 2 understands ES6 import/export
natively, so you don't want
to use the full babel-preset-es2015. You want to have all of your babel options in your .babelrc
, but there
is no good way to override a single preset in babel-loader
while keeping the rest of your .babelrc
intact,
including env
s.
This preset is for you if you've already tried to parse your .babelrc
yourself (hint, use json5
).
Add the following to the top of your webpack config:
process.env.WEBPACK_VERSION = require('webpack/package.json').version;
If this preset detects that Webpack >= 2 is being used, it will exclude transform-es2015-modules-commonjs
.
Install both this preset, and the core 'babel-preset-es2015' that it modifies:
$ npm install --save-dev babel-preset-es2015-maybe-webpack babel-preset-es2015
Hint: You can get [loose] mode by using es2015-maybe-webpack/loose
.
.babelrc
{
"presets": ["es2015-maybe-webpack"]
}
$ babel script.js --presets es2015-maybe-webpack
require("babel-core").transform("code", {
presets: ["es2015-maybe-webpack"]
})