ChunkPrefetchFunctionRuntimeModule.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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("../RuntimeTemplate")} RuntimeTemplate */
  8. class ChunkPrefetchFunctionRuntimeModule extends RuntimeModule {
  9. /**
  10. * @param {string} childType TODO
  11. * @param {string} runtimeFunction TODO
  12. * @param {string} runtimeHandlers TODO
  13. */
  14. constructor(childType, runtimeFunction, runtimeHandlers) {
  15. super(`chunk ${childType} function`);
  16. this.childType = childType;
  17. this.runtimeFunction = runtimeFunction;
  18. this.runtimeHandlers = runtimeHandlers;
  19. }
  20. /**
  21. * @returns {string} runtime code
  22. */
  23. generate() {
  24. const { runtimeFunction, runtimeHandlers } = this;
  25. const { runtimeTemplate } = this.compilation;
  26. return Template.asString([
  27. `${runtimeHandlers} = {};`,
  28. `${runtimeFunction} = ${runtimeTemplate.basicFunction("chunkId", [
  29. // map is shorter than forEach
  30. `Object.keys(${runtimeHandlers}).map(${runtimeTemplate.basicFunction(
  31. "key",
  32. `${runtimeHandlers}[key](chunkId);`
  33. )});`
  34. ])}`
  35. ]);
  36. }
  37. }
  38. module.exports = ChunkPrefetchFunctionRuntimeModule;