Skip to content

Commit 70bf6c2

Browse files
shubhekshaTheLarkInn
authored andcommitted
refactor(ES6): upgrade MinChunkSizePlugin to ES6 (webpack#3757)
* refactor(ES6): upgrade MergeDuplicateChunksPlugin to ES6
1 parent 4149f10 commit 70bf6c2

File tree

2 files changed

+72
-65
lines changed

2 files changed

+72
-65
lines changed

lib/optimize/MergeDuplicateChunksPlugin.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function MergeDuplicateChunksPlugin() {}
6-
module.exports = MergeDuplicateChunksPlugin;
5+
"use strict";
76

87
function getChunkIdentifier(chunk) {
9-
return chunk.modules.map(function(m) {
8+
return chunk.modules.map((m) => {
109
return m.identifier();
1110
}).sort().join(", ");
1211
}
1312

14-
MergeDuplicateChunksPlugin.prototype.apply = function(compiler) {
15-
compiler.plugin("compilation", function(compilation) {
16-
compilation.plugin("optimize-chunks-basic", function(chunks) {
17-
var map = {};
18-
chunks.slice().forEach(function(chunk) {
19-
if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
20-
var ident = getChunkIdentifier(chunk);
21-
if(map[ident]) {
22-
if(map[ident].integrate(chunk, "duplicate"))
23-
chunks.splice(chunks.indexOf(chunk), 1);
24-
return;
25-
}
26-
map[ident] = chunk;
13+
class MergeDuplicateChunksPlugin {
14+
15+
apply(compiler) {
16+
compiler.plugin("compilation", (compilation) => {
17+
compilation.plugin("optimize-chunks-basic", (chunks) => {
18+
const map = {};
19+
chunks.slice().forEach((chunk) => {
20+
if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
21+
const ident = getChunkIdentifier(chunk);
22+
if(map[ident]) {
23+
if(map[ident].integrate(chunk, "duplicate"))
24+
chunks.splice(chunks.indexOf(chunk), 1);
25+
return;
26+
}
27+
map[ident] = chunk;
28+
});
2729
});
2830
});
29-
});
30-
};
31+
}
32+
}
33+
module.exports = MergeDuplicateChunksPlugin;

lib/optimize/MinChunkSizePlugin.js

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,64 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function MinChunkSizePlugin(options) {
6-
if(typeof options !== "object" || Array.isArray(options)) {
7-
throw new Error("Argument should be an options object.\nFor more info on options, see https://webpack.github.io/docs/list-of-plugins.html");
5+
"use strict";
6+
7+
class MinChunkSizePlugin {
8+
constructor(options) {
9+
if(typeof options !== "object" || Array.isArray(options)) {
10+
throw new Error("Argument should be an options object.\nFor more info on options, see https://webpack.github.io/docs/list-of-plugins.html");
11+
}
12+
this.options = options;
813
}
9-
this.options = options;
10-
}
11-
module.exports = MinChunkSizePlugin;
1214

13-
MinChunkSizePlugin.prototype.apply = function(compiler) {
14-
var options = this.options;
15-
var minChunkSize = options.minChunkSize;
16-
compiler.plugin("compilation", function(compilation) {
17-
compilation.plugin("optimize-chunks-advanced", function(chunks) {
18-
19-
var combinations = [];
20-
chunks.forEach(function(a, idx) {
21-
for(var i = 0; i < idx; i++) {
22-
var b = chunks[i];
23-
combinations.push([b, a]);
24-
}
25-
});
15+
apply(compiler) {
16+
const options = this.options;
17+
const minChunkSize = options.minChunkSize;
18+
compiler.plugin("compilation", (compilation) => {
19+
compilation.plugin("optimize-chunks-advanced", (chunks) => {
2620

27-
var equalOptions = {
28-
chunkOverhead: 1,
29-
entryChunkMultiplicator: 1
30-
};
31-
combinations = combinations.filter(function(pair) {
32-
return pair[0].size(equalOptions) < minChunkSize || pair[1].size(equalOptions) < minChunkSize;
33-
});
21+
let combinations = [];
22+
chunks.forEach((a, idx) => {
23+
for(let i = 0; i < idx; i++) {
24+
const b = chunks[i];
25+
combinations.push([b, a]);
26+
}
27+
});
3428

35-
combinations.forEach(function(pair) {
36-
var a = pair[0].size(options);
37-
var b = pair[1].size(options);
38-
var ab = pair[0].integratedSize(pair[1], options);
39-
pair.unshift(a + b - ab, ab);
40-
});
29+
const equalOptions = {
30+
chunkOverhead: 1,
31+
entryChunkMultiplicator: 1
32+
};
33+
combinations = combinations.filter((pair) => {
34+
return pair[0].size(equalOptions) < minChunkSize || pair[1].size(equalOptions) < minChunkSize;
35+
});
4136

42-
combinations = combinations.filter(function(pair) {
43-
return pair[1] !== false;
44-
});
37+
combinations.forEach((pair) => {
38+
const a = pair[0].size(options);
39+
const b = pair[1].size(options);
40+
const ab = pair[0].integratedSize(pair[1], options);
41+
pair.unshift(a + b - ab, ab);
42+
});
4543

46-
if(combinations.length === 0) return;
44+
combinations = combinations.filter((pair) => {
45+
return pair[1] !== false;
46+
});
4747

48-
combinations.sort(function(a, b) {
49-
var diff = b[0] - a[0];
50-
if(diff !== 0) return diff;
51-
return a[1] - b[1];
52-
});
48+
if(combinations.length === 0) return;
49+
50+
combinations.sort((a, b) => {
51+
const diff = b[0] - a[0];
52+
if(diff !== 0) return diff;
53+
return a[1] - b[1];
54+
});
5355

54-
var pair = combinations[0];
56+
const pair = combinations[0];
5557

56-
pair[2].integrate(pair[3], "min-size");
57-
chunks.splice(chunks.indexOf(pair[3]), 1);
58-
return true;
58+
pair[2].integrate(pair[3], "min-size");
59+
chunks.splice(chunks.indexOf(pair[3]), 1);
60+
return true;
61+
});
5962
});
60-
});
61-
};
63+
}
64+
}
65+
module.exports = MinChunkSizePlugin;

0 commit comments

Comments
 (0)