【问题标题】:Webpack, how to take out module into his own build layer?Webpack,如何将模块取出到自己的构建层中?
【发布时间】:2016-04-26 09:04:46
【问题描述】:

使用默认构建设置,我得到以下构建层: (X+A)、(Y+A+B)、(Z+B)。

我想要: (X+A), (Y+A), Z, B​​p>

当我们询问 Y 和 Z 模块时,B 应该只加载一次。

我找到了CommonsChunkPlugin,但我无法正确配置。

var webpack = require("webpack");
var CommonsPlugin = new require("webpack/lib/optimize/CommonsChunkPlugin");

module.exports = {
    entry: {
        main: "./main"
    },
    resolve: {
        modulesDirectories: [
            "."
        ]
    },
    output: {
        publicPath: "js/",
        filename: "[name].builded.js"
    },
    plugins: [
           new CommonsPlugin({
            //  What should I write here?
            })
    ]
};

【问题讨论】:

    标签: build webpack commonschunkplugin front-end-optimization


    【解决方案1】:

    看来您应该添加 B 作为单独的入口点:

    entry: {
       main: "./main",
       Bentry: ["B"]
    },
    

    并在 plugins 部分添加 CommonsChunkPlugin:

    new webpack.optimize.CommonsChunkPlugin('Bentry', 'B.js'),
    

    【讨论】:

    • 试过了,出现错误“不允许对入口点的依赖”
    • 对不起,Bentry 必须是一个数组。编辑了我的答案。
    • 现在我得到了公共块 B,但我有模块 (Y+A+B)、(Z+B) 并且从这个公共块中没有任何好处。我需要将模块 B 的代码从父构建块中排除
    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 2017-07-21
    • 2017-08-17
    • 1970-01-01
    • 2017-09-14
    • 2017-09-11
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多