StartupHelpers.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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("../Compilation")} Compilation */
  14. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  15. /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
  16. /** @typedef {import("../Entrypoint")} Entrypoint */
  17. /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */
  18. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  19. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  20. /** @typedef {(string|number)[]} EntryItem */
  21. const EXPORT_PREFIX = `var ${RuntimeGlobals.exports} = `;
  22. /** @typedef {Set<Chunk>} Chunks */
  23. /** @typedef {ModuleId[]} ModuleIds */
  24. /**
  25. * @param {ChunkGraph} chunkGraph chunkGraph
  26. * @param {RuntimeTemplate} runtimeTemplate runtimeTemplate
  27. * @param {EntryModuleWithChunkGroup[]} entries entries
  28. * @param {Chunk} chunk chunk
  29. * @param {boolean} passive true: passive startup with on chunks loaded
  30. * @returns {string} runtime code
  31. */
  32. module.exports.generateEntryStartup = (
  33. chunkGraph,
  34. runtimeTemplate,
  35. entries,
  36. chunk,
  37. passive
  38. ) => {
  39. /** @type {string[]} */
  40. const runtime = [
  41. `var __webpack_exec__ = ${runtimeTemplate.returningFunction(
  42. `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`,
  43. "moduleId"
  44. )}`
  45. ];
  46. /**
  47. * @param {ModuleId} id id
  48. * @returns {string} fn to execute
  49. */
  50. const runModule = id => `__webpack_exec__(${JSON.stringify(id)})`;
  51. /**
  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. const runtimeChunk =
  83. /** @type {Entrypoint} */
  84. (entrypoint).getRuntimeChunk();
  85. const moduleId = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
  86. const chunks = getAllChunks(
  87. /** @type {Entrypoint} */
  88. (entrypoint),
  89. chunk,
  90. runtimeChunk
  91. );
  92. if (
  93. currentChunks &&
  94. currentChunks.size === chunks.size &&
  95. isSubset(currentChunks, chunks)
  96. ) {
  97. /** @type {ModuleIds} */
  98. (currentModuleIds).push(moduleId);
  99. } else {
  100. if (currentChunks) {
  101. outputCombination(
  102. currentChunks,
  103. /** @type {ModuleIds} */ (currentModuleIds)
  104. );
  105. }
  106. currentChunks = chunks;
  107. currentModuleIds = [moduleId];
  108. }
  109. }
  110. // output current modules with export prefix
  111. if (currentChunks) {
  112. outputCombination(
  113. currentChunks,
  114. /** @type {ModuleIds} */
  115. (currentModuleIds),
  116. true
  117. );
  118. }
  119. runtime.push("");
  120. return Template.asString(runtime);
  121. };
  122. /**
  123. * @param {Hash} hash the hash to update
  124. * @param {ChunkGraph} chunkGraph chunkGraph
  125. * @param {EntryModuleWithChunkGroup[]} entries entries
  126. * @param {Chunk} chunk chunk
  127. * @returns {void}
  128. */
  129. module.exports.updateHashForEntryStartup = (
  130. hash,
  131. chunkGraph,
  132. entries,
  133. chunk
  134. ) => {
  135. for (const [module, entrypoint] of entries) {
  136. const runtimeChunk =
  137. /** @type {Entrypoint} */
  138. (entrypoint).getRuntimeChunk();
  139. const moduleId = chunkGraph.getModuleId(module);
  140. hash.update(`${moduleId}`);
  141. for (const c of getAllChunks(
  142. /** @type {Entrypoint} */ (entrypoint),
  143. chunk,
  144. /** @type {Chunk} */ (runtimeChunk)
  145. )) {
  146. hash.update(`${c.id}`);
  147. }
  148. }
  149. };
  150. /**
  151. * @param {Chunk} chunk the chunk
  152. * @param {ChunkGraph} chunkGraph the chunk graph
  153. * @param {function(Chunk, ChunkGraph): boolean} filterFn filter function
  154. * @returns {Set<number | string>} initially fulfilled chunk ids
  155. */
  156. module.exports.getInitialChunkIds = (chunk, chunkGraph, filterFn) => {
  157. const initialChunkIds = new Set(chunk.ids);
  158. for (const c of chunk.getAllInitialChunks()) {
  159. if (c === chunk || filterFn(c, chunkGraph)) continue;
  160. for (const id of /** @type {ChunkId[]} */ (c.ids)) {
  161. initialChunkIds.add(id);
  162. }
  163. }
  164. return initialChunkIds;
  165. };