在我的 webpack.config.js (版本 1,2,3)文件中,我有
function isExternal(module) {
var context = module.context;
if (typeof context !== 'string') {
return false;
}
return context.indexOf('node_modules') !== -1;
}
在我的插件数组中
plugins: [
new CommonsChunkPlugin({
name: 'vendors',
minChunks: function(module) {
return isExternal(module);
}
}),
// Other plugins
]
现在我有一个文件,仅根据需要将第 3 方库添加到一个文件中。
如果您想更详细地区分供应商和入口点文件:
plugins: [
new CommonsChunkPlugin({
name: 'common',
minChunks: function(module, count) {
return !isExternal(module) && count >= 2; // adjustable
}
}),
new CommonsChunkPlugin({
name: 'vendors',
chunks: ['common'],
// or if you have an key value object for your entries
// chunks: Object.keys(entry).concat('common')
minChunks: function(module) {
return isExternal(module);
}
})
]
请注意,插件的顺序很重要。
另外,这将在版本 4 中发生变化。当它正式发布时,我会更新这个答案。
Update:Windows 用户的 indexOf 搜索更改