Make webpack's library output compatible with babel6

前端 未结 3 1942
有刺的猬
有刺的猬 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条回答
  •  清酒与你
    2020-12-28 20:05

    You can use this solution (this is more like workaround, but it allow you to keep your sources from change):

    There is a loader called callback-loader. It allow you to change your sources in a build time by calling a callback and put a result instead of it. In other words you can turn all you require('module') into a require('module').default automatically in a build time.

    Here is your config for it:

    var webpackConfig = {
        module: {
            loaders: [
                { test: /\.js$/, exclude: /node_modules/, loader: 'callback' },
                ...
            ]
        },
        ...
        callbackLoader: {
            require: function() {
                return 'require("' + Array.prototype.join.call(arguments, ',') + '").default';
            }
        }
    };
    

提交回复
热议问题