diff --git a/index.js b/index.js index b2b267f..79a918a 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ function CompressionPlugin(options) { options = options || {}; this.asset = options.asset || "[path].gz[query]"; this.algorithm = options.algorithm || "gzip"; + this.compressionOptions = {}; if(typeof this.algorithm === "string") { if (this.algorithm === "zopfli") { try { @@ -18,21 +19,22 @@ function CompressionPlugin(options) { } catch(err) { throw new Error("node-zopfli not found"); } - this.algorithm = function (content, fn) { - zopfli.gzip(content, { - 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 - }, fn); + 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 + }; + this.algorithm = function (content, options, fn) { + zopfli.gzip(content, options, fn); }; } else { var zlib = require("zlib"); this.algorithm = zlib[this.algorithm]; if(!this.algorithm) throw new Error("Algorithm not found in zlib"); - this.algorithm = this.algorithm.bind(zlib, { + this.compressionOptions = { level: options.level || 9, flush: options.flush, chunkSize: options.chunkSize, @@ -40,7 +42,7 @@ function CompressionPlugin(options) { memLevel: options.memLevel, strategy: options.strategy, dictionary: options.dictionary - }); + }; } } this.test = options.test || options.regExp; @@ -65,7 +67,7 @@ CompressionPlugin.prototype.apply = function(compiler) { content = new Buffer(content, "utf-8"); var originalSize = content.length; if(originalSize < this.threshold) return callback(); - this.algorithm(content, function(err, result) { + this.algorithm(content, this.compressionOptions, function(err, result) { if(err) return callback(err); if(result.length / originalSize > this.minRatio) return callback(); var parse = url.parse(file);