Here are the basics of what we need to be able to write ES2015 code and get it compiled down to vanilla JS.
webpack.config.js
module.exports = {
entry: "./src/index.js",
output: {
path: __dirname,
filename: "./dist/index.js"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
}
]
}
};
You need these dev dependencies in package.json
:
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"webpack": "^3.5.5"
}
Now, running webpack
will basically generate the vanilla JS code we need.
Testing
In order to make this work with tests:
- Register the npm script:
"test": "mocha --require babel-register"
- Add the dev dependency:
babel-register
- Add a .babelrc
{
"presets": [ "env" ]
}
- Add
mocha
andchai
or your preferred testing framework.