JS笔记:webpack基础es2015

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 and chai or your preferred testing framework.

你可能感兴趣的:(JS笔记:webpack基础es2015)