From f9feda1fd167eb358cf451af749cabbcb886dcb6 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 4 Dec 2017 16:24:36 +0300 Subject: [PATCH] feat: add `include` and `exclude` options (`options.include|options.exclude`) --- README.md | 29 ++++++++++++++++++++++++++--- src/index.js | 17 +++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 12710d9..25664d4 100644 --- a/README.md +++ b/README.md @@ -37,15 +37,16 @@ module.exports = { |Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| -|**`test`**|`{RegExp}`|`.`|All assets matching this `{RegExp}` are processed| +|**`test`**|`{RegExp\|Array}`|`.`|All assets matching this `{RegExp\|Array}` are processed| +|**`include`**|`{RegExp\|Array}`|`undefined`|Files to `include`| +|**`exclude`**|`{RegExp\|Array}`|`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** @@ -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** diff --git a/src/index.js b/src/index.js index 4790896..c9d639a 100644 --- a/src/index.js +++ b/src/index.js @@ -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 = {}) { @@ -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]; @@ -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(); @@ -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); });