StartupHelpers.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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("../Chunk").ChunkId} ChunkId */
  13. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  14. /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
  15. /** @typedef {import("../Entrypoint")} Entrypoint */
  16. /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
  17. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  18. const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `;
  19. /** @typedef {Set<Chunk>} Chunks */
  20. /** @typedef {ModuleId[]} ModuleIds */
  21. /**
  22. * Returns runtime code.
  23. * @param {ChunkGraph} chunkGraph chunkGraph
  24. * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
  25. * @param {EntryModuleWithChunkGroup[]} entries entries
  26. * @param {Chunk} chunk chunk
  27. * @param {boolean} passive true: passive startup with on chunks loaded
  28. * @returns {string} runtime code
  29. */
  30. module.exports.generateEntryStartup = (
  31. chunkGraph,
  32. runtimeTemplate,
  33. entries,
  34. chunk,
  35. passive
  36. ) => {
  37. /** @type {string[]} */
  38. const runtime = [
  39. `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
  40. `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`,
  41. "moduleId"
  42. )}`
  43. ];
  44. /**
  45. * Returns fn to execute.
  46. * @param {ModuleId} id id
  47. * @returns {string} fn to execute
  48. */
  49. const runModule = (id) => `__webpack_exec__(${JSON.stringify(id)})`;
  50. /**
  51. * Output combination.
  52. * @param {Chunks} chunks chunks
  53. * @param {ModuleIds} moduleIds module ids
  54. * @param {boolean=} final true when final, otherwise false
  55. */
  56. const outputCombination = (chunks, moduleIds, final) => {
  57. if (chunks.size === 0) {
  58. runtime.push(
  59. `${final ? EXPORT_PREFIX : ""}(${moduleIds.map(runModule).join(", ")});`
  60. );
  61. } else {
  62. const fn = runtimeTemplate.returningFunction(
  63. moduleIds.map(runModule).join(", ")
  64. );
  65. runtime.push(
  66. `${final && !passive ? EXPORT_PREFIX : ""}${
  67. passive
  68. ? RuntimeGlobals.onChunksLoaded
  69. : RuntimeGlobals.startupEntrypoint
  70. }(0, ${JSON.stringify(Array.from(chunks, (c) => c.id))}, ${fn});`
  71. );
  72. if (final && passive) {
  73. runtime.push(`${EXPORT_PREFIX}${RuntimeGlobals.onChunksLoaded}();`);
  74. }
  75. }
  76. };
  77. /** @type {Chunks | undefined} */
  78. let currentChunks;
  79. /** @type {ModuleIds | undefined} */
  80. let currentModuleIds;
  81. for (const [module, entrypoint] of entries) {
  82. if (!chunkGraph.getModuleSourceTypes(module).has("javascript")) {
  83. continue;
  84. }
  85. const runtimeChunk =
  86. /** @type {Entrypoint} */
  87. (entrypoint).getRuntimeChunk();
  88. const moduleId = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  89. const chunks = getAllChunks(
  90. /** @type {Entrypoint} */
  91. (entrypoint),
  92. chunk,
  93. runtimeChunk
  94. );
  95. if (
  96. currentChunks &&
  97. currentChunks.size === chunks.size &&
  98. isSubset(currentChunks, chunks)
  99. ) {
  100. /** @type {ModuleIds} */
  101. (currentModuleIds).push(moduleId);
  102. } else {
  103. if (currentChunks) {
  104. outputCombination(
  105. currentChunks,
  106. /** @type {ModuleIds} */ (currentModuleIds)
  107. );
  108. }
  109. currentChunks = chunks;
  110. currentModuleIds = [moduleId];
  111. }
  112. }
  113. // output current modules with export prefix
  114. if (currentChunks) {
  115. outputCombination(
  116. currentChunks,
  117. /** @type {ModuleIds} */
  118. (currentModuleIds),
  119. true
  120. );
  121. }
  122. runtime.push("");
  123. return Template.asString(runtime);
  124. };
  125. /**
  126. * Returns initially fulfilled chunk ids.
  127. * @param {Chunk} chunk the chunk
  128. * @param {ChunkGraph} chunkGraph the chunk graph
  129. * @param {(chunk: Chunk, chunkGraph: ChunkGraph) => boolean} filterFn filter function
  130. * @returns {Set<ChunkId>} initially fulfilled chunk ids
  131. */
  132. module.exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
  133. /** @type {Set<ChunkId>} */
  134. const initialChunkIds = new Set(chunk.ids);
  135. for (const c of chunk.getAllInitialChunks()) {
  136. if (c === chunk || filterFn(c, chunkGraph)) continue;
  137. for (const id of /** @type {ChunkId[]} */ (c.ids)) {
  138. initialChunkIds.add(id);
  139. }
  140. }
  141. return initialChunkIds;
  142. };
  143. /**
  144. * Processes the provided hash.
  145. * @param {Hash} hash the hash to update
  146. * @param {ChunkGraph} chunkGraph chunkGraph
  147. * @param {EntryModuleWithChunkGroup[]} entries entries
  148. * @param {Chunk} chunk chunk
  149. * @returns {void}
  150. */
  151. module.exports.updateHashForEntryStartup = (
  152. hash,
  153. chunkGraph,
  154. entries,
  155. chunk
  156. ) => {
  157. for (const [module, entrypoint] of entries) {
  158. const runtimeChunk =
  159. /** @type {Entrypoint} */
  160. (entrypoint).getRuntimeChunk();
  161. const moduleId = chunkGraph.getModuleId(module);
  162. hash.update(`${moduleId}`);
  163. for (const c of getAllChunks(
  164. /** @type {Entrypoint} */ (entrypoint),
  165. chunk,
  166. /** @type {Chunk} */ (runtimeChunk)
  167. )) {
  168. hash.update(`${c.id}`);
  169. }
  170. }
  171. };