Skip to content

Commit

Permalink
refactor: Use emit event instead of this-compilation (#71)
Browse files Browse the repository at this point in the history
Given that compression is most likely one of the last steps on the pipeline
chain, it makes sense that this plugin uses the `emit` event which is the last
step to add/modify assets before they're written to disk.

This plays nicely with other plugins that create and delete files
on the fly AFTER they're compiled
(for example https://github.com/jtefera/merge-files-webpack, which is the
recommeded solution for this issue
webpack-contrib/extract-text-webpack-plugin#179)
  • Loading branch information
ijpiantanida authored and joshwiens committed Sep 29, 2017
1 parent 147392d commit 9ebc852
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import url from 'url';
import async from 'async';
Expand Down Expand Up @@ -38,54 +38,53 @@ class CompressionPlugin {
}

apply(compiler) {
compiler.plugin('this-compilation', (compilation) => {
compilation.plugin('optimize-assets', (assets, callback) => {
async.forEach(Object.keys(assets), (file, cb) => {
if (Array.isArray(this.test)) {
if (this.test.every(t => !t.test(file))) {
return cb();
}
} else if (this.test && !this.test.test(file)) {
compiler.plugin('emit', (compilation, callback) => {
const assets = compilation.assets;
async.forEach(Object.keys(assets), (file, cb) => {
if (Array.isArray(this.test)) {
if (this.test.every(t => !t.test(file))) {
return cb();
}
const asset = assets[file];
let content = asset.source();
} else if (this.test && !this.test.test(file)) {
return cb();
}
const asset = assets[file];
let content = asset.source();

if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
}
if (!Buffer.isBuffer(content)) {
content = new Buffer(content, 'utf-8');
}

const originalSize = content.length;
const originalSize = content.length;

if (originalSize < this.threshold) {
return cb();
}
if (originalSize < this.threshold) {
return cb();
}

this.algorithm(content, this.compressionOptions, (err, result) => {
if (err) { return cb(err); }
this.algorithm(content, this.compressionOptions, (err, result) => {
if (err) { return cb(err); }

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

const parse = url.parse(file);
const sub = {
file,
path: parse.pathname,
query: parse.query || '',
};
const parse = url.parse(file);
const sub = {
file,
path: parse.pathname,
query: parse.query || '',
};

let newFile = this.asset.replace(/\[(file|path|query)\]/g, (p0, p1) => sub[p1]);
let newFile = this.asset.replace(/\[(file|path|query)\]/g, (p0, p1) => sub[p1]);

if (typeof this.filename === 'function') {
newFile = this.filename(newFile);
}
assets[newFile] = new RawSource(result);
if (this.deleteOriginalAssets) {
delete assets[file];
}
cb();
});
}, callback);
});
if (typeof this.filename === 'function') {
newFile = this.filename(newFile);
}
assets[newFile] = new RawSource(result);
if (this.deleteOriginalAssets) {
delete assets[file];
}
cb();
});
}, callback);
});
}
}
Expand Down

0 comments on commit 9ebc852

Please sign in to comment.