Skip to content

Commit 1ce3024

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
feat: add include and exclude options (options.include|options.exclude) (#82)
1 parent 1226605 commit 1ce3024

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ module.exports = {
3737

3838
|Name|Type|Default|Description|
3939
|:--:|:--:|:-----:|:----------|
40-
|**`test`**|`{RegExp}`|`.`|All assets matching this `{RegExp}` are processed|
40+
|**`test`**|`{RegExp\|Array<RegExp>}`|`.`|All assets matching this `{RegExp\|Array<RegExp>}` are processed|
41+
|**`include`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `include`|
42+
|**`exclude`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `exclude`|
4143
|**`asset`**|`{String}`|`[path].gz[query]`|The target asset name. `[file]` is replaced with the original asset. `[path]` is replaced with the path of the original asset and `[query]` with the query|
4244
|**`filename`**|`{Function}`|`false`|A `{Function}` `(asset) => asset` which receives the asset name (after processing `asset` option) and returns the new asset name|
43-
|**`algorithm`**|`{String\|Function}`|`gzip`|Can be `(buffer, cb) => cb(buffer)` or if a {String}` is used the algorithm is taken from `zlib`|
45+
|**`algorithm`**|`{String\|Function}`|`gzip`|Can be `(buffer, cb) => cb(buffer)` or if a `{String}` is used the algorithm is taken from `zlib`|
4446
|**`threshold`**|`{Number}`|`0`|Only assets bigger than this size are processed. In bytes.|
4547
|**`minRatio`**|`{Number}`|`0.8`|Only assets that compress better that this ratio are processed|
4648
|**`deleteOriginalAssets`**|`{Boolean}`|`false`|Whether to delete the original assets or not|
4749

48-
4950
### `test`
5051

5152
**webpack.config.js**
@@ -57,6 +58,28 @@ module.exports = {
5758
]
5859
```
5960

61+
### `include`
62+
63+
**webpack.config.js**
64+
```js
65+
[
66+
new CompressionPlugin({
67+
include: /\/includes/
68+
})
69+
]
70+
```
71+
72+
### `exclude`
73+
74+
**webpack.config.js**
75+
```js
76+
[
77+
new CompressionPlugin({
78+
exclude: /\/excludes/
79+
})
80+
]
81+
```
82+
6083
### `asset`
6184

6285
**webpack.config.js**

src/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Author Tobias Koppers @sokra
55
import url from 'url';
66
import async from 'async';
77
import RawSource from 'webpack-sources/lib/RawSource';
8+
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
89

910
class CompressionPlugin {
1011
constructor(options = {}) {
@@ -35,6 +36,7 @@ class CompressionPlugin {
3536
};
3637

3738
if (typeof algorithm === 'string') {
39+
// eslint-disable-next-line global-require
3840
const zlib = require('zlib');
3941
this.options.algorithm = zlib[this.options.algorithm];
4042

@@ -56,15 +58,13 @@ class CompressionPlugin {
5658

5759
apply(compiler) {
5860
compiler.plugin('emit', (compilation, callback) => {
59-
const assets = compilation.assets;
61+
const { assets } = compilation;
62+
// eslint-disable-next-line consistent-return
6063
async.forEach(Object.keys(assets), (file, cb) => {
61-
if (Array.isArray(this.options.test)) {
62-
if (this.options.test.every(t => !t.test(file))) {
63-
return cb();
64-
}
65-
} else if (this.options.test && !this.options.test.test(file)) {
64+
if (!ModuleFilenameHelpers.matchObject(this, file)) {
6665
return cb();
6766
}
67+
6868
const asset = assets[file];
6969
let content = asset.source();
7070

@@ -95,11 +95,16 @@ class CompressionPlugin {
9595
if (typeof this.options.filename === 'function') {
9696
newFile = this.options.filename(newFile);
9797
}
98+
9899
assets[newFile] = new RawSource(result);
100+
99101
if (this.options.deleteOriginalAssets) {
100102
delete assets[file];
101103
}
104+
102105
cb();
106+
107+
return null;
103108
});
104109
}, callback);
105110
});

0 commit comments

Comments
 (0)