|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -function LimitChunkCountPlugin(options) { |
6 | | - if(options !== undefined && 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"); |
8 | | - } |
9 | | - this.options = options || {}; |
10 | | -} |
11 | | -module.exports = LimitChunkCountPlugin; |
| 5 | +"use strict"; |
12 | 6 |
|
13 | | -LimitChunkCountPlugin.prototype.apply = function(compiler) { |
14 | | - var options = this.options; |
15 | | - compiler.plugin("compilation", function(compilation) { |
16 | | - compilation.plugin("optimize-chunks-advanced", function(chunks) { |
17 | | - var maxChunks = options.maxChunks; |
18 | | - if(!maxChunks) return; |
19 | | - if(maxChunks < 1) return; |
20 | | - if(chunks.length <= maxChunks) return; |
| 7 | +class LimitChunkCountPlugin { |
| 8 | + constructor(options) { |
| 9 | + if(options !== undefined && 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 || {}; |
| 13 | + } |
| 14 | + apply(compiler) { |
| 15 | + const options = this.options; |
| 16 | + compiler.plugin("compilation", (compilation) => { |
| 17 | + compilation.plugin("optimize-chunks-advanced", (chunks) => { |
| 18 | + const maxChunks = options.maxChunks; |
| 19 | + if(!maxChunks) return; |
| 20 | + if(maxChunks < 1) return; |
| 21 | + if(chunks.length <= maxChunks) return; |
21 | 22 |
|
22 | | - if(chunks.length > maxChunks) { |
23 | | - var combinations = []; |
24 | | - chunks.forEach(function(a, idx) { |
25 | | - for(var i = 0; i < idx; i++) { |
26 | | - var b = chunks[i]; |
27 | | - combinations.push([b, a]); |
28 | | - } |
29 | | - }); |
| 23 | + if(chunks.length > maxChunks) { |
| 24 | + let combinations = []; |
| 25 | + chunks.forEach((a, idx) => { |
| 26 | + for(let i = 0; i < idx; i++) { |
| 27 | + const b = chunks[i]; |
| 28 | + combinations.push([b, a]); |
| 29 | + } |
| 30 | + }); |
30 | 31 |
|
31 | | - combinations.forEach(function(pair) { |
32 | | - var a = pair[0].size(options); |
33 | | - var b = pair[1].size(options); |
34 | | - var ab = pair[0].integratedSize(pair[1], options); |
35 | | - pair.unshift(a + b - ab, ab); |
36 | | - pair.push(a, b); |
37 | | - }); |
38 | | - combinations = combinations.filter(function(pair) { |
39 | | - return pair[1] !== false; |
40 | | - }); |
41 | | - combinations.sort(function(a, b) { |
42 | | - var diff = b[0] - a[0]; |
43 | | - if(diff !== 0) return diff; |
44 | | - return a[1] - b[1]; |
45 | | - }); |
| 32 | + combinations.forEach((pair) => { |
| 33 | + const a = pair[0].size(options); |
| 34 | + const b = pair[1].size(options); |
| 35 | + const ab = pair[0].integratedSize(pair[1], options); |
| 36 | + pair.unshift(a + b - ab, ab); |
| 37 | + pair.push(a, b); |
| 38 | + }); |
| 39 | + combinations = combinations.filter((pair) => { |
| 40 | + return pair[1] !== false; |
| 41 | + }); |
| 42 | + combinations.sort((a, b) => { |
| 43 | + const diff = b[0] - a[0]; |
| 44 | + if(diff !== 0) return diff; |
| 45 | + return a[1] - b[1]; |
| 46 | + }); |
46 | 47 |
|
47 | | - var pair = combinations[0]; |
| 48 | + const pair = combinations[0]; |
48 | 49 |
|
49 | | - if(pair && pair[2].integrate(pair[3], "limit")) { |
50 | | - chunks.splice(chunks.indexOf(pair[3]), 1); |
51 | | - return true; |
| 50 | + if(pair && pair[2].integrate(pair[3], "limit")) { |
| 51 | + chunks.splice(chunks.indexOf(pair[3]), 1); |
| 52 | + return true; |
| 53 | + } |
52 | 54 | } |
53 | | - } |
| 55 | + }); |
54 | 56 | }); |
55 | | - }); |
56 | | -}; |
| 57 | + } |
| 58 | +} |
| 59 | +module.exports = LimitChunkCountPlugin; |
0 commit comments