CompressionWebpackPlugin

[![npm][npm]][npm-url] [![node][node]][node-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![cover][cover]][cover-url] [![chat][chat]][chat-url] [![size][size]][size-url]

预先提供带 Content-Encoding 编码的压缩版本的资源。

需求

This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.

起步

To begin, you’ll need to install compression-webpack-plugin:

  1. $ npm install compression-webpack-plugin --save-dev

Then add the plugin to your webpack config. For example:

webpack.config.js

  1. const CompressionPlugin = require('compression-webpack-plugin');
  2. module.exports = {
  3. plugins: [
  4. new CompressionPlugin()
  5. ]
  6. }

And run webpack via your preferred method.

选项

test

类型:String|RegExp|Array<String|RegExp> Default: undefined

匹配所有对应的文件。

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. test: /\.js(\?.*)?$/i
  4. })

include

类型:String|RegExp|Array<String|RegExp> 默认:undefined

所有包含(include)的文件。

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. include: /\/includes/
  4. })

exclude

类型:String|RegExp|Array<String|RegExp> 默认:undefined

所有排除(exclude)的文件。

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. exclude: /\/excludes/
  4. })

cache

类型:Boolean|String 默认:false

Enable file caching. The default path to cache directory: node_modules/.cache/compression-webpack-plugin.

Boolean

Enable/disable file caching.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. cache: true
  4. })

String

Enable file caching and set path to cache directory.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. cache: 'path/to/cache'
  4. })

filename

类型:String|Function 默认:[path].gz[query]

目标资源文件名称。

String

[file] is replaced with the original asset filename. [path] is replaced with the path of the original asset. [query] is replaced with the query.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. filename: '[path].gz[query]'
  4. })

Function

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. filename(info) {
  4. // info.file is the original asset filename
  5. // info.path is the path of the original asset
  6. // info.query is the query
  7. return `${info.path}.gz${info.query}`
  8. }
  9. })

algorithm

类型:String|Function 默认:gzip

The compression algorithm/function.

String

The algorithm is taken from zlib.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. algorithm: 'gzip'
  4. })

Function

Allow to specify a custom compression function.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. algorithm(input, compressionOptions, callback) {
  4. return compressionFunction(input, compressionOptions, callback);
  5. }
  6. })

compressionOptions

类型:Object 默认:{ level: 9 }

If you use custom function for the algorithm option, the default value is {}.

Compression options. You can find all options here zlib.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. compressionOptions: { level: 1 }
  4. })

threshold

类型:Number 默认:0

只处理比这个值大的资源。按字节计算。

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. threshold: 8192
  4. })

minRatio

类型:Number 默认:0.8

只有压缩率比这个值小的资源才会被处理(minRatio = 压缩大小 / 原始大小)。 Example: you have image.png file with 1024b size, compressed version of file has 768b size, so minRatio equal 0.75. In other words assets will be processed when the Compressed Size / Original Size value less minRatio value. You can use 1 value to process all assets.

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. minRatio: 0.8
  4. })

deleteOriginalAssets

类型:Boolean 默认:false

是否删除原始资源。

  1. // 在 webpack.config.js 中
  2. new CompressionPlugin({
  3. deleteOriginalAssets: true
  4. })

示例

使用 Zopfli

Prepare compressed versions of assets using zopfli library.

ℹ️ @gfx/zopfli require minimum 8 version of node.

To begin, you’ll need to install @gfx/zopfli:

  1. $ npm install @gfx/zopfli --save-dev

webpack.config.js

  1. const zopfli = require('@gfx/zopfli');
  2. module.exports = {
  3. plugins: [
  4. new CompressionPlugin({
  5. compressionOptions: {
  6. numiterations: 15
  7. },
  8. algorithm(input, compressionOptions, callback) {
  9. return zopfli.gzip(input, compressionOptions, callback);
  10. }
  11. })
  12. ]
  13. }

使用 Brotli

Brotli is a compression algorithm originally developed by Google, and offers compression superior to gzip.

Node 11.7.0 and later has native support for Brotli compression in its zlib module.

We can take advantage of this built-in support for Brotli in Node 11.7.0 and later by just passing in the appropriate algorithm to the CompressionPlugin:

  1. // 在 webpack.config.js 中
  2. module.exports = {
  3. plugins: [
  4. new CompressionPlugin({
  5. filename: '[path].br[query]',
  6. algorithm: 'brotliCompress',
  7. test: /\.(js|css|html|svg)$/,
  8. compressionOptions: { level: 11 },
  9. threshold: 10240,
  10. minRatio: 0.8,
  11. deleteOriginalAssets: false
  12. })
  13. ]
  14. }

N.B.: The level option matches BROTLI_PARAM_QUALITY for Brotli-based streams

Contributing

Please take a moment to read our contributing guidelines if you haven’t yet done so.

CONTRIBUTING

License

MIT