{"name":"compression-webpack-plugin","version":"12.0.0","keywords":["webpack"],"author":{"name":"Tobias Koppers @sokra"},"license":"MIT","_id":"compression-webpack-plugin@12.0.0","maintainers":[{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},{"name":"sokra","email":"tobias.koppers@googlemail.com"},{"name":"jhnns","email":"mail@johannesewald.de"},{"name":"15000621931","email":"784487301@qq.com"},{"name":"ev1stensberg","email":"evenstensberg@gmail.com"},{"name":"__hai","email":"haijie0619@gmail.com"}],"homepage":"https://github.com/webpack/compression-webpack-plugin","bugs":{"url":"https://github.com/webpack/compression-webpack-plugin/issues"},"dist":{"shasum":"6842bb3720407afb0686500411c765c37f37164d","tarball":"http://registry-npm.ywwl.com/compression-webpack-plugin/download/compression-webpack-plugin-12.0.0.tgz","fileCount":6,"integrity":"sha512-LR4mS19Jqq41XfA3xVMLrtzVNzqJbUHdzPeLRfQoLiAS9s87f0021fDuU89xxVQFcB6d20ufBkv4j1rQ4OowHw==","signatures":[{"sig":"MEYCIQDq3owUB0RLnQYyHPtCI2DKaNSgdjno7lDpwmxNa2Y/4QIhANqJrjIkbmyejnJUDZ6dRDsAm8dWUcRSXWUxcXvqAJjm","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":40937,"size":10439,"noattachment":false,"key":"/compression-webpack-plugin/-/compression-webpack-plugin-12.0.0.tgz"},"main":"dist/index.js","types":"types/index.d.ts","engines":{"node":">= 20.9.0"},"funding":{"url":"https://opencollective.com/webpack","type":"opencollective"},"gitHead":"03e9d08724eccd2e864db889d9b784ce9e96236a","scripts":{"fix":"npm-run-all -l fix:code fix:prettier","lint":"npm-run-all -l -p \"lint:**\"","test":"npm run test:coverage","build":"npm-run-all -p \"build:**\"","clean":"del-cli dist types","start":"npm run build -- -w","prepare":"husky && npm run build","pretest":"npm run lint","release":"standard-version","fix:code":"npm run lint:code -- --fix","prebuild":"npm run clean","security":"npm audit --production","lint:code":"eslint --cache .","test:only":"cross-env NODE_ENV=test jest","build:code":"cross-env NODE_ENV=production babel src -d dist --copy-files","commitlint":"commitlint --from=main","lint:types":"tsc --pretty --noEmit","test:watch":"npm run test:only -- --watch","build:types":"tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write","fix:prettier":"npm run lint:prettier -- --write","lint:prettier":"prettier --cache --list-different .","lint:spelling":"cspell --cache --no-must-find-files --quiet \"**/*.*\"","test:coverage":"npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage"},"_npmUser":{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},"repository":{"url":"git+https://github.com/webpack/compression-webpack-plugin.git","type":"git"},"_npmVersion":"11.6.2","description":"Prepare compressed versions of assets to serve them with Content-Encoding","directories":{},"_nodeVersion":"24.11.1","dependencies":{"schema-utils":"^4.2.0","serialize-javascript":"^7.0.3"},"_hasShrinkwrap":false,"devDependencies":{"del":"^7.1.0","jest":"^30.0.0","husky":"^9.1.4","memfs":"^4.11.1","cspell":"^9.7.0","eslint":"^9.30.1","del-cli":"^7.0.0","webpack":"^5.93.0","prettier":"^3.3.3","cross-env":"^7.0.3","@babel/cli":"^7.24.8","babel-jest":"^30.0.0","typescript":"^5.5.4","@babel/core":"^7.25.2","@gfx/zopfli":"^1.0.15","@types/node":"^20.14.9","file-loader":"^6.2.0","lint-staged":"^16.3.1","npm-run-all":"^4.1.5","@commitlint/cli":"^20.4.2","standard-version":"^9.5.0","@babel/preset-env":"^7.25.3","webpack-stats-plugin":"^1.1.3","eslint-config-webpack":"^4.3.2","workbox-webpack-plugin":"^7.1.0","@types/serialize-javascript":"^5.0.4","@commitlint/config-conventional":"^20.4.2"},"peerDependencies":{"webpack":"^5.1.0"},"_npmOperationalInternal":{"tmp":"tmp/compression-webpack-plugin_12.0.0_1772474103210_0.48868249688068355","host":"s3://npm-registry-packages-npm-production"},"readme":"<div align=\"center\">\n  <a href=\"https://github.com/webpack/webpack\">\n    <img width=\"200\" height=\"200\" src=\"https://webpack.js.org/assets/icon-square-big.svg\">\n  </a>\n</div>\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![tests][tests]][tests-url]\n[![cover][cover]][cover-url]\n[![discussion][discussion]][discussion-url]\n[![size][size]][size-url]\n\n# compression-webpack-plugin\n\nPrepare compressed versions of assets to serve them with Content-Encoding.\n\n## Getting Started\n\nTo begin, you'll need to install `compression-webpack-plugin`:\n\n```console\nnpm install compression-webpack-plugin --save-dev\n```\n\nor\n\n```console\nyarn add -D compression-webpack-plugin\n```\n\nor\n\n```console\npnpm add -D compression-webpack-plugin\n```\n\nThen add the plugin to your `webpack` config. For example:\n\n**webpack.config.js**\n\n```js\nconst CompressionPlugin = require(\"compression-webpack-plugin\");\n\nmodule.exports = {\n  plugins: [new CompressionPlugin()],\n};\n```\n\nFinally, run `webpack` using the method you normally use (e.g., via CLI or an npm script).\n\n## Options\n\n- **[`test`](#test)**\n- **[`include`](#include)**\n- **[`exclude`](#exclude)**\n- **[`algorithm`](#algorithm)**\n- **[`compressionOptions`](#compressionoptions)**\n- **[`threshold`](#threshold)**\n- **[`minRatio`](#minratio)**\n- **[`filename`](#filename)**\n- **[`deleteOriginalAssets`](#deleteoriginalassets)**\n\n### `test`\n\nType:\n\n```ts\ntype test = string | RegExp | (string | RegExp)[];\n```\n\nDefault: `undefined`\n\nInclude all assets that pass test assertion.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      test: /\\.js(\\?.*)?$/i,\n    }),\n  ],\n};\n```\n\n### `include`\n\nType:\n\n```ts\ntype include = string | RegExp | (string | RegExp)[];\n```\n\nDefault: `undefined`\n\nInclude all assets matching any of these conditions.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      include: /\\/includes/,\n    }),\n  ],\n};\n```\n\n### `exclude`\n\nType:\n\n```ts\ntype exclude = string | RegExp | (string | RegExp)[];\n```\n\nDefault: `undefined`\n\nExclude all assets matching any of these conditions.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      exclude: /\\/excludes/,\n    }),\n  ],\n};\n```\n\n### `algorithm`\n\nType:\n\n```ts\ntype algorithm =\n  | string\n  | ((\n      input: Buffer,\n      options: CompressionOptions,\n      callback: (\n        error: Error | null | undefined,\n        result:\n          | string\n          | ArrayBuffer\n          | SharedArrayBuffer\n          | Uint8Array\n          | readonly number[]\n          | {\n              valueOf(): ArrayBuffer | SharedArrayBuffer;\n            }\n          | {\n              valueOf(): string | Uint8Array | readonly number[];\n            }\n          | {\n              valueOf(): string;\n            }\n          | {\n              [Symbol.toPrimitive](hint: \"string\"): string;\n            },\n      ) => void,\n    ) => void);\n```\n\nDefines the compression algorithm or function to use. Defaults to `gzip`.\n\n> [!NOTE]\n>\n> If you use a custom function for the `algorithm` option, the default value of `compressionOptions` will be an empty object `{}`.\n\n#### `string`\n\nThe algorithm is based on the Node.js [zlib](https://nodejs.org/api/zlib.html) module.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      algorithm: \"gzip\",\n    }),\n  ],\n};\n```\n\n#### `function`\n\nAllow you to specify a custom compression function.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      algorithm(input, compressionOptions, callback) {\n        return compressionFunction(input, compressionOptions, callback);\n      },\n    }),\n  ],\n};\n```\n\n### `compressionOptions`\n\nType:\n\n```ts\ninterface compressionOptions {\n  flush?: number;\n  finishFlush?: number;\n  chunkSize?: number;\n  windowBits?: number;\n  level?: number;\n  memLevel?: number;\n  strategy?: number;\n  dictionary?: Buffer | TypedArray | DataView | ArrayBuffer;\n  info?: boolean;\n  maxOutputLength?: number;\n}\n```\n\nDefault: `{ level: 9 }`\n\nCompression options for `algorithm`.\n\nYou can find all available options in the [zlib](https://nodejs.org/api/zlib.html#zlib_class_options) documentation.\n\n> [!NOTE]\n>\n> If you use a custom function for the `algorithm` option, the default value of `compressionOptions` will be an empty object `{}`.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      compressionOptions: { level: 1 },\n    }),\n  ],\n};\n```\n\n### `threshold`\n\nType:\n\n```ts\ntype threshold = number;\n```\n\nDefault: `0`\n\nOnly assets larger than this size (in bytes) are processed.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      threshold: 8192,\n    }),\n  ],\n};\n```\n\n### `minRatio`\n\nType:\n\n```ts\ntype minRatio = number;\n```\n\nDefault: `0.8`\n\nOnly assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`).\nFor example, if you have a `image.png` file with a size of 1024 bytes, and its compressed version is of 768 bytes, the `minRatio` is `0.75`.\nIn other words, assets will be processed only when the ratio of `Compressed Size / Original Size` is less than the specified `minRatio`.\n\nYou can use a value of `1` to process assets that are smaller than or equal to the original size.\n\nUse a value of `Infinity` to process all assets, even if they are larger than the original size or their original size is `0` bytes (useful when you are pre-zipping all assets for AWS).\n\nUse a value of `Number.MAX_SAFE_INTEGER` to process all assets even if they are larger than the original size, excluding assets with their original size is `0` bytes.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      // Compress all assets, including files with `0` bytes size\n      // minRatio: Infinity\n\n      // Compress all assets, excluding files with `0` bytes size\n      // minRatio: Number.MAX_SAFE_INTEGER\n\n      minRatio: 0.8,\n    }),\n  ],\n};\n```\n\n### `filename`\n\nType:\n\n```ts\ntype filename = string | ((pathdata: PathData) => string);\n```\n\nDefault: `\"[path][base].gz\"`\n\nThe target asset filename.\n\n#### `string`\n\nFor example, given an asset path: `assets/images/image.png?foo=bar#hash`:\n\n`[path]` is replaced with the directories of the original asset, including the trailing `/` (`assets/images/`).\n\n`[file]` is replaced with the path of the original asset (`assets/images/image.png`).\n\n`[base]` is replaced with the base name (`[name]` + `[ext]`) of the original asset (`image.png`).\n\n`[name]` is replaced with the name of the original asset (`image`).\n\n`[ext]` is replaced with the extension of the original asset, including the `.` (`.png`).\n\n`[query]` is replaced with the query of the original asset, including the `?` (`?foo=bar`).\n\n`[fragment]` is replaced with the fragment (in the concept of URL it is called `hash`) of the original asset (`#hash`).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      filename: \"[path][base].gz\",\n    }),\n  ],\n};\n```\n\n#### `function`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      filename(pathData) {\n        // The `pathData` argument contains all placeholders - `path`/`name`/`ext`/etc\n        // Available properties described above, for the `String` notation\n        if (/\\.svg$/.test(pathData.filename)) {\n          return \"assets/svg/[path][base].gz\";\n        }\n\n        return \"assets/js/[path][base].gz\";\n      },\n    }),\n  ],\n};\n```\n\n### `deleteOriginalAssets`\n\nType:\n\n```ts\ntype deleteOriginalAssets =\n  | boolean\n  | \"keep-source-map\"\n  | ((name: string) => boolean);\n```\n\nDefault: `false`\n\nDetermines whether the original (uncompressed) assets should be deleted after compression.\n\n- If set to `true` , all original assets will be deleted.\n\n- If set to `\"keep-source-map\"`, all original assets except source maps (`.map` files) will be deleted.\n\n- If a function is provided, it will be called with each asset’s name and should return `true` to delete the asset or `false` to keep it.\n\nExample:\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      deleteOriginalAssets: (assetName) =>\n        // Delete all assets except images\n        !assetName.endsWith(\".png\") && !assetName.endsWith(\".jpg\"),\n    }),\n  ],\n};\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      deleteOriginalAssets: true,\n    }),\n  ],\n};\n```\n\nTo exclude sourcemaps from compression:\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      exclude: /.map$/,\n      deleteOriginalAssets: \"keep-source-map\",\n    }),\n  ],\n};\n```\n\nUsing a custom function:\n\n```js\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      exclude: /.map$/,\n      deleteOriginalAssets: (name) => {\n        if (/\\.js$/.test(name)) {\n          return false;\n        }\n\n        return true;\n      },\n    }),\n  ],\n};\n```\n\n## Examples\n\n### Using Zopfli\n\nPrepare compressed versions of assets using the `zopfli` library.\n\n> [!NOTE]\n>\n> `@gfx/zopfli` requires at least `Node.js` version `8`.\n\nTo begin, you'll need to install `@gfx/zopfli`:\n\n```console\n$ npm install @gfx/zopfli --save-dev\n```\n\n**webpack.config.js**\n\n```js\nconst zopfli = require(\"@gfx/zopfli\");\n\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      compressionOptions: {\n        numiterations: 15,\n      },\n      algorithm(input, compressionOptions, callback) {\n        return zopfli.gzip(input, compressionOptions, callback);\n      },\n    }),\n  ],\n};\n```\n\n### Using Brotli\n\n[Brotli](https://en.wikipedia.org/wiki/Brotli) is a compression algorithm originally developed by Google, and offers compression superior to gzip.\n\nNode.js v10.16.0 and later includes [native support](https://nodejs.org/api/zlib.html#zlib_zlib_createbrotlicompress_options) for Brotli compression in its `zlib` module.\n\nYou 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:\n\n**webpack.config.js**\n\n```js\nconst zlib = require(\"node:zlib\");\n\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      filename: \"[path][base].br\",\n      algorithm: \"brotliCompress\",\n      test: /\\.(js|css|html|svg)$/,\n      compressionOptions: {\n        params: {\n          [zlib.constants.BROTLI_PARAM_QUALITY]: 11,\n        },\n      },\n      threshold: 10240,\n      minRatio: 0.8,\n      deleteOriginalAssets: false,\n    }),\n  ],\n};\n```\n\n[!NOTE] Brotli’s `BROTLI_PARAM_QUALITY` option is functionally equivalent to zlib’s `level` option.\nYou can find all Brotli’s options in [the relevant part of the zlib module documentation](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions).\n\n### Using Zstandard\n\n[Zstandard](https://facebook.github.io/zstd/) (zstd) is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios.\n\nNode.js 22.15.0 and later includes [native support](https://nodejs.org/api/zlib.html#zlibcreatezstdcompressoptions) for Zstandard compression in its `zlib` module.\n\nYou can take advantage of this built-in support for zstd in Node 22.15.0 and later by just passing in the appropriate `algorithm` to the CompressionPlugin:\n\n**webpack.config.js**\n\n```js\nconst zlib = require(\"node:zlib\");\n\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      filename: \"[path][base].zst\",\n      algorithm: \"zstdCompress\",\n      test: /\\.(js|css|html|svg)$/,\n      compressionOptions: {\n        params: {\n          [zlib.constants.ZSTD_c_compressionLevel]: 10,\n        },\n      },\n      threshold: 10240,\n      minRatio: 0.8,\n      deleteOriginalAssets: false,\n    }),\n  ],\n};\n```\n\nYou can find all Zstandard's options in [the relevant part of the zlib module documentation](https://nodejs.org/api/zlib.html#class-zstdoptions).\n\n### Multiple compressed versions of assets for different algorithm\n\n**webpack.config.js**\n\n```js\nconst zlib = require(\"node:zlib\");\n\nmodule.exports = {\n  plugins: [\n    new CompressionPlugin({\n      filename: \"[path][base].gz\",\n      algorithm: \"gzip\",\n      test: /\\.js$|\\.css$|\\.html$/,\n      threshold: 10240,\n      minRatio: 0.8,\n    }),\n    new CompressionPlugin({\n      filename: \"[path][base].br\",\n      algorithm: \"brotliCompress\",\n      test: /\\.(js|css|html|svg)$/,\n      compressionOptions: {\n        params: {\n          [zlib.constants.BROTLI_PARAM_QUALITY]: 11,\n        },\n      },\n      threshold: 10240,\n      minRatio: 0.8,\n    }),\n  ],\n};\n```\n\n## Contributing\n\nWe welcome contributions!\n\nPlease take a moment to read our contributing guidelines if you haven't yet done so.\n\n[CONTRIBUTING](https://github.com/webpack/compression-webpack-plugin?tab=contributing-ov-file#contributing)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/compression-webpack-plugin.svg\n[npm-url]: https://npmjs.com/package/compression-webpack-plugin\n[node]: https://img.shields.io/node/v/compression-webpack-plugin.svg\n[node-url]: https://nodejs.org\n[tests]: https://github.com/webpack/compression-webpack-plugin/workflows/compression-webpack-plugin/badge.svg\n[tests-url]: https://github.com/webpack/compression-webpack-plugin/actions\n[cover]: https://codecov.io/gh/webpack/compression-webpack-plugin/branch/main/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack/compression-webpack-plugin\n[discussion]: https://img.shields.io/github/discussions/webpack/webpack\n[discussion-url]: https://github.com/webpack/webpack/discussions\n[size]: https://packagephobia.now.sh/badge?p=compression-webpack-plugin\n[size-url]: https://packagephobia.now.sh/result?p=compression-webpack-plugin\n","publish_time":1772474103416,"_cnpm_publish_time":1772474103416,"dist-tags":{"beta":"1.0.0-beta.1","latest":"12.0.0","version-6":"6.1.2"}}