Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
fix: always set compression level to maximum, also for Brotli (webpac…
Browse files Browse the repository at this point in the history
…k-contrib#154)

Signed-off-by: Adrian Pedziwiatr <180254@users.noreply.github.com>
  • Loading branch information
180254 committed Oct 24, 2020
1 parent a4e48ab commit 436ec3f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
24 changes: 12 additions & 12 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
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 436ec3f

Please sign in to comment.