StartupEntrypointRuntimeModule.js 1.6 KB

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