StartupEntrypointRuntimeModule.js 1.4 KB

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