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
Type
Default
Description
Name
test
Type
{RegExp}
Default
.
Description
处理所有匹配此 {RegExp} 的资源
Name
asset
Type
{String}
Default
[path].gz[query]
Description
目标资源名称。[file] 会被替换成原资源。[path] 会被替换成原资源路径,[query] 替换成原查询字符串
Name
filename
Type
{Function}
Default
false
Description
一个 {Function} (asset) => asset 函数,接收原资源名(通过 asset 选项)返回新资源名
Name
algorithm
Type
{String|Function}
Default
gzip
Description
可以是 (buffer, cb) => cb(buffer) 或者是使用 zlib 里面的算法的 {String}
Name
threshold
Type
{Number}
Default
0
Description
只处理比这个值大的资源。按字节计算
Name
minRatio
Type
{Number}
Default
0.8
Description
只有压缩率比这个值小的资源才会被处理
Name
deleteOriginalAssets
Type
{Boolean}
Default
false
Description
是否删除原资源

test

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
  })
]

维护人员


    Joshua Wiens


    Juho Vepsäläinen


    Michael Ciniawsky


    Alexander Krasnoyarov