【发布时间】:2017-10-25 21:31:12
【问题描述】:
使用CommonsChunkPlugin,我目前将我的代码拆分为:
vendors.js
common.js
page-1.js
page-2-authenticated.js
page-3-authenticated.js
所以在page-1.html 上,我加载了以下脚本:
<script src="vendors.js" />
<script src="common.js" />
<script src="page-1.js" />
它工作正常,page-1.js、page-2-authenticated.js 和page-3-authenticated.js 中的所有共享代码都捆绑到common.js 中。
如您所见,我的应用程序要求用户登录page-2-authenticated.html 和page-3-authenticated.html。但是,page-2-authenticated.js 和page-3-authenticated.js 中的共享代码也捆绑到common.js 中。 但我不想打扰没有登录的用户,代码只在你登录时使用。
所以对于page-2-authenticated.html 我想拥有:
<script src="vendors.js" />
<script src="common.js" />
<script src="common-authenticated.js" /> // Shared code for authenticated users
<script src="page-2-authenticated.js" />
但是,当我在common-authenticated.js 中导出一个测试变量并将其导入page-2-authenticated.js 和page-3-authenticated.js 时,此共享代码仍捆绑在common.js 中。而common-authenticated.js 是空的(只是一些webpackJsonp([12],[],[85]);)。
我有以下 webpack 2 配置:
entry: {
vendors: ['react'],
common: 'index.js',
'common-authenticated': 'common-authenticated.js',
'page-1': 'page-1.js',
'page-2-authenticated': 'page-2-authenticated.js',
'page-3-authenticated': 'page-3-authenticated.js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
// The order of this array matters
names: ['common', 'vendors'],
minChunks: 2
})
]
问题:如何将特定代码捆绑到common-authenticated.js?有什么想法吗?
【问题讨论】:
标签: webpack webpack-2 chunks code-splitting