@amoo-miki/compression-webpack-plugin

中文文档
4.0.1-rc.1发布于 2023/03/17MITCommonJS近 30 天下载 2.9万

Prepare compressed versions of assets to serve them with Content-Encoding

原始使用文档
文档目录18 节
  1. Getting Started
  2. Options
  3. test
  4. include
  5. exclude
  6. algorithm
  7. compressionOptions
  8. threshold
  9. minRatio
  10. filename
  11. deleteOriginalAssets
  12. cache
  13. Examples
  14. Using Zopfli
  15. Using Brotli
  16. Multiple compressed versions of assets for different algori…
  17. Contributing
  18. License

npm node deps tests cover chat size

compression-webpack-plugin

Prepare compressed versions of assets to serve them with Content-Encoding.

Getting Started

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

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

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

webpack.config.js

const CompressionPlugin = require('compression-webpack-plugin');

module.exports = {
  plugins: [new CompressionPlugin()],
};

And run webpack via your preferred method.

Options

NameTypeDefaultDescription
test{String|RegExp|Array<String|RegExp>}undefinedInclude all assets that pass test assertion
include{String|RegExp|Array<String|RegExp>}undefinedInclude all assets matching any of these conditions
exclude{String|RegExp|Array<String|RegExp>}undefinedExclude all assets matching any of these conditions
algorithm{String|Function}gzipThe compression algorithm/function
compressionOptions{Object}{ level: 9 }Compression options for algorithm
threshold{Number}0Only assets bigger than this size are processed (in bytes)
minRatio{Number}0.8Only assets that compress better than this ratio are processed (minRatio = Compressed Size / Original Size)
filename{String|Function}[path].gz[query]The target asset filename.
cache{Boolean}trueEnable file caching

test

Type: String|RegExp|Array<String|RegExp> Default: undefined

Include all assets that pass test assertion.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      test: /\.js(\?.*)?$/i,
    }),
  ],
};

include

Type: String|RegExp|Array<String|RegExp> Default: undefined

Include all assets matching any of these conditions.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      include: /\/includes/,
    }),
  ],
};

exclude

Type: String|RegExp|Array<String|RegExp> Default: undefined

Exclude all assets matching any of these conditions.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      exclude: /\/excludes/,
    }),
  ],
};

algorithm

Type: String|Function Default: gzip

The compression algorithm/function.

String

The algorithm is taken from zlib.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      algorithm: 'gzip',
    }),
  ],
};

Function

Allow to specify a custom compression function.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      algorithm(input, compressionOptions, callback) {
        return compressionFunction(input, compressionOptions, callback);
      },
    }),
  ],
};

compressionOptions

Type: Object Default: { level: 9 }

Compression options for algorithm.

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

You can find all options here zlib.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      compressionOptions: { level: 1 },
    }),
  ],
};

threshold

Type: Number Default: 0

Only assets bigger than this size are processed. In bytes.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      threshold: 8192,
    }),
  ],
};

minRatio

Type: Number Default: 0.8

Only assets that compress better than this ratio are processed (minRatio = Compressed Size / Original Size). 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 assets that are smaller than the original. Use a value of Number.MAX_SAFE_INTEGER to process all assets even if they are larger than the original (useful when you are pre-zipping all assets for AWS)

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      minRatio: 0.8,
    }),
  ],
};

filename

Type: String|Function Default: [path].gz[query]

The target asset filename.

String

[file] is replaced with the original asset filename. [path] is replaced with the path of the original asset. [dir] is replaced with the directory of the original asset. [name] is replaced with the filename of the original asset. [ext] is replaced with the extension of the original asset. [query] is replaced with the query.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      filename: '[path].gz[query]',
    }),
  ],
};

Function

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      filename(info) {
        // info.file is the original asset filename
        // info.path is the path of the original asset
        // info.query is the query
        return `${info.path}.gz${info.query}`;
      },
    }),
  ],
};

deleteOriginalAssets

Type: Boolean Default: false

Whether to delete the original assets or not.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      deleteOriginalAssets: true,
    }),
  ],
};

cache

⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.

Type: Boolean|String Default: true

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

Boolean

Enable/disable file caching.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      cache: true,
    }),
  ],
};

String

Enable file caching and set path to cache directory.

webpack.config.js

module.exports = {
  plugins: [
    new CompressionPlugin({
      cache: 'path/to/cache',
    }),
  ],
};

Examples

Using 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:

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

webpack.config.js

const zopfli = require('@gfx/zopfli');

module.exports = {
  plugins: [
    new CompressionPlugin({
      compressionOptions: {
        numiterations: 15,
      },
      algorithm(input, compressionOptions, callback) {
        return zopfli.gzip(input, compressionOptions, callback);
      },
    }),
  ],
};

Using Brotli

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

Node 10.16.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 10.16.0 and later by just passing in the appropriate algorithm to the CompressionPlugin:

webpack.config.js

const zlib = require('zlib');

module.exports = {
  plugins: [
    new CompressionPlugin({
      filename: '[path].br[query]',
      algorithm: 'brotliCompress',
      test: /\.(js|css|html|svg)$/,
      compressionOptions: {
        // zlib’s `level` option matches Brotli’s `BROTLI_PARAM_QUALITY` option.
        level: 11,
      },
      threshold: 10240,
      minRatio: 0.8,
      deleteOriginalAssets: false,
    }),
  ],
};

Note The level option matches BROTLI_PARAM_QUALITY for Brotli-based streams

Multiple compressed versions of assets for different algorithm

webpack.config.js

const zlib = require('zlib');

module.exports = {
  plugins: [
    new CompressionPlugin({
      filename: '[path].gz[query]',
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.8,
    }),
    new CompressionPlugin({
      filename: '[path].br[query]',
      algorithm: 'brotliCompress',
      test: /\.(js|css|html|svg)$/,
      compressionOptions: {
        level: 11,
      },
      threshold: 10240,
      minRatio: 0.8,
    }),
  ],
};

Contributing

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

CONTRIBUTING

License

MIT

版本与发布记录

@amoo-miki/compression-webpack-plugin 共发布 2 个版本,首次发布于 2023/03/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.1-rc.1latest2023/03/173 年前
6.1.1-rc.12023/03/173 年前

关于 @amoo-miki/compression-webpack-plugin 的常见问题

@amoo-miki/compression-webpack-plugin 是什么?

Prepare compressed versions of assets to serve them with Content-Encoding

如何安装 @amoo-miki/compression-webpack-plugin?

在项目目录运行 npm install @amoo-miki/compression-webpack-plugin,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:webpack(^4.0.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@amoo-miki/compression-webpack-plugin 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/amoo-miki__compression-webpack-plugin 是否存在。

@amoo-miki/compression-webpack-plugin 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@amoo-miki/compression-webpack-plugin 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 10.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@amoo-miki/compression-webpack-plugin 可以商用吗?

@amoo-miki/compression-webpack-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@amoo-miki/compression-webpack-plugin 的下载量有多少?

根据 npm 官方下载统计,@amoo-miki/compression-webpack-plugin 在 2026/10/06 – 2026/11/04 这 30 天内被下载 29,341 次,日均约 978 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@amoo-miki/compression-webpack-plugin 还在维护吗?

最新版本 4.0.1-rc.1 发布于 2023/03/17(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/03/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。