Skip to content

Commit 437bdff

Browse files
sokrajoshwiens
authored andcommitted
fix: plugin options syntax
use [] instead of {} rename `regExp` to `test`
1 parent 9d05172 commit 437bdff

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ var CompressionPlugin = require("compression-webpack-plugin");
77
module.exports = {
88
plugins: [
99
new CompressionPlugin({
10-
asset: "{file}.gz",
10+
asset: "[path].gz[query]",
1111
algorithm: "gzip",
12-
regExp: /\.js$|\.html$/,
12+
test: /\.js$|\.html$/,
1313
threshold: 10240,
1414
minRatio: 0.8
1515
})
@@ -19,9 +19,9 @@ module.exports = {
1919

2020
Arguments:
2121

22-
* `asset`: The target asset name. `{file}` is replaced with the original asset. Defaults to `"{file}.gz"`.
22+
* `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]"`.
2323
* `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"`.
24-
* `regExp`: All assets matching this RegExp are processed. Defaults to every asset.
24+
* `test`: All assets matching this RegExp are processed. Defaults to every asset.
2525
* `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`.
2626
* `minRatio`: Only assets that compress better that this ratio are processed. Defaults to `0.8`.
2727

index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
var async = require("async");
66
var url = require('url');
77

8-
var RawSource = require("webpack/lib/RawSource");
8+
var RawSource = require("webpack-sources/lib/RawSource");
99

1010
function CompressionPlugin(options) {
1111
options = options || {};
12-
this.asset = options.asset || "{file}.gz";
12+
this.asset = options.asset || "[path].gz[query]";
1313
this.algorithm = options.algorithm || "gzip";
1414
if(typeof this.algorithm === "string") {
1515
if (this.algorithm === "zopfli") {
@@ -43,7 +43,7 @@ function CompressionPlugin(options) {
4343
});
4444
}
4545
}
46-
this.regExp = options.regExp;
46+
this.test = options.test || options.regExp;
4747
this.threshold = options.threshold || 0;
4848
this.minRatio = options.minRatio || 0.8;
4949
}
@@ -53,7 +53,12 @@ CompressionPlugin.prototype.apply = function(compiler) {
5353
compiler.plugin("this-compilation", function(compilation) {
5454
compilation.plugin("optimize-assets", function(assets, callback) {
5555
async.forEach(Object.keys(assets), function(file, callback) {
56-
if(this.regExp && !this.regExp.test(file)) return callback();
56+
if(Array.isArray(this.test)) {
57+
if(this.test.every(function(t) {
58+
return !t.test(file);
59+
})) return callback();
60+
} else if(this.test && !this.test.test(file))
61+
return callback();
5762
var asset = assets[file];
5863
var content = asset.source();
5964
if(!Buffer.isBuffer(content))
@@ -67,9 +72,9 @@ CompressionPlugin.prototype.apply = function(compiler) {
6772
var sub = {
6873
file: file,
6974
path: parse.pathname,
70-
query: parse.query
75+
query: parse.query || ""
7176
};
72-
var newFile = this.asset.replace(/\{(file|path|query)\}/g, function(p0,p1) {
77+
var newFile = this.asset.replace(/\[(file|path|query)\]/g, function(p0,p1) {
7378
return sub[p1];
7479
});
7580
assets[newFile] = new RawSource(result);

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
"version": "0.2.0",
44
"author": "Tobias Koppers @sokra",
55
"description": "Prepare compressed versions of assets to serve them with Content-Encoding",
6-
"peerDependencies": {
7-
"webpack": ">=0.11 <2"
8-
},
96
"dependencies": {
10-
"async": "0.2.x"
7+
"async": "0.2.x",
8+
"webpack-sources": "^0.1.0"
119
},
1210
"optionalDependencies": {
1311
"node-zopfli": "^1.3.4"

0 commit comments

Comments
 (0)