Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9ebc852

Browse files
ijpiantanidajoshwiens
authored andcommitted
refactor: Use emit event instead of this-compilation (#71)
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)
1 parent 147392d commit 9ebc852

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

src/index.js

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
44
*/
55
import url from 'url';
66
import async from 'async';
@@ -38,54 +38,53 @@ class CompressionPlugin {
3838
}
3939

4040
apply(compiler) {
41-
compiler.plugin('this-compilation', (compilation) => {
42-
compilation.plugin('optimize-assets', (assets, callback) => {
43-
async.forEach(Object.keys(assets), (file, cb) => {
44-
if (Array.isArray(this.test)) {
45-
if (this.test.every(t => !t.test(file))) {
46-
return cb();
47-
}
48-
} else if (this.test && !this.test.test(file)) {
41+
compiler.plugin('emit', (compilation, callback) => {
42+
const assets = compilation.assets;
43+
async.forEach(Object.keys(assets), (file, cb) => {
44+
if (Array.isArray(this.test)) {
45+
if (this.test.every(t => !t.test(file))) {
4946
return cb();
5047
}
51-
const asset = assets[file];
52-
let content = asset.source();
48+
} else if (this.test && !this.test.test(file)) {
49+
return cb();
50+
}
51+
const asset = assets[file];
52+
let content = asset.source();
5353

54-
if (!Buffer.isBuffer(content)) {
55-
content = new Buffer(content, 'utf-8');
56-
}
54+
if (!Buffer.isBuffer(content)) {
55+
content = new Buffer(content, 'utf-8');
56+
}
5757

58-
const originalSize = content.length;
58+
const originalSize = content.length;
5959

60-
if (originalSize < this.threshold) {
61-
return cb();
62-
}
60+
if (originalSize < this.threshold) {
61+
return cb();
62+
}
6363

64-
this.algorithm(content, this.compressionOptions, (err, result) => {
65-
if (err) { return cb(err); }
64+
this.algorithm(content, this.compressionOptions, (err, result) => {
65+
if (err) { return cb(err); }
6666

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

69-
const parse = url.parse(file);
70-
const sub = {
71-
file,
72-
path: parse.pathname,
73-
query: parse.query || '',
74-
};
69+
const parse = url.parse(file);
70+
const sub = {
71+
file,
72+
path: parse.pathname,
73+
query: parse.query || '',
74+
};
7575

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

78-
if (typeof this.filename === 'function') {
79-
newFile = this.filename(newFile);
80-
}
81-
assets[newFile] = new RawSource(result);
82-
if (this.deleteOriginalAssets) {
83-
delete assets[file];
84-
}
85-
cb();
86-
});
87-
}, callback);
88-
});
78+
if (typeof this.filename === 'function') {
79+
newFile = this.filename(newFile);
80+
}
81+
assets[newFile] = new RawSource(result);
82+
if (this.deleteOriginalAssets) {
83+
delete assets[file];
84+
}
85+
cb();
86+
});
87+
}, callback);
8988
});
9089
}
9190
}

0 commit comments

Comments
 (0)