diff options
Diffstat (limited to 'node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js')
-rw-r--r-- | node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js | 120 |
1 files changed, 53 insertions, 67 deletions
diff --git a/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js b/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js index a6bb30b34..45ec34719 100644 --- a/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js +++ b/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js @@ -15,98 +15,84 @@ class OccurrenceOrderPlugin { const preferEntry = this.preferEntry;
compiler.plugin("compilation", (compilation) => {
compilation.plugin("optimize-module-order", (modules) => {
- function entryChunks(m) {
- return m.chunks.map((c) => {
- const sum = (c.isInitial() ? 1 : 0) + (c.entryModule === m ? 1 : 0);
- return sum;
- }).reduce((a, b) => {
- return a + b;
- }, 0);
- }
+ const occursInInitialChunksMap = new Map();
+ const occursInAllChunksMap = new Map();
- function occursInEntry(m) {
- if(typeof m.__OccurenceOrderPlugin_occursInEntry === "number") return m.__OccurenceOrderPlugin_occursInEntry;
- const result = m.reasons.map((r) => {
- if(!r.module) return 0;
- return entryChunks(r.module);
- }).reduce((a, b) => {
- return a + b;
- }, 0) + entryChunks(m);
- return m.__OccurenceOrderPlugin_occursInEntry = result;
- }
+ const initialChunkChunkMap = new Map();
+ const entryCountMap = new Map();
+ modules.forEach(m => {
+ let initial = 0;
+ let entry = 0;
+ m.forEachChunk(c => {
+ if(c.isInitial()) initial++;
+ if(c.entryModule === m) entry++;
+ });
+ initialChunkChunkMap.set(m, initial);
+ entryCountMap.set(m, entry);
+ });
+
+ const countOccursInEntry = (sum, r) => {
+ if(!r.module) return sum;
+ return sum + initialChunkChunkMap.get(r.module);
+ };
+ const countOccurs = (sum, r) => {
+ if(!r.module) return sum;
+ return sum + r.module.getNumberOfChunks();
+ };
- function occurs(m) {
- if(typeof m.__OccurenceOrderPlugin_occurs === "number") return m.__OccurenceOrderPlugin_occurs;
- const result = m.reasons.map((r) => {
- if(!r.module) return 0;
- return r.module.chunks.length;
- }).reduce((a, b) => {
- return a + b;
- }, 0) + m.chunks.length + m.chunks.filter((c) => {
- return c.entryModule === m;
- }).length;
- return m.__OccurenceOrderPlugin_occurs = result;
+ if(preferEntry) {
+ modules.forEach(m => {
+ const result = m.reasons.reduce(countOccursInEntry, 0) + initialChunkChunkMap.get(m) + entryCountMap.get(m);
+ occursInInitialChunksMap.set(m, result);
+ });
}
+
+ modules.forEach(m => {
+ const result = m.reasons.reduce(countOccurs, 0) + m.getNumberOfChunks() + entryCountMap.get(m);
+ occursInAllChunksMap.set(m, result);
+ });
+
modules.sort((a, b) => {
if(preferEntry) {
- const aEntryOccurs = occursInEntry(a);
- const bEntryOccurs = occursInEntry(b);
+ const aEntryOccurs = occursInInitialChunksMap.get(a);
+ const bEntryOccurs = occursInInitialChunksMap.get(b);
if(aEntryOccurs > bEntryOccurs) return -1;
if(aEntryOccurs < bEntryOccurs) return 1;
}
- const aOccurs = occurs(a);
- const bOccurs = occurs(b);
+ const aOccurs = occursInAllChunksMap.get(a);
+ const bOccurs = occursInAllChunksMap.get(b);
if(aOccurs > bOccurs) return -1;
if(aOccurs < bOccurs) return 1;
- if(a.identifier() > b.identifier()) return 1;
- if(a.identifier() < b.identifier()) return -1;
+ if(a.index > b.index) return 1;
+ if(a.index < b.index) return -1;
return 0;
});
- // TODO refactor to Map
- modules.forEach((m) => {
- m.__OccurenceOrderPlugin_occursInEntry = undefined;
- m.__OccurenceOrderPlugin_occurs = undefined;
- });
});
compilation.plugin("optimize-chunk-order", (chunks) => {
- function occursInEntry(c) {
- if(typeof c.__OccurenceOrderPlugin_occursInEntry === "number") return c.__OccurenceOrderPlugin_occursInEntry;
- const result = c.parents.filter((p) => {
- return p.isInitial();
- }).length;
- return c.__OccurenceOrderPlugin_occursInEntry = result;
- }
+ const occursInInitialChunksMap = new Map();
+
+ chunks.forEach(c => {
+ const result = c.parents.reduce((sum, p) => {
+ if(p.isInitial()) return sum + 1;
+ return sum;
+ }, 0);
+ return occursInInitialChunksMap.set(c, result);
+ });
function occurs(c) {
return c.blocks.length;
}
- chunks.forEach((c) => {
- c.modules.sort((a, b) => {
- if(a.identifier() > b.identifier()) return 1;
- if(a.identifier() < b.identifier()) return -1;
- return 0;
- });
- });
+
chunks.sort((a, b) => {
- const aEntryOccurs = occursInEntry(a);
- const bEntryOccurs = occursInEntry(b);
+ const aEntryOccurs = occursInInitialChunksMap.get(a);
+ const bEntryOccurs = occursInInitialChunksMap.get(b);
if(aEntryOccurs > bEntryOccurs) return -1;
if(aEntryOccurs < bEntryOccurs) return 1;
const aOccurs = occurs(a);
const bOccurs = occurs(b);
if(aOccurs > bOccurs) return -1;
if(aOccurs < bOccurs) return 1;
- if(a.modules.length > b.modules.length) return -1;
- if(a.modules.length < b.modules.length) return 1;
- for(let i = 0; i < a.modules.length; i++) {
- if(a.modules[i].identifier() > b.modules[i].identifier()) return -1;
- if(a.modules[i].identifier() < b.modules[i].identifier()) return 1;
- }
- return 0;
- });
- // TODO refactor to Map
- chunks.forEach((c) => {
- c.__OccurenceOrderPlugin_occursInEntry = undefined;
+ return a.compareTo(b);
});
});
});
|