* Returns expression. * @param {object} options options * @param {AsyncDependenciesBlock | undefined} options.block the async block * @param {string} options.message the message * @param {ChunkGraph} options.chunkGraph the chunk graph * @param {RuntimeRequirements} options.runtimeRequireme
({ block, message, chunkGraph, runtimeRequirements })
| 1449 | * @returns {string} expression |
| 1450 | */ |
| 1451 | blockPromise({ block, message, chunkGraph, runtimeRequirements }) { |
| 1452 | if (!block) { |
| 1453 | const comment = this.comment({ |
| 1454 | message |
| 1455 | }); |
| 1456 | return `Promise.resolve(${comment.trim()})`; |
| 1457 | } |
| 1458 | const chunkGroup = chunkGraph.getBlockChunkGroup(block); |
| 1459 | if (!chunkGroup || chunkGroup.chunks.length === 0) { |
| 1460 | const comment = this.comment({ |
| 1461 | message |
| 1462 | }); |
| 1463 | return `Promise.resolve(${comment.trim()})`; |
| 1464 | } |
| 1465 | const chunks = chunkGroup.chunks.filter( |
| 1466 | (chunk) => !chunk.hasRuntime() && chunk.id !== null |
| 1467 | ); |
| 1468 | const comment = this.comment({ |
| 1469 | message, |
| 1470 | chunkName: block.chunkName |
| 1471 | }); |
| 1472 | if (chunks.length === 1) { |
| 1473 | const chunkId = JSON.stringify(chunks[0].id); |
| 1474 | runtimeRequirements.add(RuntimeGlobals.ensureChunk); |
| 1475 | |
| 1476 | const fetchPriority = chunkGroup.options.fetchPriority; |
| 1477 | |
| 1478 | if (fetchPriority) { |
| 1479 | runtimeRequirements.add(RuntimeGlobals.hasFetchPriority); |
| 1480 | } |
| 1481 | |
| 1482 | return `${RuntimeGlobals.ensureChunk}(${comment}${chunkId}${ |
| 1483 | fetchPriority ? `, ${JSON.stringify(fetchPriority)}` : "" |
| 1484 | })`; |
| 1485 | } else if (chunks.length > 0) { |
| 1486 | runtimeRequirements.add(RuntimeGlobals.ensureChunk); |
| 1487 | |
| 1488 | const fetchPriority = chunkGroup.options.fetchPriority; |
| 1489 | |
| 1490 | if (fetchPriority) { |
| 1491 | runtimeRequirements.add(RuntimeGlobals.hasFetchPriority); |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * Returns require chunk id code. |
| 1496 | * @param {Chunk} chunk chunk |
| 1497 | * @returns {string} require chunk id code |
| 1498 | */ |
| 1499 | const requireChunkId = (chunk) => |
| 1500 | `${RuntimeGlobals.ensureChunk}(${JSON.stringify(chunk.id)}${ |
| 1501 | fetchPriority ? `, ${JSON.stringify(fetchPriority)}` : "" |
| 1502 | })`; |
| 1503 | return `Promise.all(${comment.trim()}[${chunks |
| 1504 | .map(requireChunkId) |
| 1505 | .join(", ")}])`; |
| 1506 | } |
| 1507 | return `Promise.resolve(${comment.trim()})`; |
| 1508 | } |
no test coverage detected