|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -function CommonsChunkPlugin(chunkName, filenameTemplate, entryPoints, minCount) { |
6 | | - if(typeof filenameTemplate !== "string") { |
7 | | - minCount = entryPoints; |
8 | | - entryPoints = filenameTemplate |
9 | | - filenameTemplate = chunkName; |
| 5 | +function CommonsChunkPlugin(chunkNames, filenameTemplate, selectedChunks, minCount) { |
| 6 | + if(typeof filenameTemplate !== "string" && filenameTemplate !== null) { |
| 7 | + minCount = selectedChunks; |
| 8 | + selectedChunks = filenameTemplate |
| 9 | + filenameTemplate = chunkNames; |
10 | 10 | } |
11 | | - if(typeof entryPoints === "number") { |
12 | | - minCount = entryPoints; |
13 | | - entryPoints = undefined; |
| 11 | + if(!Array.isArray(selectedChunks) && typeof selectedChunks !== "boolean") { |
| 12 | + minCount = selectedChunks; |
| 13 | + selectedChunks = undefined; |
14 | 14 | } |
15 | | - this.chunkName = chunkName; |
| 15 | + this.chunkNames = chunkNames; |
16 | 16 | this.filenameTemplate = filenameTemplate; |
17 | 17 | this.minCount = minCount; |
18 | | - this.entryPoints = entryPoints; |
| 18 | + this.selectedChunks = selectedChunks; |
19 | 19 | } |
20 | 20 | module.exports = CommonsChunkPlugin; |
21 | 21 | CommonsChunkPlugin.prototype.apply = function(compiler) { |
22 | | - var chunkName = this.chunkName; |
| 22 | + var chunkNames = this.chunkNames; |
23 | 23 | var filenameTemplate = this.filenameTemplate; |
24 | 24 | var minCount = this.minCount; |
25 | | - var entryPoints = this.entryPoints; |
| 25 | + var selectedChunks = this.selectedChunks; |
26 | 26 | compiler.plugin("this-compilation", function(compilation) { |
27 | 27 | compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) { |
28 | | - var commonModulesCount = []; |
29 | | - var commonModules = []; |
30 | | - var commonChunk = chunks.filter(function(chunk) { |
31 | | - return chunk.name === chunkName; |
32 | | - })[0] || this.addChunk(chunkName); |
33 | | - var usedChunks = chunks.filter(function(chunk) { |
34 | | - if(chunk === commonChunk) return false; |
35 | | - if(!chunk.entry) return false; |
36 | | - if(!entryPoints) return true; |
37 | | - return entryPoints.indexOf(chunk.name) >= 0; |
38 | | - }); |
39 | | - usedChunks.forEach(function(chunk) { |
40 | | - chunk.modules.forEach(function(module) { |
41 | | - var idx = commonModules.indexOf(module); |
42 | | - if(idx < 0) { |
43 | | - commonModules.push(module); |
44 | | - commonModulesCount.push(1); |
45 | | - } else { |
46 | | - commonModulesCount[idx]++; |
47 | | - } |
| 28 | + if(!chunkNames && selectedChunks === false) { |
| 29 | + var commonChunks = chunks; |
| 30 | + } else if(Array.isArray(chunkNames)) { |
| 31 | + var commonChunks = chunks.filter(function(chunk) { |
| 32 | + return chunkNames.indexOf(chunk.name) >= 0; |
48 | 33 | }); |
49 | | - }); |
50 | | - commonModulesCount.forEach(function(count, idx) { |
51 | | - if(count >= (minCount || Math.max(2, usedChunks.length))) { |
| 34 | + } else { |
| 35 | + var commonChunks = chunks.filter(function(chunk) { |
| 36 | + return chunk.name === chunkNames; |
| 37 | + }); |
| 38 | + } |
| 39 | + if(commonChunks.length === 0) { |
| 40 | + var chunk = this.addChunk(chunkNames); |
| 41 | + chunk.initial = chunk.entry = true; |
| 42 | + commonChunks = [chunk]; |
| 43 | + } |
| 44 | + commonChunks.forEach(function processCommonChunk(commonChunk) { |
| 45 | + var commonModulesCount = []; |
| 46 | + var commonModules = []; |
| 47 | + if(Array.isArray(selectedChunks)) { |
| 48 | + var usedChunks = chunks.filter(function(chunk) { |
| 49 | + if(chunk === commonChunk) return false; |
| 50 | + return selectedChunks.indexOf(chunk.name) >= 0; |
| 51 | + }); |
| 52 | + } else if(selectedChunks === false || !commonChunk.entry) { |
| 53 | + var usedChunks = (commonChunk.chunks || []).filter(function(chunk) { |
| 54 | + // we can only move modules from this chunk if the "commonChunk" is the only parent |
| 55 | + return chunk.parents.length === 1; |
| 56 | + }); |
| 57 | + } else { |
| 58 | + var usedChunks = chunks.filter(function(chunk) { |
| 59 | + if(chunk === commonChunk) return false; |
| 60 | + return chunk.entry; |
| 61 | + }); |
| 62 | + } |
| 63 | + usedChunks.forEach(function(chunk) { |
| 64 | + chunk.modules.forEach(function(module) { |
| 65 | + var idx = commonModules.indexOf(module); |
| 66 | + if(idx < 0) { |
| 67 | + commonModules.push(module); |
| 68 | + commonModulesCount.push(1); |
| 69 | + } else { |
| 70 | + commonModulesCount[idx]++; |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + commonModulesCount.forEach(function(count, idx) { |
52 | 75 | var module = commonModules[idx]; |
| 76 | + if(typeof minCount === "function") { |
| 77 | + if(!minCount(module, count)) |
| 78 | + return; |
| 79 | + } else if(count < (minCount || Math.max(2, usedChunks.length))) { |
| 80 | + return; |
| 81 | + } |
53 | 82 | usedChunks.forEach(function(chunk) { |
54 | 83 | module.removeChunk(chunk); |
55 | 84 | }); |
56 | 85 | commonChunk.addModule(module); |
57 | 86 | module.addChunk(commonChunk); |
58 | | - } |
59 | | - }); |
60 | | - usedChunks.forEach(function(chunk) { |
61 | | - chunk.parents = [commonChunk]; |
62 | | - commonChunk.chunks.push(chunk); |
63 | | - chunk.entry = false; |
| 87 | + }); |
| 88 | + usedChunks.forEach(function(chunk) { |
| 89 | + chunk.parents = [commonChunk]; |
| 90 | + commonChunk.chunks.push(chunk); |
| 91 | + chunk.entry = false; |
| 92 | + }); |
| 93 | + if(filenameTemplate) |
| 94 | + commonChunk.filenameTemplate = filenameTemplate; |
64 | 95 | }); |
65 | | - commonChunk.initial = commonChunk.entry = true; |
66 | | - commonChunk.filenameTemplate = filenameTemplate; |
67 | 96 | }); |
68 | 97 | }); |
69 | 98 | }; |
0 commit comments