ModuleConcatenationPlugin

    这个插件会在 webpack 中实现以上的预编译功能。By default this plugin is already enabled in production and disabled otherwise. If you need it in other modes, you can add it manually:

    像文章中解释的,webpack 试图达到分批的作用域提升(scope hoisting)。它会将一些模块绑定到一个作用域内,但并不是任何情况下都会这么做。如果 webpack 不能绑定模块,将会有两个选择 Prevent 和 Root,Prevent 意思是模块必须在自己的作用域内。 Root 意味着将创建一个新的模块组。以下条件决定了输出结果:

    Condition

    Outcome

    Condition

    Non ES6 Module

    Outcome

    Prevent

    Condition

    Imported By Non Import

    Outcome

    Root

    Condition

    Outcome

    Root

    Condition

    Imported By Multiple Other Module Groups

    Outcome

    Root

    Condition

    Imported With

    Outcome

    Root

    Condition

    Affected By ProvidePlugin Or Using module

    Outcome

    Prevent

    Condition

    HMR Accepted

    Root

    Condition

    Using

    Outcome

    Prevent

    Condition

    In Multiple Chunks

    Outcome

    Prevent

    Condition

    export * from "cjs-module"

    Outcome

    Prevent

    以下 JavaScript 伪代码解释了算法:

    当我们使用 webpack CLI 时,加上参数 --display-optimization-bailout 将显示绑定失败的原因。在 webpack 配置里,只需将以下内容添加到 stats 对象中:



    byzyk byzyk