指令:npm install webpack@4.46.0 --save-dev
指令:npm install compression-webpack-plugin@6.1.1 --save-dev
vue.config.js
const CompressionWebpackPlugin = require('compression-webpack-plugin'); module.exports = { configureWebpack: config => { if (process.env.UNI_PLATFORM === 'h5') { filePath = 'static/js/'; //打包文件存放文件夹路径 Timestamp = '.' + new Date().getTime(); //时间戳 } config.plugins.push( new CompressionWebpackPlugin({ filename: "[path][base].gz", algorithm: "gzip", test: /\.js$/, threshold: 10240, minRatio: 0.8, exclude: /node_modules/, }) ); }, chainWebpack: config => { //通过运行 vue inspect plugins 的值html插件别名。。 config.plugin('html-index').tap(args => { args[0].minify = { removeAttributeQuotes: false } return args }) config.when(process.env.NODE_ENV === "production", config => { config.plugin('html-index').tap(args => { args[0].minify = { removeAttributeQuotes: false } return args }) config.output.filename("static/js/[name]-[contenthash].js"); config.output.chunkFilename("static/js/[id]-[chunkhash].js"); config.optimization.splitChunks({ name: false, //官方建议在生产环境时将 name 设置为 false,为了“it doesn't change names unnecessarily” hidePathInfo: true, }) }) }, }
这样打包出来的文件名就会短很多,且会打包压缩出gz文件.