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