• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
斩月飞仙
赚钱使我得到想要的生活,而想要的生活使我快乐,所以四舍五入,约等于赚钱使我快乐
博客园    首页    新随笔    联系   管理    订阅  订阅

webpack代码分离CommonsChunkPlugin插件的使用(防止重复)

 1.webpack.config.js中添加:


const path = require('path'); + const webpack = require('webpack'); const HTMLWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { index: './src/index.js', another: './src/another-module.js' }, plugins: [ new HTMLWebpackPlugin({ title: 'Code Splitting' - }) + }), + new webpack.optimize.CommonsChunkPlugin({ + name: 'common' // 指定公共 bundle 的名称。 + }) ], output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist') } };

2.然后就遇到了一个问题,还给出了一个解决方案,需要去查看文档中的插件章节

 

 

//optimization与plugins同级
optimization: {
    splitChunks: {
        cacheGroups: {
            commons: {
                name: "commons",
                chunks: "initial",
                minChunks: 2
            }
        }
    }
},

 

3.运行npm run build,如果有公共部分可得到common.bundle.js文件,如果没有则不会生成这个文件

 

 

posted @ 2019-11-26 10:48  斩月飞仙  阅读(450)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3