Make webpack's library output compatible with babel6

前端 未结 3 1928
有刺的猬
有刺的猬 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:06

    Was just going at this my self. Whether one like to call it a workaround or solution, there seem to be a Babel plugin that "solve it".

    Using the plugin babel-plugin-add-module-exports as referenced in https://stackoverflow.com/a/34778391/1592572

    Example config

    var webpackOptions = {
        entry: {
            Lib1: './src/Lib1.js',
            Lib2: './src/Lib2.js'
        },
        output: {
            filename: "Master.[name].js",
            library: ["Master","[name]"],
            libraryTarget: "var"
        },
        module: {
            loaders: [
                {
                    loader: 'babel',
                    query: {
                        presets: ['es2015'],
                        plugins: ["add-module-exports"]
                    }
                }
            ]
        }
    };
    

    This yields Master.Lib1 to be lib1 instead of Master.Lib1.default.

提交回复
热议问题