侧边栏壁纸
博主头像
fastjrun博主等级

前大厂工程师,长期从事 Java 开发,架构设计,容器化等相关工作,精通java,熟练使用maven、jenkins等devops相关工具链,擅长容器化方案规划、设计和落地。

  • 累计撰写 70 篇文章
  • 累计创建 47 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

webpack-gzip构建压缩优化

fastjrun
2022-03-01 / 0 评论 / 0 点赞 / 374 阅读 / 801 字 / 正在检测是否收录...

compresison-webpack-plugin 压缩插件

插件描述

准备资源的压缩版本,以使用Content-Encoding服务

插件安装

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

备注:压缩插件使用过程中遇到的问题:
Cannot read property ‘tapPromise‘ of undefined
是版本过高导致的,所以卸载后重新安装低版本的插件。

npm uninstall compression-webpack-plugin
npm i compression-webpack-plugin@5.0.0

插件使用(在 vue.config.js里面)

const CompressionWebpackPlugin = require('compression-webpack-plugin');
......
configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      new CompressionWebpackPlugin(
        {
          filename: info => {
            return `${info.path}.gz${info.query}`
          },
          algorithm: 'gzip',
          threshold: 10240, // 只有大小大于该值的资源会被处理 10240
          test: new RegExp('\\.(' + ['js'].join('|') + ')$'
          ),
          minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
          deleteOriginalAssets: false // 删除原文件
        }
      )
    ]
  }
0

评论区