source: frontend/node_modules/webpack/lib/javascript/StartupHelpers.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const Template = require("../Template");
10const { isSubset } = require("../util/SetHelpers");
11const { getAllChunks } = require("./ChunkHelpers");
12
13/** @typedef {import("../util/Hash")} Hash */
14/** @typedef {import("../Chunk")} Chunk */
15/** @typedef {import("../Chunk").ChunkId} ChunkId */
16/** @typedef {import("../ChunkGraph")} ChunkGraph */
17/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
18/** @typedef {import("../Entrypoint")} Entrypoint */
19/** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
20/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
21
22const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `;
23
24/** @typedef {Set<Chunk>} Chunks */
25/** @typedef {ModuleId[]} ModuleIds */
26
27/**
28 * Returns runtime code.
29 * @param {ChunkGraph} chunkGraph chunkGraph
30 * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
31 * @param {EntryModuleWithChunkGroup[]} entries entries
32 * @param {Chunk} chunk chunk
33 * @param {boolean} passive true: passive startup with on chunks loaded
34 * @returns {string} runtime code
35 */
36module.exports.generateEntryStartup = (
37 chunkGraph,
38 runtimeTemplate,
39 entries,
40 chunk,
41 passive
42) => {
43 /** @type {string[]} */
44 const runtime = [
45 `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
46 `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`,
47 "moduleId"
48 )}`
49 ];
50
51 /**
52 * Returns fn to execute.
53 * @param {ModuleId} id id
54 * @returns {string} fn to execute
55 */
56 const runModule = (id) => `__webpack_exec__(${JSON.stringify(id)})`;
57 /**
58 * Output combination.
59 * @param {Chunks} chunks chunks
60 * @param {ModuleIds} moduleIds module ids
61 * @param {boolean=} final true when final, otherwise false
62 */
63 const outputCombination = (chunks, moduleIds, final) => {
64 if (chunks.size === 0) {
65 runtime.push(
66 `${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});`
67 );
68 } else {
69 const fn = runtimeTemplate.returningFunction(
70 moduleIds.map(runModule).join(", ")
71 );
72 runtime.push(
73 `${final && !passive ? EXPORT_PREFIX : ""}${
74 passive
75 ? RuntimeGlobals.onChunksLoaded
76 : RuntimeGlobals.startupEntrypoint
77 }(0, ${JSON.stringify(Array.from(chunks, (c) => c.id))}, ${fn});`
78 );
79 if (final && passive) {
80 runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`);
81 }
82 }
83 };
84
85 /** @type {Chunks | undefined} */
86 let currentChunks;
87 /** @type {ModuleIds | undefined} */
88 let currentModuleIds;
89
90 for (const [module, entrypoint] of entries) {
91 if (!chunkGraph.getModuleSourceTypes(module).has("javascript")) {
92 continue;
93 }
94 const runtimeChunk =
95 /** @type {Entrypoint} */
96 (entrypoint).getRuntimeChunk();
97 const moduleId = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
98 const chunks = getAllChunks(
99 /** @type {Entrypoint} */
100 (entrypoint),
101 chunk,
102 runtimeChunk
103 );
104 if (
105 currentChunks &&
106 currentChunks.size === chunks.size &&
107 isSubset(currentChunks, chunks)
108 ) {
109 /** @type {ModuleIds} */
110 (currentModuleIds).push(moduleId);
111 } else {
112 if (currentChunks) {
113 outputCombination(
114 currentChunks,
115 /** @type {ModuleIds} */ (currentModuleIds)
116 );
117 }
118 currentChunks = chunks;
119 currentModuleIds = [moduleId];
120 }
121 }
122
123 // output current modules with export prefix
124 if (currentChunks) {
125 outputCombination(
126 currentChunks,
127 /** @type {ModuleIds} */
128 (currentModuleIds),
129 true
130 );
131 }
132 runtime.push("");
133 return Template.asString(runtime);
134};
135
136/**
137 * Returns initially fulfilled chunk ids.
138 * @param {Chunk} chunk the chunk
139 * @param {ChunkGraph} chunkGraph the chunk graph
140 * @param {(chunk: Chunk, chunkGraph: ChunkGraph) => boolean} filterFn filter function
141 * @returns {Set<ChunkId>} initially fulfilled chunk ids
142 */
143module.exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
144 /** @type {Set<ChunkId>} */
145 const initialChunkIds = new Set(chunk.ids);
146 for (const c of chunk.getAllInitialChunks()) {
147 if (c === chunk || filterFn(c, chunkGraph)) continue;
148 for (const id of /** @type {ChunkId[]} */ (c.ids)) {
149 initialChunkIds.add(id);
150 }
151 }
152 return initialChunkIds;
153};
154
155/**
156 * Processes the provided hash.
157 * @param {Hash} hash the hash to update
158 * @param {ChunkGraph} chunkGraph chunkGraph
159 * @param {EntryModuleWithChunkGroup[]} entries entries
160 * @param {Chunk} chunk chunk
161 * @returns {void}
162 */
163module.exports.updateHashForEntryStartup = (
164 hash,
165 chunkGraph,
166 entries,
167 chunk
168) => {
169 for (const [module, entrypoint] of entries) {
170 const runtimeChunk =
171 /** @type {Entrypoint} */
172 (entrypoint).getRuntimeChunk();
173 const moduleId = chunkGraph.getModuleId(module);
174 hash.update(`${moduleId}`);
175 for (const c of getAllChunks(
176 /** @type {Entrypoint} */ (entrypoint),
177 chunk,
178 /** @type {Chunk} */ (runtimeChunk)
179 )) {
180 hash.update(`${c.id}`);
181 }
182 }
183};
Note: See TracBrowser for help on using the repository browser.