StartupHelpers.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const Template = require("../Template");
  8. const { isSubset } = require("../util/SetHelpers");
  9. const { getAllChunks } = require("./ChunkHelpers");
  10. /** @typedef {import("../util/Hash")} Hash */
  11. /** @typedef {import("../Chunk")} Chunk */
  12. /** @typedef {import("../Compilation")} Compilation */
  13. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  14. /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
  15. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  16. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  17. /** @typedef {(string|number)[]} EntryItem */
  18. const EXPORT_PREFIX = "var __webpack_exports__ = ";
  19. /**
  20. * @param {ChunkGraph} chunkGraph chunkGraph
  21. * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
  22. * @param {EntryModuleWithChunkGroup[]} entries entries
  23. * @param {Chunk} chunk chunk
  24. * @param {boolean} passive true: passive startup with on chunks loaded
  25. * @returns {string} runtime code
  26. */
  27. exports.generateEntryStartup = (
  28. chunkGraph,
  29. runtimeTemplate,
  30. entries,
  31. chunk,
  32. passive
  33. ) => {
  34. /** @type {string[]} */
  35. const runtime = [
  36. `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
  37. `__webpack_require__(${RuntimeGlobals.entryModuleId} = moduleId)`,
  38. "moduleId"
  39. )}`
  40. ];
  41. const runModule = id => {
  42. return `__webpack_exec__(${JSON.stringify(id)})`;
  43. };
  44. const outputCombination = (chunks, moduleIds, final) => {
  45. if (chunks.size === 0) {
  46. runtime.push(
  47. `${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});`
  48. );
  49. } else {
  50. const fn = runtimeTemplate.returningFunction(
  51. moduleIds.map(runModule).join(", ")
  52. );
  53. runtime.push(
  54. `${final && !passive ? EXPORT_PREFIX : ""}${
  55. passive
  56. ? RuntimeGlobals.onChunksLoaded
  57. : RuntimeGlobals.startupEntrypoint
  58. }(0, ${JSON.stringify(Array.from(chunks, c => c.id))}, ${fn});`
  59. );
  60. if (final && passive) {
  61. runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`);
  62. }
  63. }
  64. };
  65. let currentChunks = undefined;
  66. let currentModuleIds = undefined;
  67. for (const [module, entrypoint] of entries) {
  68. const runtimeChunk = entrypoint.getRuntimeChunk();
  69. const moduleId = chunkGraph.getModuleId(module);
  70. const chunks = getAllChunks(entrypoint, chunk, runtimeChunk);
  71. if (
  72. currentChunks &&
  73. currentChunks.size === chunks.size &&
  74. isSubset(currentChunks, chunks)
  75. ) {
  76. currentModuleIds.push(moduleId);
  77. } else {
  78. if (currentChunks) {
  79. outputCombination(currentChunks, currentModuleIds);
  80. }
  81. currentChunks = chunks;
  82. currentModuleIds = [moduleId];
  83. }
  84. }
  85. // output current modules with export prefix
  86. if (currentChunks) {
  87. outputCombination(currentChunks, currentModuleIds, true);
  88. }
  89. runtime.push("");
  90. return Template.asString(runtime);
  91. };
  92. /**
  93. * @param {Hash} hash the hash to update
  94. * @param {ChunkGraph} chunkGraph chunkGraph
  95. * @param {EntryModuleWithChunkGroup[]} entries entries
  96. * @param {Chunk} chunk chunk
  97. * @returns {void}
  98. */
  99. exports.updateHashForEntryStartup = (hash, chunkGraph, entries, chunk) => {
  100. for (const [module, entrypoint] of entries) {
  101. const runtimeChunk = entrypoint.getRuntimeChunk();
  102. const moduleId = chunkGraph.getModuleId(module);
  103. hash.update(`${moduleId}`);
  104. for (const c of getAllChunks(entrypoint, chunk, runtimeChunk))
  105. hash.update(`${c.id}`);
  106. }
  107. };
  108. /**
  109. * @param {Chunk} chunk the chunk
  110. * @param {ChunkGraph} chunkGraph the chunk graph
  111. * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function
  112. * @returns {Set<number | string>} initially fulfilled chunk ids
  113. */
  114. exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
  115. const initialChunkIds = new Set(chunk.ids);
  116. for (const c of chunk.getAllInitialChunks()) {
  117. if (c === chunk || filterFn(c, chunkGraph)) continue;
  118. for (const id of c.ids) initialChunkIds.add(id);
  119. }
  120. return initialChunkIds;
  121. };