
- 0133技术站
- 联系QQ:18840023
- QQ交流群
- 微信公众号

提供带 Content-Encoding 编码的压缩版的资源
npm i -D compression-webpack-plugin
webpack.config.js
const CompressionPlugin = require("compression-webpack-plugin")
module.exports = {
plugins: [
new CompressionPlugin(...options)
]
}| Name | Type | Default | Description | |
|---|---|---|---|---|
test | {RegExp} | . | 处理所有匹配此 {RegExp} 的资源 | |
asset | {String} | [path].gz[query] | 目标资源名称。[file] 会被替换成原资源。[path] 会被替换成原资源路径,[query] 替换成原查询字符串 | |
filename | {Function} | false | 一个 {Function} (asset) => asset 函数,接收原资源名(通过 asset 选项)返回新资源名 | |
algorithm | `{String\ | Function}` | gzip | 可以是 (buffer, cb) => cb(buffer) 或者是使用 zlib 里面的算法的 {String} |
threshold | {Number} | 0 | 只处理比这个值大的资源。按字节计算 | |
minRatio | {Number} | 0.8 | 只有压缩率比这个值小的资源才会被处理 | |
deleteOriginalAssets | {Boolean} | false | 是否删除原资源 |
testwebpack.config.js
[
new CompressionPlugin({
test: /\.js/
})
]assetwebpack.config.js
[
new CompressionPlugin({
asset: '[path].gz[query]'
})
]filenamewebpack.config.js
[
new CompressionPlugin({
filename (asset) {
asset = 'rename'
return asset
}
})
]algorithmwebpack.config.js
[
new CompressionPlugin({
algorithm: 'gzip'
})
]thresholdwebpack.config.js
[
new CompressionPlugin({
threshold: 0
})
]minRatiowebpack.config.js
[
new CompressionPlugin({
minRatio: 0.8
})
]deleteOriginalAssetswebpack.config.js
[
new CompressionPlugin({
deleteOriginalAssets: true
})
] Joshua Wiens | Juho Vepsäläinen | Michael Ciniawsky | ![]() Alexander Krasnoyarov |
推荐手册