1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Entrypoint = require("../Entrypoint");
|
9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
10 | const Template = require("../Template");
|
11 | const { isSubset } = require("../util/SetHelpers");
|
12 | const { chunkHasJs } = require("./JavascriptModulesPlugin");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const getAllChunks = (entrypoint, excludedChunk1, excludedChunk2) => {
|
31 | const queue = new Set([entrypoint]);
|
32 | const chunks = new Set();
|
33 | for (const entrypoint of queue) {
|
34 | for (const chunk of entrypoint.chunks) {
|
35 | if (chunk === excludedChunk1) continue;
|
36 | if (chunk === excludedChunk2) continue;
|
37 | chunks.add(chunk);
|
38 | }
|
39 | for (const parent of entrypoint.parentsIterable) {
|
40 | if (parent instanceof Entrypoint) queue.add(parent);
|
41 | }
|
42 | }
|
43 | return chunks;
|
44 | };
|
45 |
|
46 | const EXPORT_PREFIX = "var __webpack_exports__ = ";
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | exports.generateEntryStartup = (
|
57 | chunkGraph,
|
58 | runtimeTemplate,
|
59 | entries,
|
60 | chunk,
|
61 | passive
|
62 | ) => {
|
63 |
|
64 | const runtime = [
|
65 | `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
|
66 | `__webpack_require__(${RuntimeGlobals.entryModuleId} = moduleId)`,
|
67 | "moduleId"
|
68 | )}`
|
69 | ];
|
70 |
|
71 | const runModule = id => {
|
72 | return `__webpack_exec__(${JSON.stringify(id)})`;
|
73 | };
|
74 | const outputCombination = (chunks, moduleIds, final) => {
|
75 | if (chunks.size === 0) {
|
76 | runtime.push(
|
77 | `${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});`
|
78 | );
|
79 | } else {
|
80 | const fn = runtimeTemplate.returningFunction(
|
81 | moduleIds.map(runModule).join(", ")
|
82 | );
|
83 | runtime.push(
|
84 | `${final && !passive ? EXPORT_PREFIX : ""}${
|
85 | passive
|
86 | ? RuntimeGlobals.onChunksLoaded
|
87 | : RuntimeGlobals.startupEntrypoint
|
88 | }(0, ${JSON.stringify(Array.from(chunks, c => c.id))}, ${fn});`
|
89 | );
|
90 | if (final && passive) {
|
91 | runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`);
|
92 | }
|
93 | }
|
94 | };
|
95 |
|
96 | let currentChunks = undefined;
|
97 | let currentModuleIds = undefined;
|
98 |
|
99 | for (const [module, entrypoint] of entries) {
|
100 | const runtimeChunk = entrypoint.getRuntimeChunk();
|
101 | const moduleId = chunkGraph.getModuleId(module);
|
102 | const chunks = getAllChunks(entrypoint, chunk, runtimeChunk);
|
103 | if (
|
104 | currentChunks &&
|
105 | currentChunks.size === chunks.size &&
|
106 | isSubset(currentChunks, chunks)
|
107 | ) {
|
108 | currentModuleIds.push(moduleId);
|
109 | } else {
|
110 | if (currentChunks) {
|
111 | outputCombination(currentChunks, currentModuleIds);
|
112 | }
|
113 | currentChunks = chunks;
|
114 | currentModuleIds = [moduleId];
|
115 | }
|
116 | }
|
117 |
|
118 |
|
119 | if (currentChunks) {
|
120 | outputCombination(currentChunks, currentModuleIds, true);
|
121 | }
|
122 | runtime.push("");
|
123 | return Template.asString(runtime);
|
124 | };
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | exports.updateHashForEntryStartup = (hash, chunkGraph, entries, chunk) => {
|
134 | for (const [module, entrypoint] of entries) {
|
135 | const runtimeChunk = entrypoint.getRuntimeChunk();
|
136 | const moduleId = chunkGraph.getModuleId(module);
|
137 | hash.update(`${moduleId}`);
|
138 | for (const c of getAllChunks(entrypoint, chunk, runtimeChunk))
|
139 | hash.update(`${c.id}`);
|
140 | }
|
141 | };
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | exports.getInitialChunkIds = (chunk, chunkGraph) => {
|
149 | const initialChunkIds = new Set(chunk.ids);
|
150 | for (const c of chunk.getAllInitialChunks()) {
|
151 | if (c === chunk || chunkHasJs(c, chunkGraph)) continue;
|
152 | for (const id of c.ids) initialChunkIds.add(id);
|
153 | }
|
154 | return initialChunkIds;
|
155 | };
|