Skip to content

Commit

Permalink
Merge pull request #6 from Regily/master
Browse files Browse the repository at this point in the history
Compile on a later stage
  • Loading branch information
mynameiswhm authored Jul 19, 2017
2 parents 1731253 + 8ec7fc7 commit a18a9ca
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,50 @@ function CompressionPlugin(options) {
module.exports = CompressionPlugin;

CompressionPlugin.prototype.apply = function (compiler) {
compiler.plugin('this-compilation', function (compilation) {
compilation.plugin('optimize-assets', function (assets, callback) {
async.forEach(Object.keys(assets), function (file, callback) {
if (this.test && !this.test.test(file)) {
return callback();
}
compiler.plugin('emit', function (compilation, callback) {
var assets = compilation.assets;

async.forEach(Object.keys(assets), function (file, callback) {
if (this.test && !this.test.test(file)) {
return callback();
}

var asset = assets[file];
var content = asset.source();
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
}

var asset = assets[file];
var content = asset.source();
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
var originalSize = content.length;
if (originalSize < this.threshold) {
return callback();
}

this.compress(content, function (err, result) {
if (err) {
return callback(err);
}

var originalSize = content.length;
if (originalSize < this.threshold) {
if (result.length / originalSize > this.minRatio) {
return callback();
}

this.compress(content, function (err, result) {
if (err) {
return callback(err);
}

if (result.length / originalSize > this.minRatio) {
return callback();
}

var parse = url.parse(file);
var sub = {
file: file,
fileWithoutExt: file.split('.').slice(0, -1).join('.'),
ext: file.split('.').slice(-1).join(''),
path: parse.pathname,
query: parse.query || ''
};
var parse = url.parse(file);
var sub = {
file: file,
fileWithoutExt: file.split('.').slice(0, -1).join('.'),
ext: file.split('.').slice(-1).join(''),
path: parse.pathname,
query: parse.query || ''
};

var newFile = this.asset.replace(/\[(file|fileWithoutExt|ext|path|query)]/g, function (p0, p1) {
return sub[p1];
});
var newFile = this.asset.replace(/\[(file|fileWithoutExt|ext|path|query)]/g, function (p0, p1) {
return sub[p1];
});

assets[newFile] = new RawSource(result);
callback();
}.bind(this));
}.bind(this), callback);
}.bind(this));
assets[newFile] = new RawSource(result);
callback();
}.bind(this));
}.bind(this), callback);
}.bind(this));
};

0 comments on commit a18a9ca

Please sign in to comment.