[英]Extracting common chunks amongst multiple compiler configurations in webpack?
I'm trying out the multi-compiler option in webpack and am following the example at their github. 我正在尝试webpack中的多编译器选项,并在他们的github上关注这个例子 。 However, I can't seem to understand how I can split out the common code amongst the multiple configurations.
但是,我似乎无法理解如何在多个配置中拆分公共代码。
For example, I may have same vendor libraries used in the different set of configurations. 例如,我可能在不同的配置集中使用相同的供应商库。 I would like to have these shared codes to be bundled to one single common file.
我想将这些共享代码捆绑到一个公共文件中。
I tried the following but it ended up creating an individual bundles of the vendors
entry for each compile configuration. 我尝试了以下内容,但最终为每个编译配置创建了各个
vendors
条目包。
var path = require("path");
var webpack = require("webpack");
module.exports = [
{
name: "app-mod1",
entry: {
vendors: ['jquery', 'react', 'react-dom'],
pageA: ['./mod1/pageA'],
pageB: ['./mod1/pageB']
},
output: {
path: path.join(__dirname, "/mod1/js"),
filename: "[name].bundle.js"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendors'],
minChunks: Infinity
})
]
},
{
name: "app-mod2",
entry: {
vendors: ['lodash', 'react', 'react-dom'],
pageA: ['./mod2/pageA'],
pageB: ['./mod2/pageB']
},
output: {
path: path.join(__dirname, "/mod2/js"),
filename: "[name].bundle.js"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendors'],
minChunks: Infinity
})
]
}
];
Since react, react-dom are shared between the 2 compilations, my intention is for them to be bundled as a single file which can be shared instead of creating a same bundle for each compilation. 由于react,react-dom在2个编译之间共享,我的意图是将它们捆绑为单个文件,可以共享而不是为每个编译创建相同的包。
How can I extract the common chunks out of multiple compiler configurations? 如何从多个编译器配置中提取公共块?
You can't do that job in the way you want. 你不能以你想要的方式完成这项工作。
@Carven, I am afraid that you can't achieve your goal via MultiCompiler
of Webpack, MultiCompiler
is not meant to do that job, at least for the near feature. @Carven,我担心你无法通过
MultiCompiler
的MultiCompiler实现你的目标, MultiCompiler
并不打算做那项工作,至少对于近乎功能而言。
See the source code for initiating the MultiCompiler instance , it actually initiates separate Compiler
instances. 请参阅启动MultiCompiler实例的源代码 ,它实际上启动了单独的
Compiler
实例。 These compiler have no data shared between. 这些编译器之间没有共享数据。
See also the source of running MultiCompiler instance , the compilers instance also run separately without sharing data. 另请参阅运行MultiCompiler实例的源代码 ,编译器实例也单独运行而不共享数据。
The only thing these compilers share is the Stats
instance they produce and merge into a MultiStats
. 这些编译器共享的唯一内容是它们生成的
Stats
实例并合并到MultiStats
。
By the way, there is no clue in the example you mentioned that some modules are shared between multi-compilers. 顺便说一句, 你提到的例子中没有任何线索,一些模块在多编译器之间共享。
As described by @Tzook-Bar-Noy, IMHO, you have to use muti-entries to achieve MPA(Multi-page Application) bundling. 正如@ Tzook-Bar-Noy,恕我直言所描述的那样,你必须使用多条目来实现MPA(多页面应用程序)捆绑。
I noticed a library called webpack-multi-configurator is using the multi-compiler feature. 我注意到一个名为webpack-multi-configurator的库正在使用多编译器功能。 But I don't think it will share common chunk(s).
但我不认为它会分享共同的块。
You can extract the shared code into another compilation, and bundle it with DllBundlesPlugin. 您可以将共享代码提取到另一个编译中,并将其与DllBundlesPlugin捆绑在一起。 later consume this DLL via DLLReferencePlugin and add it to your page either manually or via HTMLWebpackPlugin's add-asset-html-webpack-plugin
稍后通过DLLReferencePlugin使用此DLL并将其手动或通过HTMLWebpackPlugin的add-asset-html-webpack-plugin添加到您的页面
Bolierplate can be reduced by using autodll-webpack-Plugin 使用autodll-webpack-Plugin可以减少Bolierplate
I learned about it now, and this topic seems quite hard to understand in webpack docs. 我现在已经了解了它,在webpack文档中这个主题似乎很难理解。 I managed to create something that works, as it created 2 separate files and extracted the common dependencies to another file.
我设法创建了一些有用的东西,因为它创建了2个单独的文件,并将公共依赖项提取到另一个文件中。
Here is my webpack config: 这是我的webpack配置:
{
entry: {
pageA: "./first/first",
pageB: "./second/second"
},
output: {
path: path.join(__dirname, "js"),
filename: "[name].js"
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ["vendor", "common"],
})
]
};
the output of this will be: 这个输出将是:
./js/
common.js
vendor.js
pageA.js
pageB.js
I created a repo with the example I worked on: https://github.com/tzookb/webpack-common-vendor-chunks 我用我工作的例子创建了一个repo: https : //github.com/tzookb/webpack-common-vendor-chunks
when I open a new html file I load these files: 当我打开一个新的html文件时,我加载这些文件:
first page:
common.js
vendor.js
pageA.js
sec page:
common.js
vendor.js
pageB.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.