Skip to content

Commit

Permalink
feat: add include and exclude options (`options.include|options.e…
Browse files Browse the repository at this point in the history
…xclude`)
  • Loading branch information
evilebottnawi committed Dec 5, 2017
1 parent 252c5d9 commit 6269e9d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ module.exports = {

|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`test`**|`{RegExp}`|`.`|All assets matching this `{RegExp}` are processed|
|**`test`**|`{RegExp\|Array<RegExp>}`|`.`|All assets matching this `{RegExp\|Array<RegExp>}` are processed|
|**`include`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `include`|
|**`exclude`**|`{RegExp\|Array<RegExp>}`|`undefined`|Files to `exclude`|
|**`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|
|**`filename`**|`{Function}`|`false`|A `{Function}` `(asset) => asset` which receives the asset name (after processing `asset` option) and returns the new asset name|
|**`algorithm`**|`{String\|Function}`|`gzip`|Can be `(buffer, cb) => cb(buffer)` or if a {String}` is used the algorithm is taken from `zlib`|
|**`algorithm`**|`{String\|Function}`|`gzip`|Can be `(buffer, cb) => cb(buffer)` or if a `{String}` is used the algorithm is taken from `zlib`|
|**`threshold`**|`{Number}`|`0`|Only assets bigger than this size are processed. In bytes.|
|**`minRatio`**|`{Number}`|`0.8`|Only assets that compress better that this ratio are processed|
|**`deleteOriginalAssets`**|`{Boolean}`|`false`|Whether to delete the original assets or not|


### `test`

**webpack.config.js**
Expand All @@ -57,6 +58,28 @@ module.exports = {
]
```

### `include`

**webpack.config.js**
```js
[
new CompressionPlugin({
include: /\/includes/
})
]
```

### `exclude`

**webpack.config.js**
```js
[
new CompressionPlugin({
exclude: /\/excludes/
})
]
```

### `asset`

**webpack.config.js**
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Author Tobias Koppers @sokra
import url from 'url';
import async from 'async';
import RawSource from 'webpack-sources/lib/RawSource';
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';

class CompressionPlugin {
constructor(options = {}) {
Expand All @@ -14,6 +15,7 @@ class CompressionPlugin {
this.compressionOptions = {};

if (typeof this.algorithm === 'string') {
// eslint-disable-next-line global-require
const zlib = require('zlib');
this.algorithm = zlib[this.algorithm];

Expand All @@ -39,15 +41,13 @@ class CompressionPlugin {

apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
const assets = compilation.assets;
const { assets } = compilation;
// eslint-disable-next-line consistent-return
async.forEach(Object.keys(assets), (file, cb) => {
if (Array.isArray(this.test)) {
if (this.test.every(t => !t.test(file))) {
return cb();
}
} else if (this.test && !this.test.test(file)) {
if (!ModuleFilenameHelpers.matchObject(this, file)) {
return cb();
}

const asset = assets[file];
let content = asset.source();

Expand Down Expand Up @@ -83,6 +83,7 @@ class CompressionPlugin {
delete assets[file];
}
cb();
return null;
});
}, callback);
});
Expand Down

0 comments on commit 6269e9d

Please sign in to comment.