将公共的依赖模块提取到现有的入口chunk中,或者提取到一个新生成的chunk。让我们使用这个插件,将之前的示例中重复的lodash
模块移除:
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
entry: {
index: './src/index.js',
another: './src/another-module.js',
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
+ optimization: {
+ splitChunks: {
+ chunks: 'all',
+ },
+ },
};
使用optimization.splitChunks
配置选项之后,现在应该可以研磨,index.bundle.js
和another.bundle.js
中已经移除了重复的依赖模块。需要注意的是,插件将lodash
分离到单独的块,并且将其从主捆绑包中移除,减小了大小。执行npm run build
查看效果