Skip to content

Commit 483f328

Browse files
authored
fix: always set compression level to maximum
1 parent 6aa8e38 commit 483f328

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ And run `webpack` via your preferred method.
4040

4141
## Options
4242

43-
| Name | Type | Default | Description |
44-
| :-------------------------------------------------: | :---------------------------------------: | :---------------: | :------------------------------------------------------------------------------------------------------------ |
45-
| **[`test`](#test)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Include all assets that pass test assertion |
46-
| **[`include`](#include)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Include all assets matching any of these conditions |
47-
| **[`exclude`](#exclude)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Exclude all assets matching any of these conditions |
48-
| **[`algorithm`](#algorithm)** | `{String\|Function}` | `gzip` | The compression algorithm/function |
49-
| **[`compressionOptions`](#compressionoptions)** | `{Object}` | `{ level: 9 }` | Compression options for `algorithm` |
50-
| **[`threshold`](#threshold)** | `{Number}` | `0` | Only assets bigger than this size are processed (in bytes) |
51-
| **[`minRatio`](#minratio)** | `{Number}` | `0.8` | Only assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`) |
52-
| **[`filename`](#filename)** | `{String\|Function}` | `[path][base].gz` | The target asset filename |
53-
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean}` | `false` | Whether to delete the original assets or not |
54-
| **[`cache`](#cache)** | `{Boolean}` | `true` | Enable file caching |
43+
| Name | Type | Default | Description |
44+
| :-------------------------------------------------: | :---------------------------------------: | :-----------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------ |
45+
| **[`test`](#test)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Include all assets that pass test assertion |
46+
| **[`include`](#include)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Include all assets matching any of these conditions |
47+
| **[`exclude`](#exclude)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Exclude all assets matching any of these conditions |
48+
| **[`algorithm`](#algorithm)** | `{String\|Function}` | `gzip` | The compression algorithm/function |
49+
| **[`compressionOptions`](#compressionoptions)** | `{Object}` | maximum available compression level, for gzip: `{ level: 9 }` | Compression options for `algorithm` |
50+
| **[`threshold`](#threshold)** | `{Number}` | `0` | Only assets bigger than this size are processed (in bytes) |
51+
| **[`minRatio`](#minratio)** | `{Number}` | `0.8` | Only assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`) |
52+
| **[`filename`](#filename)** | `{String\|Function}` | `[path][base].gz` | The target asset filename |
53+
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean}` | `false` | Whether to delete the original assets or not |
54+
| **[`cache`](#cache)** | `{Boolean}` | `true` | Enable file caching |
5555

5656
### `test`
5757

@@ -401,8 +401,9 @@ module.exports = {
401401
algorithm: 'brotliCompress',
402402
test: /\.(js|css|html|svg)$/,
403403
compressionOptions: {
404-
// zlib’s `level` option matches Brotli’s `BROTLI_PARAM_QUALITY` option.
405-
level: 11,
404+
params: {
405+
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
406+
},
406407
},
407408
threshold: 10240,
408409
minRatio: 0.8,
@@ -412,7 +413,8 @@ module.exports = {
412413
};
413414
```
414415

415-
**Note** The `level` option matches `BROTLI_PARAM_QUALITY` [for Brotli-based streams](https://nodejs.org/api/zlib.html#zlib_for_brotli_based_streams)
416+
**Note** Brotli’s `BROTLI_PARAM_QUALITY` option is functionally equivalent to zlib’s `level` option.
417+
You can find all Brotli’s options in [the relevant part of the zlib module documentation](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions).
416418

417419
### Multiple compressed versions of assets for different algorithm
418420

@@ -435,7 +437,9 @@ module.exports = {
435437
algorithm: 'brotliCompress',
436438
test: /\.(js|css|html|svg)$/,
437439
compressionOptions: {
438-
level: 11,
440+
params: {
441+
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
442+
},
439443
},
440444
threshold: 10240,
441445
minRatio: 0.8,

src/index.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,27 @@ class CompressionPlugin {
6666
);
6767
}
6868

69+
const defaultCompressionOptions =
70+
{
71+
gzip: {
72+
level: zlib.constants.Z_BEST_COMPRESSION,
73+
},
74+
deflate: {
75+
level: zlib.constants.Z_BEST_COMPRESSION,
76+
},
77+
deflateRaw: {
78+
level: zlib.constants.Z_BEST_COMPRESSION,
79+
},
80+
brotliCompress: {
81+
params: {
82+
[zlib.constants.BROTLI_PARAM_QUALITY]:
83+
zlib.constants.BROTLI_MAX_QUALITY,
84+
},
85+
},
86+
}[algorithm] || {};
87+
6988
this.options.compressionOptions = {
70-
...{ level: 9 },
89+
...defaultCompressionOptions,
7190
...this.options.compressionOptions,
7291
};
7392
}

test/compressionOptions-option.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,44 @@ describe('"compressionOptions" option', () => {
4444
expect(getWarnings(stats)).toMatchSnapshot('warnings');
4545
expect(getErrors(stats)).toMatchSnapshot('errors');
4646
});
47+
48+
it('set default compression level to maximum for gzip', async () => {
49+
const compressionPlugin = new CompressionPlugin({
50+
algorithm: 'gzip',
51+
});
52+
53+
expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
54+
level: 9,
55+
});
56+
});
57+
58+
it('set default compression level to maximum for deflate', async () => {
59+
const compressionPlugin = new CompressionPlugin({
60+
algorithm: 'deflate',
61+
});
62+
63+
expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
64+
level: 9,
65+
});
66+
});
67+
68+
it('set default compression level to maximum for deflateRaw', async () => {
69+
const compressionPlugin = new CompressionPlugin({
70+
algorithm: 'deflateRaw',
71+
});
72+
73+
expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
74+
level: 9,
75+
});
76+
});
77+
78+
it('set default compression level to maximum for brotli', async () => {
79+
const compressionPlugin = new CompressionPlugin({
80+
algorithm: 'brotliCompress',
81+
});
82+
83+
expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
84+
params: { 1: 11 },
85+
});
86+
});
4787
});

0 commit comments

Comments
 (0)