依赖的插件
npm install compression-webpack-plugin -D
使用
const CompressionPlugin = require('compression-webpack-plugin')
plugins:[
new CompressionPlugin({
filename:"[path][base].gz",// 文件名
algorithm: "gzip", // 压缩算法
threshold:0, // 只处理大于此大小的资产。以字节为单位。
minRatio:0.8, // 只处理压缩率大于此的资源
deleteOriginalAssets:false,//是否删除原始资产
})
]
针对不同算法的多个资产压缩版本
const zlib = require("zlib");
module.exports = {
plugins: [
new CompressionPlugin({
filename: "[path][base].gz",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
new CompressionPlugin({
filename: "[path][base].br",
algorithm: "brotliCompress",
test: /\.(js|css|html|svg)$/,
compressionOptions: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
threshold: 10240,
minRatio: 0.8,
}),
],
};