使用webpack捆绑包进行模块复制

时间:2019-04-08 07:22:53

标签: javascript node.js webpack ecmascript-6

如何使用Webpack V4跨包处理模块重复?

如果“模块A”具有依赖项“模块C”,并生成捆绑包“ index.js”。 如果“模块B”具有依赖项“模块C”(相同版本)并生成捆绑包“ index.js”。

如果“模块D”与捆绑软件“模块A”和捆绑软件“模块B”都具有依赖关系:检查生成的捆绑软件,我可以看到“模块C”代码是重复的。

如何消除跨不同包的重复代码?

编辑: 我的webpack配置:

[autorun]
open=ConsoleApplication1.exe
Label=(memory stick hehehe)  

导入示例:

var path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;


var webpackConfigManager = function(){

    let outDir = path.join(process.cwd(), "dist");
    let inPath = path.join(process.cwd(), 'src/index.js')

    let config = {
        mode: 'production', 
        devtool:false,
        plugins: [
            new BundleAnalyzerPlugin()
          ],
        entry: [inPath],
        output: {
            path: outDir,
            filename: 'index.js',
            libraryTarget: "umd" 
        },
        resolve: {
            extensions: ['.js', '.jsx', '.css', '.scss'],
            symlinks:false
        },
        module: {
            rules: [
                {
                    enforce: "pre",
                    test: /\.jsx?$/,
                    exclude: /node_modules|bower_components/,
                    loader: "eslint-loader",
                },
                {
                    test: /\.s?css$/,
                    use: ['style-loader', {
                        loader: 'css-loader',
                        options: {
                            modules: false,
                            importLoaders: 2,
                            sourceMap: true,
                            localIdentName: "[path][name]__[local]--[hash:base64:5]"
                        }
                    },
                        {
                            loader: "sass-loader",
                            options: {
                                sourceMap: true,
                                includePaths: ['./']
                            }
                        }
                    ]
                },

                {
                    test: /\.jsx?$/,
                    exclude: /node_modules|bower_components/,
                    use: {
                      loader: "babel-loader"
                    }
                },
            ]
        }
    };

    return config;
}

module.exports = env => {
return webpackConfigManager();
};

0 个答案:

没有答案