|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
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; |
8 | 13 | } |
9 | | - this.options = options; |
10 | | -} |
11 | | -module.exports = MinChunkSizePlugin; |
12 | 14 |
|
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) => { |
26 | 20 |
|
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 | + }); |
34 | 28 |
|
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 | + }); |
41 | 36 |
|
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 | + }); |
45 | 43 |
|
46 | | - if(combinations.length === 0) return; |
| 44 | + combinations = combinations.filter((pair) => { |
| 45 | + return pair[1] !== false; |
| 46 | + }); |
47 | 47 |
|
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 | + }); |
53 | 55 |
|
54 | | - var pair = combinations[0]; |
| 56 | + const pair = combinations[0]; |
55 | 57 |
|
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 | + }); |
59 | 62 | }); |
60 | | - }); |
61 | | -}; |
| 63 | + } |
| 64 | +} |
| 65 | +module.exports = MinChunkSizePlugin; |
0 commit comments