Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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]"`.
* `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"`.
* `algorithm`: Can be a `function(buf, options, callback)` or a string. For a string the algorithm is taken from `zlib` (or zopfli for `zopfli`). Defaults to `"gzip"`. Options contains any options passed to the constructor which aren't listed here.
* `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`.
* `minRatio`: Only assets that compress better that this ratio are processed. Defaults to `0.8`.
Expand Down
37 changes: 21 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ var url = require('url');

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

var NON_COMPRESSION_OPTIONS = ['asset', 'algorithm', 'test', 'threshold', 'minRatio'];

function CompressionPlugin(options) {
options = options || {};
this.asset = options.asset || "[path].gz[query]";
this.algorithm = options.algorithm || "gzip";
this.compressionOptions = {};
this.compressionOptions = Object.keys(options).reduce(function(o, k) {
if(NON_COMPRESSION_OPTIONS.indexOf(k) < 0) o[k] = options[k];
return o;
}, {});
if(typeof this.algorithm === "string") {
var defaultCompressionOptions;
if (this.algorithm === "zopfli") {
try {
var zopfli = require("node-zopfli");
} catch(err) {
throw new Error("node-zopfli not found");
}
this.compressionOptions = {
verbose: options.hasOwnProperty('verbose') ? options.verbose : false,
verbose_more: options.hasOwnProperty('verbose_more') ? options.verbose_more : false,
numiterations: options.numiterations ? options.numiterations : 15,
blocksplitting: options.hasOwnProperty('blocksplitting') ? options.blocksplitting : true,
blocksplittinglast: options.hasOwnProperty('blocksplittinglast') ? options.blocksplittinglast : false,
blocksplittingmax: options.blocksplittingmax ? options.blocksplittingmax : 15
defaultCompressionOptions = {
verbose: false,
verbose_more: false,
numiterations: 15,
blocksplitting: true,
blocksplittinglast: false,
blocksplittingmax: 15
};
this.algorithm = function (content, options, fn) {
zopfli.gzip(content, options, fn);
Expand All @@ -34,16 +40,15 @@ function CompressionPlugin(options) {
var zlib = require("zlib");
this.algorithm = zlib[this.algorithm];
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
this.compressionOptions = {
level: options.level || 9,
flush: options.flush,
chunkSize: options.chunkSize,
windowBits: options.windowBits,
memLevel: options.memLevel,
strategy: options.strategy,
dictionary: options.dictionary
defaultCompressionOptions = {
level: 9
};
}
for(var k in defaultCompressionOptions) {
if(!this.compressionOptions.hasOwnProperty(k)) {
this.compressionOptions[k] = defaultCompressionOptions[k];
}
}
}
this.test = options.test || options.regExp;
this.threshold = options.threshold || 0;
Expand Down