vue 打包设置

1、vue webpack配置

filename: '[path][base].gz',// 设置成这样就行了

const { defineConfig } = require('@vue/cli-service')

const debug = process.env.NODE_ENV !== 'production'


const CompressionWebpackPlugin = require('compression-webpack-plugin')

const TerserPlugin = require("terser-webpack-plugin");
const productionGzipExtensions = ['js', 'css'];

module.exports = defineConfig({
  transpileDependencies: true,

})
module.exports = {
  publicPath: "./",
  outputDir: `dist/prod`,
  lintOnSave: false,//是否开启eslint保存检测 ,它的有效值为 true || false || 'error'\
  devServer: {
    open: true, //配置自动启动浏览器
    // host: "192.168.10.18",
    https: false,
    // hotOnly: false, //热更新
    port: 7878,
    proxy: {
      

    },
    client: {
      overlay: false,
    },
  },
  // configureWebpack: {
  //   resolve: {
  //     alias: {
  //       '@': path.resolve(__dirname, 'src')
  //     }
  //   }
  // },

  // chainWebpack: (config) => {
  //   config.resolve.alias
  //     .set('@', resolve('src'))
  //     .set('assets', resolve('src/assets'));
  // },
  // css: {
  //   loaderOptions: {
  //     postcss: {
  //       plugins: [
  //         require("postcss-px2rem-exclude")({
  //           remUnit: 192, //1920
  //           // exclude: /node_modules|folder_name/i
  //         }),
  //       ],
  //     },
  //   },
  // },

  productionSourceMap: false,
  configureWebpack: config => {
    if (debug) { // 开发环境配置
      config.devtool = 'source-map'
    }else{

      // 开启分离js
      config.optimization = {
        runtimeChunk: 'single',
        splitChunks: {
          chunks: 'all',  
          maxInitialRequests: Infinity,
          minSize: 20000,

          cacheGroups: {
            vendor: {
              test: /[\\/]node_modules[\\/]/, // 提取 node_modules 中的库
              name: 'vendors',                // 提取到 vendors.js
              chunks: 'all',
              // priority: -10,
              // name (module) {
              //   const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
              //   return `npm.${packageName.replace('@', '')}`
              // }

            },
          },
        },
        minimize: true,
        minimizer: [
          new TerserPlugin({
            extractComments: false,//不将注释提取到单独的文件中
            terserOptions: {
              compress: {
                // 移除 warning,console,debugger等
                warnings:false, 
                drop_console: true,
                drop_debugger: true,
                pure_funcs: ['console.log'] // 移除console
              },
              output: {
                // 去掉注释内容
                comments: false,
              }
            }
          })
        ]
      };
      config.plugins.push(new CompressionWebpackPlugin({
        // filename: '[path].gz[query]',
        filename: '[path][base].gz',
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8,

      }))
    }

  },
  configureWebpack:{
    
    module: {
      rules: [
        {
          test: /\.(jpg|png|gif)$/,
          use: [
            {
              loader: 'image-webpack-loader',
                options: {
                 mozjpeg: {
                  progressive: true,
                  quality: 65
                 },
                optipng: {
                  enabled: false
                },
                pngquant: {
                  quality: [0.65, 0.90],
                  speed: 4
                },
                gifsicle: {
                  interlaced: false
                  },
                webp: {
                  quality: 75
                }
  
  
              }
            }
          ]
          
        },
      ],
    },
  }




}

2、nginx 配置访问gz

http {
  gzip_static on;          # 启用预压缩文件查找
  brotli_static on;        # 启用 Brotli 预压缩文件(需安装 ngx_brotli 模块)

  # 若未找到预压缩文件,动态压缩
  gzip on;
  gzip_types text/plain text/css application/json application/javascript text/xml;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值