ChunkPrefetchFunctionRuntimeModule.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeModule = require("../RuntimeModule");
  6. const Template = require("../Template");
  7. /** @typedef {import("../Compilation")} Compilation */
  8. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  9. class ChunkPrefetchFunctionRuntimeModule extends RuntimeModule {
  10. /**
  11. * @param {string} childType TODO
  12. * @param {string} runtimeFunction TODO
  13. * @param {string} runtimeHandlers TODO
  14. */
  15. constructor(childType, runtimeFunction, runtimeHandlers) {
  16. super(`chunk ${childType} function`);
  17. this.childType = childType;
  18. this.runtimeFunction = runtimeFunction;
  19. this.runtimeHandlers = runtimeHandlers;
  20. }
  21. /**
  22. * @returns {string | null} runtime code
  23. */
  24. generate() {
  25. const { runtimeFunction, runtimeHandlers } = this;
  26. const compilation = /** @type {Compilation} */ (this.compilation);
  27. const { runtimeTemplate } = compilation;
  28. return Template.asString([
  29. `${runtimeHandlers} = {};`,
  30. `${runtimeFunction} = ${runtimeTemplate.basicFunction("chunkId", [
  31. // map is shorter than forEach
  32. `Object.keys(${runtimeHandlers}).map(${runtimeTemplate.basicFunction(
  33. "key",
  34. `${runtimeHandlers}[key](chunkId);`
  35. )});`
  36. ])}`
  37. ]);
  38. }
  39. }
  40. module.exports = ChunkPrefetchFunctionRuntimeModule;