From 173ace908eeeeef0babaead91eb9a946b22b3ae8 Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Thu, 4 Feb 2016 17:11:38 -0800 Subject: [PATCH 1/2] Update index.js --- index.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index b2b267f..53d781d 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ var async = require("async"); var url = require('url'); var RawSource = require("webpack-sources/lib/RawSource"); +var CompressionOptions = {}; function CompressionPlugin(options) { options = options || {}; @@ -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); + 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, { + 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, CompressionOptions, function(err, result) { if(err) return callback(err); if(result.length / originalSize > this.minRatio) return callback(); var parse = url.parse(file); From ee8e701e47b2f488da32b135abe88a47972487de Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Thu, 11 Feb 2016 10:40:56 -0800 Subject: [PATCH 2/2] instance var instead of static --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 53d781d..79a918a 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,12 @@ var async = require("async"); var url = require('url'); var RawSource = require("webpack-sources/lib/RawSource"); -var CompressionOptions = {}; 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 { @@ -19,7 +19,7 @@ function CompressionPlugin(options) { } catch(err) { throw new Error("node-zopfli not found"); } - CompressionOptions = { + 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, @@ -34,7 +34,7 @@ function CompressionPlugin(options) { var zlib = require("zlib"); this.algorithm = zlib[this.algorithm]; if(!this.algorithm) throw new Error("Algorithm not found in zlib"); - CompressionOptions = { + this.compressionOptions = { level: options.level || 9, flush: options.flush, chunkSize: options.chunkSize, @@ -67,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, CompressionOptions, 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);