当前位置: 首页 > news >正文

vue打包压缩

参考

  • https://www.cnblogs.com/lafitewu/p/8309305.html

注意:方法1和方法2不能同时用

取消打包的map

config/index.js的productionSourceMap设置为false

抽取js

将部分常用又比较大的组件直接抽取为一个单独的js

打开webpack.base.conf.js,在module.exports.entry中添加想要抽取的组件,其中key是js的名,value是想要抽取的组件

//比如
  entry: {
    app: './src/main.js',
    vendor: ['vue','vuex','jquery','vue-awesome-swiper','vue-cool-select','vue-lazyload','vue-quill-editor','vuex-persistedstate'],
    leaflet: ['leaflet','leaflet-hotline'],
    echarts: ['echarts'],
  }

在这里插入图片描述

开启预编译

参考https://cloud.tencent.com/developer/section/1477569

开启之后页面速度会提升

打开webpack.base.conf.js,在module.exports.plugins中添加

new webpack.optimize.ModuleConcatenationPlugin()

在这里插入图片描述

压缩

方法1:对js和css单独压缩

uglifyjs-webpack-plugin对js压缩

需要dev环境引入uglifyjs-webpack-plugin

npm install --save-dev uglifyjs-webpack-plugin

找到/build/webpack.prod.conf.js 文件

添加

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

//在 const webpackConfig = merge(baseWebpackConfig, {的 plugins 中添加
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false
        }
      },
      sourceMap: config.build.productionSourceMap,
      parallel: true
    }),
 
//将  HtmlWebpackPlugin中的  minify的所有的都改为false

在这里插入图片描述

在这里插入图片描述

optimize-css-assets-webpack-plugin图片压缩

需要dev环境引入optimize-css-assets-webpack-plugin

npm install --save-dev optimize-css-assets-webpack-plugin

找到/build/webpack.prod.conf.js 文件

添加

const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
 
//在 const webpackConfig = merge(baseWebpackConfig, {的 plugins 中添加
    // css 压缩代码,将下面代码注释掉
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
          ? { safe: true, map: { inline: false } }
          : { safe: true }
    }),
 
//将  HtmlWebpackPlugin中的  minify的所有的都改为false

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

方法2vue开启Gzip压缩

依赖

npm install --save-dev compression-webpack-plugin

方法1

按照上面的方法
const CompressionWebpackPlugin = require('compression-webpack-plugin');
 
//在 const webpackConfig = merge(baseWebpackConfig, {的 plugins 中添加
new CompressionWebpackPlugin({
    filename: '[path].gz[query]',
    algorithm: 'gzip',
    test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'), // 匹配文件名
    threshold: 102, // 对0.1K以上的数据进行压缩
    minRatio: 0.8,
    deleteOriginalAssets: false /* process.env.NODE_ENV == 'production' // 是否删除源文件 */
});

方法2:

if (config.build.productionGzip) {
  const CompressionWebpackPlugin = require('compression-webpack-plugin')
 
  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

在这里插入图片描述

相关文章:

  • Go语言入门心法(三): 接口
  • 端到端的机器学习项目(Machine Learning 研习之六)
  • 深入了解 JavaScript 中的构造函数和对象创建
  • 最新ai创作系统CHATGPT系统源码+支持GPT4.0+支持ai绘画(Midjourney)
  • 海康威视、大华、宇视rtsp实时读取网络摄像头
  • c++string类的赋值问题
  • Android Handler/Looper视角看UI线程的原理
  • 1.12.C++项目:仿muduo库实现并发服务器之LoopThreadPool模块的设计
  • PCL点云处理之从两片点云中获取具有匹配关系的同名点对 (二百一十八)
  • 【音视频|ALSA】SS528开发板编译Linux内核ALSA驱动、移植alsa-lib、采集与播放usb耳机声音
  • vsc连接wsl安装vsc时遇到权限问题的解决方案
  • 如何使用ChatPPT生成PPT文档
  • SpringCloud-Gateway
  • LENOVO联想笔记本小新 Pro-14 2021AMD处理器ACH版(82MS)原厂Win10系统
  • ElementUI编辑表格单元格与查看模式切换的应用
  • 算法解析:LeetCode——机器人碰撞和最低票价
  • Qt工具开发,该不该跳槽?
  • Pytorch之MobileViT图像分类
  • 使用PyTorch处理多维特征输入的完美指南
  • hive抽取mysql里的表,如果mysql表没有时间字段如何做增量抽取数据
  • 体坛联播|曼联热刺会师欧联杯决赛,多哈世乒赛首日赛程出炉
  • 中信银行:拟出资100亿元全资设立信银金融资产投资有限公司
  • Meta正为AI眼镜开发人脸识别功能
  • 王日春已任教育部社会科学司司长,此前系人教社总编辑
  • 不主动上门检查,上海已制定14个细分领域“企业白名单”甄别规则
  • 杭温高铁、沪苏湖高铁明起推出定期票和计次票,不限车次执行优惠折扣