StartupEntrypointRuntimeModule.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. /** @typedef {import("../Compilation")} Compilation */
  8. /** @typedef {import("../MainTemplate")} MainTemplate */
  9. class StartupEntrypointRuntimeModule extends RuntimeModule {
  10. /**
  11. * @param {boolean} asyncChunkLoading use async chunk loading
  12. */
  13. constructor(asyncChunkLoading) {
  14. super("startup entrypoint");
  15. this.asyncChunkLoading = asyncChunkLoading;
  16. }
  17. /**
  18. * @returns {string | null} runtime code
  19. */
  20. generate() {
  21. const compilation = /** @type {Compilation} */ (this.compilation);
  22. const { runtimeTemplate } = compilation;
  23. return `${
  24. RuntimeGlobals.startupEntrypoint
  25. } = ${runtimeTemplate.basicFunction("result, chunkIds, fn", [
  26. "// arguments: chunkIds, moduleId are deprecated",
  27. "var moduleId = chunkIds;",
  28. `if(!fn) chunkIds = result, fn = ${runtimeTemplate.returningFunction(
  29. `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`
  30. )};`,
  31. ...(this.asyncChunkLoading
  32. ? [
  33. `return Promise.all(chunkIds.map(${RuntimeGlobals.ensureChunk}, ${
  34. RuntimeGlobals.require
  35. })).then(${runtimeTemplate.basicFunction("", [
  36. "var r = fn();",
  37. "return r === undefined ? result : r;"
  38. ])})`
  39. ]
  40. : [
  41. `chunkIds.map(${RuntimeGlobals.ensureChunk}, ${RuntimeGlobals.require})`,
  42. "var r = fn();",
  43. "return r === undefined ? result : r;"
  44. ])
  45. ])}`;
  46. }
  47. }
  48. module.exports = StartupEntrypointRuntimeModule;