在线文档教程

CompressionWebpackPlugin

CompressionWebpackPlugin

准备资源的压缩版本以通过Content-Encoding为其提供服务

安装

npm i -D compression-webpack-plugin

用法

webpack.config.js

const CompressionPlugin = require("compression-webpack-plugin") module.exports = { plugins: [ new CompressionPlugin(...options) ] }

选项

Name类型默认描述
test{RegExp}.所有匹配此{RegExp}的资产都会被处理
asset{String}path.gzquery目标资产名称。文件被替换为原始资产。路径将替换为原始资产的路径并使用查询进行查询
filename{Function}{功能}(资产)=>接收资产名称(处理资产选项后)并返回新资产名称的资产
algorithm{String|Function}gzip的可以是(缓冲区,cb)=> cb(缓冲区),或者如果使用了{String},则该算法取自zlib` |
threshold{Number}0只处理大于此大小的资产。以字节为单位
minRatio{Number}0.8只有压缩好这个比率的资产才能被处理
deleteOriginalAssets {Boolean}是否删除原始资产

webpack.config.js

[ new CompressionPlugin{ test: /\.js/ }) ]

asset

webpack.config.js

[ new CompressionPlugin{ asset: '[path].gz[query]' }) ]

filename

webpack.config.js

[ new CompressionPlugin{ filename (asset) { asset = 'rename' return asset } }) ]

algorithm

webpack.config.js

[ new CompressionPlugin{ algorithm: 'gzip' }) ]

threshold

webpack.config.js

[ new CompressionPlugin{ threshold: 0 }) ]

minRatio

webpack.config.js

[ new CompressionPlugin{ minRatio: 0.8 }) ]

deleteOriginalAssets

webpack.config.js

[ new CompressionPlugin{ deleteOriginalAssets: true }) ]