* Generates runtime code for this runtime module. * @returns {string | null} runtime code
()
| 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", |
nothing calls this directly
no test coverage detected