From 93d7d601abc4387992913401efa4ed3504bdaef6 Mon Sep 17 00:00:00 2001 From: Ignacio Piantanida Date: Sat, 2 Sep 2017 16:13:01 -0300 Subject: [PATCH] Use emit event instead of this-compilation 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) --- src/index.js | 79 ++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/index.js b/src/index.js index 4883911..415f193 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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); }); } }