Vue项目优化之webpack

Vue项目优化之webpack

  1. babel-plugin-transform-remove-console 生产环境去除console
  2. compression-webpack-plugin 代码压缩gz
  3. 路由的懒加载和按需加载
  4. babel-plugin-component 组件的按需引入
  5. svg-sprite-loader 使用矢量图
  6. vendor文件的拆包
  7. 切换多入口模式
  8. 打包不生成.map文件

6.7.8点配置vue.config.js如下:

vendor文件的拆包
切换多入口模式
打包不生成.map文件

module.exports = {
	publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
    productionSourceMap: false,//打包不生成.map文件

	pages:{
        index:{//配置多入口
            entry: "./src/main.js",
            template: "./public/index.html" //模板
        },
        test:{
            entry: "./src/test.js",//创建一个test.js文件随便写几个代码
            template: "./public/index.html"
        }
    },
	
	chainWebpack(config) {
	  config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .optimization.splitChunks({ //vendor文件的拆包
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // 重量需要大于libs和app,否则会被打包成libs或app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 匹配 cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // 可以自定义规则
                  minChunks: 3, //  最少公共的数量
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
        }
      )
	}
}

配置以上打包后:生成了2个html文件,分别单独引入其中的js文件
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值