webpack能不能按照js的不同功能,对node_modules里的包分开打包?

比如,我有个tab用到了f2,有的tab用到了moment,能不能把这两个包分开,而不是放在一个单独的vendor里。

阅读 2.4k
1 个回答

可以
1.使用webpack的import函数

import ( /* webpackChunkName: "moment" */ 'moment').then(({ default: moment}) => {
    
}).catch(error => 'An error occurred while loading the component');

将会把moment单独打包 异步引用
2.手动配置webpack的 splitChunks插件中的cacheGroups参数

splitChunks: {
    cacheGroups: {
        moment: {
            test: /[\\/]node_modules[\\/](moment)[\\/]/,
            name: 'moment',
            priority: 3,
            chunks: 'initial',
        }
    }
}
推荐问题