From fb7bd8186b7bb068c350528b3e3163579ef74623 Mon Sep 17 00:00:00 2001 From: Ronen Elster Date: Sat, 8 Apr 2017 04:57:19 +0300 Subject: [PATCH] feat: add option to change the filename (#51) --- README.md | 1 + index.js | 4 ++++ 2 files changed, 5 insertions(+) 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 ae51bc9..df284f8 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.filename = options.filename || 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.filename === 'function') { + newFile = this.filename(newFile); + } assets[newFile] = new RawSource(result); if (this.deleteOriginalAssets) { delete assets[file];