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`) (#82)
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Dec 7, 2017
1 parent 1226605 commit 1ce3024
Show file tree
Hide file tree
Showing 2 changed files with 37 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
17 changes: 11 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 Down Expand Up @@ -35,6 +36,7 @@ class CompressionPlugin {
};

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

Expand All @@ -56,15 +58,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.options.test)) {
if (this.options.test.every(t => !t.test(file))) {
return cb();
}
} else if (this.options.test && !this.options.test.test(file)) {
if (!ModuleFilenameHelpers.matchObject(this, file)) {
return cb();
}

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

Expand Down Expand Up @@ -95,11 +95,16 @@ class CompressionPlugin {
if (typeof this.options.filename === 'function') {
newFile = this.options.filename(newFile);
}

assets[newFile] = new RawSource(result);

if (this.options.deleteOriginalAssets) {
delete assets[file];
}

cb();

return null;
});
}, callback);
});
Expand Down

0 comments on commit 1ce3024

Please sign in to comment.