* Renders chunk modules. * @param {ChunkRenderContext} renderContext render context * @param {Module[]} modules modules to render (should be ordered by identifier) * @param {(module: Module, renderInArray?: boolean) => Source | null} renderModule function to render a module * @param {string=
(renderContext, modules, renderModule, prefix = "")
| 313 | * @returns {Source | null} rendered chunk modules in a Source object or null if no modules |
| 314 | */ |
| 315 | static renderChunkModules(renderContext, modules, renderModule, prefix = "") { |
| 316 | const { chunkGraph } = renderContext; |
| 317 | const source = new ConcatSource(); |
| 318 | if (modules.length === 0) { |
| 319 | return null; |
| 320 | } |
| 321 | /** @type {{ id: ModuleId, module: Module }[]} */ |
| 322 | const modulesWithId = modules.map((m) => ({ |
| 323 | id: /** @type {ModuleId} */ (chunkGraph.getModuleId(m)), |
| 324 | module: m |
| 325 | })); |
| 326 | const bounds = Template.getModulesArrayBounds(modulesWithId); |
| 327 | const renderInObject = bounds === false; |
| 328 | |
| 329 | /** @type {{ id: ModuleId, source: Source | "false" }[]} */ |
| 330 | const allModules = modulesWithId.map(({ id, module }) => ({ |
| 331 | id, |
| 332 | source: renderModule(module, renderInObject) || "false" |
| 333 | })); |
| 334 | |
| 335 | if (bounds) { |
| 336 | // Render a spare array |
| 337 | const minId = bounds[0]; |
| 338 | const maxId = bounds[1]; |
| 339 | if (minId !== 0) { |
| 340 | source.add(`Array(${minId}).concat(`); |
| 341 | } |
| 342 | source.add("[\n"); |
| 343 | /** @type {Map<ModuleId, { id: ModuleId, source: Source | "false" }>} */ |
| 344 | const modules = new Map(); |
| 345 | for (const module of allModules) { |
| 346 | modules.set(module.id, module); |
| 347 | } |
| 348 | for (let idx = minId; idx <= maxId; idx++) { |
| 349 | const module = modules.get(idx); |
| 350 | if (idx !== minId) { |
| 351 | source.add(",\n"); |
| 352 | } |
| 353 | source.add(`/* ${idx} */`); |
| 354 | if (module) { |
| 355 | source.add("\n"); |
| 356 | source.add(module.source); |
| 357 | } |
| 358 | } |
| 359 | source.add(`\n${prefix}]`); |
| 360 | if (minId !== 0) { |
| 361 | source.add(")"); |
| 362 | } |
| 363 | } else { |
| 364 | // Render an object |
| 365 | source.add("{\n"); |
| 366 | for (let i = 0; i < allModules.length; i++) { |
| 367 | const module = allModules[i]; |
| 368 | if (i !== 0) { |
| 369 | source.add(",\n"); |
| 370 | } |
| 371 | source.add( |
| 372 | `\n/***/ ${JSON.stringify(module.id)}${renderContext.runtimeTemplate.supportsMethodShorthand() && module.source !== "false" ? "" : ":"}\n` |
no test coverage detected