我的供应商捆绑包真的很大,我想分为两个部分。(一个具有所有与React相关的软件包的部分,另一部分带有其余的软件包(。
我目前必须创建的供应商捆绑包是:
new webpack.optimize.CommonsChunkPlugin({
name: ['vendor'],
filename: '[name].bundle.js',
minChunks: module => module.context.includes('node_modules')
})
我尝试在下面添加这些不同的方法,但到目前为止还没有成功:
// approach 1
new webpack.optimize.CommonsChunkPlugin({
name: 'react',
chunks: ['vendor'],
minChunks: ({resource}) => (/node_modules/react/).test(resource)
})
// approach 2
new webpack.optimize.CommonsChunkPlugin({
name: ['react'],
filename: '[name].bundle.js',
minChunks: ({resource}) => (/node_modules/react/).test(resource)
})
拆分发生,但我无法在浏览器中运行。在我的控制台中,我得到:
vendor.bundle.js:1 Uncaught ReferenceError: webpackJsonp is not defined
at vendor.bundle.js:1
(anonymous) @ vendor.bundle.js:1
12:52:57.478 react.bundle.js:55 Uncaught TypeError: Cannot read property 'call' of undefined
at __webpack_require__ (react.bundle.js:55)
at eval (react.development.js:18)
at eval (react.development.js:1356)
at Object.603 (react.bundle.js:747)
at __webpack_require__ (react.bundle.js:55)
at eval (index.js:6)
at Object.0 (react.bundle.js:156)
at __webpack_require__ (react.bundle.js:55)
at eval (index.jsx:8)
at Object.<anonymous> (client.bundle.js:1375)
我已经在我的html中加载了新文件。
我刚刚使用以下解决方案解决了问题:
new webpack.optimize.CommonsChunkPlugin({
name: ['vendor'],
filename: '[name].bundle.js',
minChunks: module => module.context.includes('node_modules') && !module.request.includes('scss')
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'react',
chunks: ['vendor'],
minChunks: ({resource}) => (/node_modules/react/).test(resource)
}),
处理它的一种简单方法是指定供应商的内容并在此类入口属性中进行反应:
entry: {
app: ['./src/index.js'],
react: [
'react',
'react-dom'],
vendor: [
'other-libraries',
]
}
然后,您可以在Commonschunkplugin中指定这些条目:
new webpack.optimize.CommonsChunkPlugin({
name: ['react', 'vendor']
})