From 9711b28a7a6ff2e492fd8b8a42dd176687abd376 Mon Sep 17 00:00:00 2001 From: Ronen Elster Date: Wed, 5 Apr 2017 17:46:48 +0300 Subject: [PATCH 1/2] Add option to change new file name --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index ae51bc9..941069c 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ function CompressionPlugin(options) { options = options || {}; this.asset = options.asset || "[path].gz[query]"; this.algorithm = options.algorithm || "gzip"; + this.changeNewFileName = options.changeNewFileName || false; this.compressionOptions = {}; if(typeof this.algorithm === "string") { if (this.algorithm === "zopfli") { @@ -80,6 +81,9 @@ CompressionPlugin.prototype.apply = function(compiler) { var newFile = this.asset.replace(/\[(file|path|query)\]/g, function(p0,p1) { return sub[p1]; }); + if (typeof this.changeNewFileName === 'function') { + newFile = this.changeNewFileName(newFile); + } assets[newFile] = new RawSource(result); if (this.deleteOriginalAssets) { delete assets[file]; From 923f5fa336b0a009dd114a942b33331829581841 Mon Sep 17 00:00:00 2001 From: Ronen E Date: Fri, 7 Apr 2017 17:17:30 +0300 Subject: [PATCH 2/2] update docs and change option to `filename` --- README.md | 1 + index.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3127689..7c2a412 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ module.exports = { Arguments: * `asset`: 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. Defaults to `"[path].gz[query]"`. +* `filename`: A `function(asset)` which receives the asset name (after processing `asset` option) and returns the new asset name. Defaults to `false`. * `algorithm`: Can be a `function(buf, callback)` or a string. For a string the algorithm is taken from `zlib` (or zopfli for `zopfli`). Defaults to `"gzip"`. * `test`: All assets matching this RegExp are processed. Defaults to every asset. * `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`. diff --git a/index.js b/index.js index 941069c..df284f8 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ function CompressionPlugin(options) { options = options || {}; this.asset = options.asset || "[path].gz[query]"; this.algorithm = options.algorithm || "gzip"; - this.changeNewFileName = options.changeNewFileName || false; + this.filename = options.filename || false; this.compressionOptions = {}; if(typeof this.algorithm === "string") { if (this.algorithm === "zopfli") { @@ -81,8 +81,8 @@ CompressionPlugin.prototype.apply = function(compiler) { var newFile = this.asset.replace(/\[(file|path|query)\]/g, function(p0,p1) { return sub[p1]; }); - if (typeof this.changeNewFileName === 'function') { - newFile = this.changeNewFileName(newFile); + if (typeof this.filename === 'function') { + newFile = this.filename(newFile); } assets[newFile] = new RawSource(result); if (this.deleteOriginalAssets) {