Make webpack's library output compatible with babel6

前端 未结 3 1931
有刺的猬
有刺的猬 2020-12-28 19:24

Babel\'s 6th version changes the functioning of export default and in particular its relation with commonjs require.

To summarise, while un

3条回答
  •  萌比男神i
    2020-12-28 20:13

    Webpack 2 now supports es6 modules which partially solves this issue. Migrating from webpack 1 to webpack 2 is relatively painless. One just need to remember to disable babel's es6 module to commonjs conversion to make this work:

    .babelrc

    {
      "presets": [
        ["es2015", {"modules": false}]
      ]
    }
    

    However, unfortunately, it does not work properly with export default (but an issue is opened, hopefully a solution will be released eventually).

    EDIT

    Good news! Webpack 3 supports the output.libraryExport option that can be used to directly expose the default export:

    var path = require("path");
    var webpack = require("webpack");
    
    module.exports = {
      entry: {
        lib: [ path.resolve(__dirname, "src/main.js") ],
      },
      output: {
        path: path.resolve(__dirname, "dist"),
        filename: "mylib-build.js",
        library: "myLib",
        // Expose the default export.
        libraryExport: "default"
      },
      module: {
        loaders: [
          {
            test: /\.js$/,
            loader: "babel",
            include: path.resolve(__dirname, "src")
          }
        ]
      }
    };
    

提交回复
热议问题