Skip to content

Commit d07d854

Browse files
refactor: improve validation for options and docs
1 parent 778e866 commit d07d854

File tree

4 files changed

+263
-198
lines changed

4 files changed

+263
-198
lines changed

README.md

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,24 @@ 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].gz[query]` | The target asset filename. |
53+
| **[`cache`](#cache)** | `{Boolean}` | `true` | Enable file caching |
54+
4355
### `test`
4456

4557
Type: `String|RegExp|Array<String|RegExp>`
4658
Default: `undefined`
4759

48-
Test to match files against.
60+
Include all assets that pass test assertion.
4961

5062
**webpack.config.js**
5163

@@ -64,7 +76,7 @@ module.exports = {
6476
Type: `String|RegExp|Array<String|RegExp>`
6577
Default: `undefined`
6678

67-
Files to include.
79+
Include all assets matching any of these conditions.
6880

6981
**webpack.config.js**
7082

@@ -83,7 +95,7 @@ module.exports = {
8395
Type: `String|RegExp|Array<String|RegExp>`
8496
Default: `undefined`
8597

86-
Files to exclude.
98+
Exclude all assets matching any of these conditions.
8799

88100
**webpack.config.js**
89101

@@ -97,53 +109,6 @@ module.exports = {
97109
};
98110
```
99111

100-
### `filename`
101-
102-
Type: `String|Function`
103-
Default: `[path].gz[query]`
104-
105-
The target asset filename.
106-
107-
#### `String`
108-
109-
`[file]` is replaced with the original asset filename.
110-
`[path]` is replaced with the path of the original asset.
111-
`[dir]` is replaced with the directory of the original asset.
112-
`[name]` is replaced with the filename of the original asset.
113-
`[ext]` is replaced with the extension of the original asset.
114-
`[query]` is replaced with the query.
115-
116-
**webpack.config.js**
117-
118-
```js
119-
module.exports = {
120-
plugins: [
121-
new CompressionPlugin({
122-
filename: '[path].gz[query]',
123-
}),
124-
],
125-
};
126-
```
127-
128-
#### `Function`
129-
130-
**webpack.config.js**
131-
132-
```js
133-
module.exports = {
134-
plugins: [
135-
new CompressionPlugin({
136-
filename(info) {
137-
// info.file is the original asset filename
138-
// info.path is the path of the original asset
139-
// info.query is the query
140-
return `${info.path}.gz${info.query}`;
141-
},
142-
}),
143-
],
144-
};
145-
```
146-
147112
### `algorithm`
148113

149114
Type: `String|Function`
@@ -190,9 +155,10 @@ module.exports = {
190155
Type: `Object`
191156
Default: `{ level: 9 }`
192157

158+
Compression options for `algorithm`.
159+
193160
If you use custom function for the `algorithm` option, the default value is `{}`.
194161

195-
Compression options.
196162
You can find all options here [zlib](https://nodejs.org/api/zlib.html#zlib_class_options).
197163

198164
**webpack.config.js**
@@ -248,6 +214,53 @@ module.exports = {
248214
};
249215
```
250216

217+
### `filename`
218+
219+
Type: `String|Function`
220+
Default: `[path].gz[query]`
221+
222+
The target asset filename.
223+
224+
#### `String`
225+
226+
`[file]` is replaced with the original asset filename.
227+
`[path]` is replaced with the path of the original asset.
228+
`[dir]` is replaced with the directory of the original asset.
229+
`[name]` is replaced with the filename of the original asset.
230+
`[ext]` is replaced with the extension of the original asset.
231+
`[query]` is replaced with the query.
232+
233+
**webpack.config.js**
234+
235+
```js
236+
module.exports = {
237+
plugins: [
238+
new CompressionPlugin({
239+
filename: '[path].gz[query]',
240+
}),
241+
],
242+
};
243+
```
244+
245+
#### `Function`
246+
247+
**webpack.config.js**
248+
249+
```js
250+
module.exports = {
251+
plugins: [
252+
new CompressionPlugin({
253+
filename(info) {
254+
// info.file is the original asset filename
255+
// info.path is the path of the original asset
256+
// info.query is the query
257+
return `${info.path}.gz${info.query}`;
258+
},
259+
}),
260+
],
261+
};
262+
```
263+
251264
### `deleteOriginalAssets`
252265

253266
Type: `Boolean`
@@ -272,7 +285,7 @@ module.exports = {
272285
> ⚠ Ignored in webpack 5! Please use https://webpack.js.org/configuration/other-options/#cache.
273286
274287
Type: `Boolean|String`
275-
Default: `false`
288+
Default: `true`
276289

277290
Enable file caching.
278291
The default path to cache directory: `node_modules/.cache/compression-webpack-plugin`.
@@ -348,7 +361,7 @@ module.exports = {
348361

349362
Node 10.16.0 and later has [native support](https://nodejs.org/api/zlib.html#zlib_zlib_createbrotlicompress_options) for Brotli compression in its zlib module.
350363

351-
We can take advantage of this built-in support for Brotli in Node 11.7.0 and later by just passing in the appropriate `algorithm` to the CompressionPlugin:
364+
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:
352365

353366
**webpack.config.js**
354367

src/options.json

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,66 @@
11
{
22
"additionalProperties": false,
33
"definitions": {
4-
"file-conditions": {
4+
"Rule": {
5+
"description": "Filtering rule as regex or string.",
56
"anyOf": [
67
{
7-
"instanceof": "RegExp"
8+
"instanceof": "RegExp",
9+
"tsType": "RegExp"
810
},
911
{
10-
"type": "string"
12+
"type": "string",
13+
"minLength": 1
1114
}
1215
]
13-
}
14-
},
15-
"properties": {
16-
"test": {
16+
},
17+
"Rules": {
18+
"description": "Filtering rules.",
1719
"anyOf": [
1820
{
19-
"$ref": "#/definitions/file-conditions"
20-
},
21-
{
21+
"type": "array",
2222
"items": {
23-
"anyOf": [
23+
"description": "A rule condition.",
24+
"oneOf": [
2425
{
25-
"$ref": "#/definitions/file-conditions"
26+
"$ref": "#/definitions/Rule"
2627
}
2728
]
28-
},
29-
"type": "array"
30-
}
31-
]
32-
},
33-
"include": {
34-
"anyOf": [
35-
{
36-
"$ref": "#/definitions/file-conditions"
29+
}
3730
},
3831
{
39-
"items": {
40-
"anyOf": [
41-
{
42-
"$ref": "#/definitions/file-conditions"
43-
}
44-
]
45-
},
46-
"type": "array"
32+
"$ref": "#/definitions/Rule"
4733
}
4834
]
49-
},
50-
"exclude": {
51-
"anyOf": [
52-
{
53-
"$ref": "#/definitions/file-conditions"
54-
},
35+
}
36+
},
37+
"properties": {
38+
"test": {
39+
"description": "Include all assets that pass test assertion.",
40+
"oneOf": [
5541
{
56-
"items": {
57-
"anyOf": [
58-
{
59-
"$ref": "#/definitions/file-conditions"
60-
}
61-
]
62-
},
63-
"type": "array"
42+
"$ref": "#/definitions/Rules"
6443
}
6544
]
6645
},
67-
"cache": {
68-
"anyOf": [
69-
{
70-
"type": "boolean"
71-
},
46+
"include": {
47+
"description": "Include all assets matching any of these conditions.",
48+
"oneOf": [
7249
{
73-
"type": "string"
50+
"$ref": "#/definitions/Rules"
7451
}
7552
]
7653
},
77-
"filename": {
78-
"anyOf": [
79-
{
80-
"type": "string"
81-
},
54+
"exclude": {
55+
"description": "Exclude all assets matching any of these conditions.",
56+
"oneOf": [
8257
{
83-
"instanceof": "Function"
58+
"$ref": "#/definitions/Rules"
8459
}
8560
]
8661
},
8762
"algorithm": {
63+
"description": "The compression algorithm/function.",
8864
"anyOf": [
8965
{
9066
"type": "string"
@@ -95,17 +71,43 @@
9571
]
9672
},
9773
"compressionOptions": {
74+
"description": "Compression options for `algorithm`.",
9875
"additionalProperties": true,
9976
"type": "object"
10077
},
10178
"threshold": {
79+
"description": "Only assets bigger than this size are processed. In bytes.",
10280
"type": "number"
10381
},
10482
"minRatio": {
83+
"description": "Only assets that compress better than this ratio are processed (`minRatio = Compressed Size / Original Size`).",
10584
"type": "number"
10685
},
10786
"deleteOriginalAssets": {
87+
"description": "Whether to delete the original assets or not.",
10888
"type": "boolean"
89+
},
90+
"filename": {
91+
"description": "The target asset filename.",
92+
"anyOf": [
93+
{
94+
"type": "string"
95+
},
96+
{
97+
"instanceof": "Function"
98+
}
99+
]
100+
},
101+
"cache": {
102+
"description": "Enable file caching. Ignored in webpack 5, for webpack 5 please use https://webpack.js.org/configuration/other-options/#cache.",
103+
"anyOf": [
104+
{
105+
"type": "boolean"
106+
},
107+
{
108+
"type": "string"
109+
}
110+
]
109111
}
110112
},
111113
"type": "object"

0 commit comments

Comments
 (0)