* Generates runtime code for this runtime module. * @returns {string | null} runtime code
()
| 26 | * @returns {string | null} runtime code |
| 27 | */ |
| 28 | generate() { |
| 29 | const compilation = /** @type {Compilation} */ (this.compilation); |
| 30 | const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); |
| 31 | const { runtimeTemplate, moduleGraph } = compilation; |
| 32 | /** @type {Record<ChunkId, ModuleId[]>} */ |
| 33 | const chunkToRemotesMapping = {}; |
| 34 | /** @type {Record<ModuleId, [string, string, ModuleId]>} */ |
| 35 | const idToExternalAndNameMapping = {}; |
| 36 | for (const chunk of /** @type {Chunk} */ ( |
| 37 | this.chunk |
| 38 | ).getAllReferencedChunks()) { |
| 39 | const modules = chunkGraph.getChunkModulesIterableBySourceType( |
| 40 | chunk, |
| 41 | "remote" |
| 42 | ); |
| 43 | if (!modules) continue; |
| 44 | /** @type {ModuleId[]} */ |
| 45 | const remotes = (chunkToRemotesMapping[ |
| 46 | /** @type {ChunkId} */ |
| 47 | (chunk.id) |
| 48 | ] = []); |
| 49 | for (const m of modules) { |
| 50 | const module = /** @type {RemoteModule} */ (m); |
| 51 | const name = module.internalRequest; |
| 52 | const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module)); |
| 53 | const shareScope = module.shareScope; |
| 54 | const dep = module.dependencies[0]; |
| 55 | const externalModule = moduleGraph.getModule(dep); |
| 56 | const externalModuleId = |
| 57 | /** @type {ModuleId} */ |
| 58 | (externalModule && chunkGraph.getModuleId(externalModule)); |
| 59 | remotes.push(id); |
| 60 | idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId]; |
| 61 | } |
| 62 | } |
| 63 | const cst = runtimeTemplate.renderConst(); |
| 64 | const lt = runtimeTemplate.renderLet(); |
| 65 | return Template.asString([ |
| 66 | `${cst} chunkMapping = ${JSON.stringify( |
| 67 | chunkToRemotesMapping, |
| 68 | null, |
| 69 | "\t" |
| 70 | )};`, |
| 71 | `${cst} idToExternalAndNameMapping = ${JSON.stringify( |
| 72 | idToExternalAndNameMapping, |
| 73 | null, |
| 74 | "\t" |
| 75 | )};`, |
| 76 | `${ |
| 77 | RuntimeGlobals.ensureChunkHandlers |
| 78 | }.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [ |
| 79 | `if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`, |
| 80 | Template.indent([ |
| 81 | `chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [ |
| 82 | `${lt} getScope = ${RuntimeGlobals.currentRemoteGetScope};`, |
| 83 | "if(!getScope) getScope = [];", |
| 84 | `${cst} data = idToExternalAndNameMapping[id];`, |
| 85 | "if(getScope.indexOf(data) >= 0) return;", |
nothing calls this directly
no test coverage detected