ChunkPrefetchTriggerRuntimeModule.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const RuntimeModule = require("../RuntimeModule");
  7. const Template = require("../Template");
  8. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  9. class ChunkPrefetchTriggerRuntimeModule extends RuntimeModule {
  10. /**
  11. * @param {Record<string|number, (string|number)[]>} chunkMap map from chunk to
  12. */
  13. constructor(chunkMap) {
  14. super(`chunk prefetch trigger`, RuntimeModule.STAGE_TRIGGER);
  15. this.chunkMap = chunkMap;
  16. }
  17. /**
  18. * @returns {string} runtime code
  19. */
  20. generate() {
  21. const { chunkMap } = this;
  22. const { runtimeTemplate } = this.compilation;
  23. const body = [
  24. "var chunks = chunkToChildrenMap[chunkId];",
  25. `Array.isArray(chunks) && chunks.map(${RuntimeGlobals.prefetchChunk});`
  26. ];
  27. return Template.asString([
  28. Template.asString([
  29. `var chunkToChildrenMap = ${JSON.stringify(chunkMap, null, "\t")};`,
  30. `${
  31. RuntimeGlobals.ensureChunkHandlers
  32. }.prefetch = ${runtimeTemplate.expressionFunction(
  33. `Promise.all(promises).then(${runtimeTemplate.basicFunction(
  34. "",
  35. body
  36. )})`,
  37. "chunkId, promises"
  38. )};`
  39. ])
  40. ]);
  41. }
  42. }
  43. module.exports = ChunkPrefetchTriggerRuntimeModule;