ChunkPreloadTriggerRuntimeModule.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 ChunkPreloadTriggerRuntimeModule extends RuntimeModule {
  10. /**
  11. * @param {Record<string|number, (string|number)[]>} chunkMap map from chunk to chunks
  12. */
  13. constructor(chunkMap) {
  14. super(`chunk preload 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.preloadChunk});`
  26. ];
  27. return Template.asString([
  28. Template.asString([
  29. `var chunkToChildrenMap = ${JSON.stringify(chunkMap, null, "\t")};`,
  30. `${
  31. RuntimeGlobals.ensureChunkHandlers
  32. }.preload = ${runtimeTemplate.basicFunction("chunkId", body)};`
  33. ])
  34. ]);
  35. }
  36. }
  37. module.exports = ChunkPreloadTriggerRuntimeModule;