Skip to content

Commit 9555370

Browse files
ronen-emichael-ciniawsky
authored andcommitted
feat: add option to change the filename (#51)
1 parent 7489a78 commit 9555370

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
Arguments:
3939

4040
* `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]"`.
41+
* `filename`: A `function(asset)` which receives the asset name (after processing `asset` option) and returns the new asset name. Defaults to `false`.
4142
* `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"`.
4243
* `test`: All assets matching this RegExp are processed. Defaults to every asset.
4344
* `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function CompressionPlugin(options) {
1111
options = options || {};
1212
this.asset = options.asset || "[path].gz[query]";
1313
this.algorithm = options.algorithm || "gzip";
14+
this.filename = options.filename || false;
1415
this.compressionOptions = {};
1516
if(typeof this.algorithm === "string") {
1617
if (this.algorithm === "zopfli") {
@@ -80,6 +81,9 @@ CompressionPlugin.prototype.apply = function(compiler) {
8081
var newFile = this.asset.replace(/\[(file|path|query)\]/g, function(p0,p1) {
8182
return sub[p1];
8283
});
84+
if (typeof this.filename === 'function') {
85+
newFile = this.filename(newFile);
86+
}
8387
assets[newFile] = new RawSource(result);
8488
if (this.deleteOriginalAssets) {
8589
delete assets[file];

0 commit comments

Comments
 (0)