MCPcopy
hub / github.com/webpack/webpack / generate

Method generate

lib/sharing/ShareRuntimeModule.js:30–152  ·  view source on GitHub ↗

* Generates runtime code for this runtime module. * @returns {string | null} runtime code

()

Source from the content-addressed store, hash-verified

28 * @returns {string | null} runtime code
29 */
30 generate() {
31 const compilation = /** @type {Compilation} */ (this.compilation);
32 const {
33 runtimeTemplate,
34 outputOptions: { uniqueName, ignoreBrowserWarnings }
35 } = compilation;
36 const codeGenerationResults =
37 /** @type {CodeGenerationResults} */
38 (compilation.codeGenerationResults);
39 const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
40 /** @type {Map<string, Map<number, Set<string>>>} */
41 const initCodePerScope = new Map();
42 for (const chunk of /** @type {Chunk} */ (
43 this.chunk
44 ).getAllReferencedChunks()) {
45 const modules = chunkGraph.getOrderedChunkModulesIterableBySourceType(
46 chunk,
47 "share-init",
48 compareModulesByIdentifier
49 );
50 if (!modules) continue;
51 for (const m of modules) {
52 const data = codeGenerationResults.getData(
53 m,
54 chunk.runtime,
55 "share-init"
56 );
57 if (!data) continue;
58 for (const item of data) {
59 const { shareScope, initStage, init } = item;
60 let stages = initCodePerScope.get(shareScope);
61 if (stages === undefined) {
62 initCodePerScope.set(shareScope, (stages = new Map()));
63 }
64 let list = stages.get(initStage || 0);
65 if (list === undefined) {
66 stages.set(initStage || 0, (list = new Set()));
67 }
68 list.add(init);
69 }
70 }
71 }
72 const cst = runtimeTemplate.renderConst();
73 const lt = runtimeTemplate.renderLet();
74 return Template.asString([
75 `${RuntimeGlobals.shareScopeMap} = {};`,
76 `${cst} initPromises = {};`,
77 `${cst} initTokens = {};`,
78 `${RuntimeGlobals.initializeSharing} = ${runtimeTemplate.basicFunction(
79 "name, initScope",
80 [
81 "if(!initScope) initScope = [];",
82 "// handling circular init calls",
83 `${lt} initToken = initTokens[name];`,
84 "if(!initToken) initToken = initTokens[name] = {};",
85 "if(initScope.indexOf(initToken) >= 0) return;",
86 "initScope.push(initToken);",
87 "// only runs once",

Callers

nothing calls this directly

Calls 15

compareStringsFunction · 0.85
renderConstMethod · 0.80
renderLetMethod · 0.80
basicFunctionMethod · 0.80
assignOrMethod · 0.80
expressionFunctionMethod · 0.80
indentMethod · 0.80
returningFunctionMethod · 0.80
optionalChainingMethod · 0.80
sortMethod · 0.80

Tested by

no test coverage detected