Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use emit event instead of this-compilation #71

Merged
merged 1 commit into from
Sep 29, 2017
Merged
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
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