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

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

阅读 2.3k
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',
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题