aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js')
-rw-r--r--node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js237
1 files changed, 135 insertions, 102 deletions
diff --git a/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js b/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js
index 45ec34719..c73ec8e57 100644
--- a/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js
+++ b/node_modules/webpack/lib/optimize/OccurrenceOrderPlugin.js
@@ -1,102 +1,135 @@
-/*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
-*/
-"use strict";
-
-class OccurrenceOrderPlugin {
- constructor(preferEntry) {
- if(preferEntry !== undefined && typeof preferEntry !== "boolean") {
- throw new Error("Argument should be a boolean.\nFor more info on this plugin, see https://webpack.js.org/plugins/");
- }
- this.preferEntry = preferEntry;
- }
- apply(compiler) {
- const preferEntry = this.preferEntry;
- compiler.plugin("compilation", (compilation) => {
- compilation.plugin("optimize-module-order", (modules) => {
- const occursInInitialChunksMap = new Map();
- const occursInAllChunksMap = new Map();
-
- 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();
- };
-
- 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 = occursInInitialChunksMap.get(a);
- const bEntryOccurs = occursInInitialChunksMap.get(b);
- if(aEntryOccurs > bEntryOccurs) return -1;
- if(aEntryOccurs < bEntryOccurs) return 1;
- }
- const aOccurs = occursInAllChunksMap.get(a);
- const bOccurs = occursInAllChunksMap.get(b);
- if(aOccurs > bOccurs) return -1;
- if(aOccurs < bOccurs) return 1;
- if(a.index > b.index) return 1;
- if(a.index < b.index) return -1;
- return 0;
- });
- });
- compilation.plugin("optimize-chunk-order", (chunks) => {
- 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.sort((a, 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;
- return a.compareTo(b);
- });
- });
- });
- }
-}
-
-module.exports = OccurrenceOrderPlugin;
+/*
+ MIT License http://www.opensource.org/licenses/mit-license.php
+ Author Tobias Koppers @sokra
+*/
+"use strict";
+
+// TODO webpack 5 remove this plugin
+// It has been splitted into separate plugins for modules and chunks
+class OccurrenceOrderPlugin {
+ constructor(preferEntry) {
+ if (preferEntry !== undefined && typeof preferEntry !== "boolean") {
+ throw new Error(
+ "Argument should be a boolean.\nFor more info on this plugin, see https://webpack.js.org/plugins/"
+ );
+ }
+ this.preferEntry = preferEntry;
+ }
+ apply(compiler) {
+ const preferEntry = this.preferEntry;
+ compiler.hooks.compilation.tap("OccurrenceOrderPlugin", compilation => {
+ compilation.hooks.optimizeModuleOrder.tap(
+ "OccurrenceOrderPlugin",
+ modules => {
+ const occursInInitialChunksMap = new Map();
+ const occursInAllChunksMap = new Map();
+
+ const initialChunkChunkMap = new Map();
+ const entryCountMap = new Map();
+ for (const m of modules) {
+ let initial = 0;
+ let entry = 0;
+ for (const c of m.chunksIterable) {
+ if (c.canBeInitial()) 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;
+ }
+ let factor = 1;
+ if (typeof r.dependency.getNumberOfIdOccurrences === "function") {
+ factor = r.dependency.getNumberOfIdOccurrences();
+ }
+ if (factor === 0) {
+ return sum;
+ }
+ return sum + factor * r.module.getNumberOfChunks();
+ };
+
+ if (preferEntry) {
+ for (const m of modules) {
+ const result =
+ m.reasons.reduce(countOccursInEntry, 0) +
+ initialChunkChunkMap.get(m) +
+ entryCountMap.get(m);
+ occursInInitialChunksMap.set(m, result);
+ }
+ }
+
+ const originalOrder = new Map();
+ let i = 0;
+ for (const m of modules) {
+ const result =
+ m.reasons.reduce(countOccurs, 0) +
+ m.getNumberOfChunks() +
+ entryCountMap.get(m);
+ occursInAllChunksMap.set(m, result);
+ originalOrder.set(m, i++);
+ }
+
+ modules.sort((a, b) => {
+ if (preferEntry) {
+ const aEntryOccurs = occursInInitialChunksMap.get(a);
+ const bEntryOccurs = occursInInitialChunksMap.get(b);
+ if (aEntryOccurs > bEntryOccurs) return -1;
+ if (aEntryOccurs < bEntryOccurs) return 1;
+ }
+ const aOccurs = occursInAllChunksMap.get(a);
+ const bOccurs = occursInAllChunksMap.get(b);
+ if (aOccurs > bOccurs) return -1;
+ if (aOccurs < bOccurs) return 1;
+ const orgA = originalOrder.get(a);
+ const orgB = originalOrder.get(b);
+ return orgA - orgB;
+ });
+ }
+ );
+ compilation.hooks.optimizeChunkOrder.tap(
+ "OccurrenceOrderPlugin",
+ chunks => {
+ const occursInInitialChunksMap = new Map();
+ const originalOrder = new Map();
+
+ let i = 0;
+ for (const c of chunks) {
+ let occurs = 0;
+ for (const chunkGroup of c.groupsIterable) {
+ for (const parent of chunkGroup.parentsIterable) {
+ if (parent.isInitial()) occurs++;
+ }
+ }
+ occursInInitialChunksMap.set(c, occurs);
+ originalOrder.set(c, i++);
+ }
+
+ chunks.sort((a, b) => {
+ const aEntryOccurs = occursInInitialChunksMap.get(a);
+ const bEntryOccurs = occursInInitialChunksMap.get(b);
+ if (aEntryOccurs > bEntryOccurs) return -1;
+ if (aEntryOccurs < bEntryOccurs) return 1;
+ const aOccurs = a.getNumberOfGroups();
+ const bOccurs = b.getNumberOfGroups();
+ if (aOccurs > bOccurs) return -1;
+ if (aOccurs < bOccurs) return 1;
+ const orgA = originalOrder.get(a);
+ const orgB = originalOrder.get(b);
+ return orgA - orgB;
+ });
+ }
+ );
+ });
+ }
+}
+
+module.exports = OccurrenceOrderPlugin;