Skip to content

Commit 9c75ecc

Browse files
committed
Merge pull request #10 from rufman/master
Add zopfli option
2 parents e0fd30d + ec8897a commit 9c75ecc

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ module.exports = {
2020
Arguments:
2121

2222
* `asset`: The target asset name. `{file}` is replaced with the original asset. Defaults to `"{file}.gz"`.
23-
* `algorithm`: Can be a `function(buf, callback)` or a string. For a string the algorithm is tacken from `zlib`. Defaults to `"gzip"`.
23+
* `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"`.
2424
* `regExp`: 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

28+
Option Arguments for Zopfli (see [node-zopfli](https://github.com/pierreinglebert/node-zopfli#options) doc for details):
29+
* verbose: Default: false,
30+
* verbose_more: Default: false,
31+
* numiterations: Default: 15,
32+
* blocksplitting: Default: true,
33+
* blocksplittinglast: Default: false,
34+
* blocksplittingmax: Default: 15
35+
2836
## License
2937

3038
MIT (http://www.opensource.org/licenses/mit-license.php)

index.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,28 @@ function CompressionPlugin(options) {
1212
this.asset = options.asset || "{file}.gz";
1313
this.algorithm = options.algorithm || "gzip";
1414
if(typeof this.algorithm === "string") {
15-
var zlib = require("zlib");
16-
this.algorithm = zlib[this.algorithm];
17-
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
18-
this.algorithm = this.algorithm.bind(zlib);
15+
if (this.algorithm === "zopfli") {
16+
try {
17+
var zopfli = require("node-zopfli");
18+
} catch(err) {
19+
throw new Error("node-zopfli not found");
20+
}
21+
this.algorithm = function (content, fn) {
22+
zopfli.gzip(content, {
23+
verbose: options.hasOwnProperty('verbose') ? options.verbose : false,
24+
verbose_more: options.hasOwnProperty('verbose_more') ? options.verbose_more : false,
25+
numiterations: options.numiterations ? options.numiterations : 15,
26+
blocksplitting: options.hasOwnProperty('blocksplitting') ? options.blocksplitting : true,
27+
blocksplittinglast: options.hasOwnProperty('blocksplittinglast') ? options.blocksplittinglast : false,
28+
blocksplittingmax: options.blocksplittingmax ? options.blocksplittingmax : 15
29+
}, fn);
30+
};
31+
} else {
32+
var zlib = require("zlib");
33+
this.algorithm = zlib[this.algorithm];
34+
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
35+
this.algorithm = this.algorithm.bind(zlib);
36+
}
1937
}
2038
this.regExp = options.regExp;
2139
this.threshold = options.threshold || 0;

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"dependencies": {
1010
"async": "0.2.x"
1111
},
12+
"optionalDependencies": {
13+
"node-zopfli": "^1.3.4"
14+
},
1215
"homepage": "http://github.com/webpack/compression-webpack-plugin",
1316
"repository": {
1417
"type": "git",

0 commit comments

Comments
 (0)