Skip to content

Commit

Permalink
fix: always set compression level to maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
180254 authored Oct 26, 2020
1 parent 6aa8e38 commit 483f328
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 17 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ And run `webpack` via your preferred method.

## Options

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

### `test`

Expand Down Expand Up @@ -401,8 +401,9 @@ module.exports = {
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
compressionOptions: {
// zlib’s `level` option matches Brotli’s `BROTLI_PARAM_QUALITY` option.
level: 11,
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
threshold: 10240,
minRatio: 0.8,
Expand All @@ -412,7 +413,8 @@ module.exports = {
};
```

**Note** The `level` option matches `BROTLI_PARAM_QUALITY` [for Brotli-based streams](https://nodejs.org/api/zlib.html#zlib_for_brotli_based_streams)
**Note** Brotli’s `BROTLI_PARAM_QUALITY` option is functionally equivalent to zlib’s `level` option.
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).

### Multiple compressed versions of assets for different algorithm

Expand All @@ -435,7 +437,9 @@ module.exports = {
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
compressionOptions: {
level: 11,
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
threshold: 10240,
minRatio: 0.8,
Expand Down
21 changes: 20 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,27 @@ class CompressionPlugin {
);
}

const defaultCompressionOptions =
{
gzip: {
level: zlib.constants.Z_BEST_COMPRESSION,
},
deflate: {
level: zlib.constants.Z_BEST_COMPRESSION,
},
deflateRaw: {
level: zlib.constants.Z_BEST_COMPRESSION,
},
brotliCompress: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]:
zlib.constants.BROTLI_MAX_QUALITY,
},
},
}[algorithm] || {};

this.options.compressionOptions = {
...{ level: 9 },
...defaultCompressionOptions,
...this.options.compressionOptions,
};
}
Expand Down
40 changes: 40 additions & 0 deletions test/compressionOptions-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,44 @@ describe('"compressionOptions" option', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('set default compression level to maximum for gzip', async () => {
const compressionPlugin = new CompressionPlugin({
algorithm: 'gzip',
});

expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
level: 9,
});
});

it('set default compression level to maximum for deflate', async () => {
const compressionPlugin = new CompressionPlugin({
algorithm: 'deflate',
});

expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
level: 9,
});
});

it('set default compression level to maximum for deflateRaw', async () => {
const compressionPlugin = new CompressionPlugin({
algorithm: 'deflateRaw',
});

expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
level: 9,
});
});

it('set default compression level to maximum for brotli', async () => {
const compressionPlugin = new CompressionPlugin({
algorithm: 'brotliCompress',
});

expect(compressionPlugin).toHaveProperty('options.compressionOptions', {
params: { 1: 11 },
});
});
});

0 comments on commit 483f328

Please sign in to comment.