1、首先安装compression-webpack-plugin
npm install compression-webpack-plugin --save -dev
2、在Vue.config.js文件中配置:
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']configureWebpack: {plugins: [new CompressionWebpackPlugin({filename: '[path].gz[query]', // 提示compression-webpack-plugin@3.0.0的话asset改为filenamealgorithm: 'gzip',test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),threshold: 10240,minRatio: 0.8}),]
}
然后npm run build:prod --report ,在dist文件夹中可以看到 .gz文件,项目被压缩了