ChunkPreloadTriggerRuntimeModule.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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("../Compilation")} Compilation */
  9. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  10. class ChunkPreloadTriggerRuntimeModule extends RuntimeModule {
  11. /**
  12. * @param {Record<string|number, (string|number)[]>} chunkMap map from chunk to chunks
  13. */
  14. constructor(chunkMap) {
  15. super(`chunk preload trigger`, RuntimeModule.STAGE_TRIGGER);
  16. this.chunkMap = chunkMap;
  17. }
  18. /**
  19. * @returns {string | null} runtime code
  20. */
  21. generate() {
  22. const { chunkMap } = this;
  23. const compilation = /** @type {Compilation} */ (this.compilation);
  24. const { runtimeTemplate } = compilation;
  25. const body = [
  26. "var chunks = chunkToChildrenMap[chunkId];",
  27. `Array.isArray(chunks) && chunks.map(${RuntimeGlobals.preloadChunk});`
  28. ];
  29. return Template.asString([
  30. Template.asString([
  31. `var chunkToChildrenMap = ${JSON.stringify(chunkMap, null, "\t")};`,
  32. `${
  33. RuntimeGlobals.ensureChunkHandlers
  34. }.preload = ${runtimeTemplate.basicFunction("chunkId", body)};`
  35. ])
  36. ]);
  37. }
  38. }
  39. module.exports = ChunkPreloadTriggerRuntimeModule;