Skip to content

Commit

Permalink
feat: exclude and include options
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 4, 2017
1 parent 252c5d9 commit acff2b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 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
14 changes: 7 additions & 7 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 @@ -82,7 +82,7 @@ class CompressionPlugin {
if (this.deleteOriginalAssets) {
delete assets[file];
}
cb();
return cb();
});
}, callback);
});
Expand Down

0 comments on commit acff2b7

Please sign in to comment.