Skip to content

Commit

Permalink
feat: add option to change the filename (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronen-e authored and michael-ciniawsky committed Apr 8, 2017
1 parent 7489a78 commit 9555370
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 9555370

Please sign in to comment.