InitFragment.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const { ConcatSource } = require("webpack-sources");
  7. const makeSerializable = require("./util/makeSerializable");
  8. /** @typedef {import("webpack-sources").Source} Source */
  9. /** @typedef {import("./Generator").GenerateContext} GenerateContext */
  10. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  11. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  12. /**
  13. * @template T
  14. * @param {InitFragment<T>} fragment the init fragment
  15. * @param {number} index index
  16. * @returns {[InitFragment<T>, number]} tuple with both
  17. */
  18. const extractFragmentIndex = (fragment, index) => [fragment, index];
  19. /**
  20. * @template T
  21. * @param {[InitFragment<T>, number]} a first pair
  22. * @param {[InitFragment<T>, number]} b second pair
  23. * @returns {number} sort value
  24. */
  25. const sortFragmentWithIndex = ([a, i], [b, j]) => {
  26. const stageCmp = a.stage - b.stage;
  27. if (stageCmp !== 0) return stageCmp;
  28. const positionCmp = a.position - b.position;
  29. if (positionCmp !== 0) return positionCmp;
  30. return i - j;
  31. };
  32. /**
  33. * @template GenerateContext
  34. */
  35. class InitFragment {
  36. /**
  37. * @param {string | Source | undefined} content the source code that will be included as initialization code
  38. * @param {number} stage category of initialization code (contribute to order)
  39. * @param {number} position position in the category (contribute to order)
  40. * @param {string=} key unique key to avoid emitting the same initialization code twice
  41. * @param {string | Source=} endContent the source code that will be included at the end of the module
  42. */
  43. constructor(content, stage, position, key, endContent) {
  44. this.content = content;
  45. this.stage = stage;
  46. this.position = position;
  47. this.key = key;
  48. this.endContent = endContent;
  49. }
  50. /**
  51. * @param {GenerateContext} context context
  52. * @returns {string | Source | undefined} the source code that will be included as initialization code
  53. */
  54. getContent(context) {
  55. return this.content;
  56. }
  57. /**
  58. * @param {GenerateContext} context context
  59. * @returns {string|Source=} the source code that will be included at the end of the module
  60. */
  61. getEndContent(context) {
  62. return this.endContent;
  63. }
  64. /**
  65. * @template Context
  66. * @template T
  67. * @param {Source} source sources
  68. * @param {InitFragment<T>[]} initFragments init fragments
  69. * @param {Context} context context
  70. * @returns {Source} source
  71. */
  72. static addToSource(source, initFragments, context) {
  73. if (initFragments.length > 0) {
  74. // Sort fragments by position. If 2 fragments have the same position,
  75. // use their index.
  76. const sortedFragments = initFragments
  77. .map(extractFragmentIndex)
  78. .sort(sortFragmentWithIndex);
  79. // Deduplicate fragments. If a fragment has no key, it is always included.
  80. const keyedFragments = new Map();
  81. for (const [fragment] of sortedFragments) {
  82. if (
  83. typeof (
  84. /** @type {InitFragment<T> & { mergeAll?: (fragments: InitFragment<Context>[]) => InitFragment<Context>[] }} */
  85. (fragment).mergeAll
  86. ) === "function"
  87. ) {
  88. if (!fragment.key) {
  89. throw new Error(
  90. `InitFragment with mergeAll function must have a valid key: ${fragment.constructor.name}`
  91. );
  92. }
  93. const oldValue = keyedFragments.get(fragment.key);
  94. if (oldValue === undefined) {
  95. keyedFragments.set(fragment.key, fragment);
  96. } else if (Array.isArray(oldValue)) {
  97. oldValue.push(fragment);
  98. } else {
  99. keyedFragments.set(fragment.key, [oldValue, fragment]);
  100. }
  101. continue;
  102. } else if (typeof fragment.merge === "function") {
  103. const oldValue = keyedFragments.get(fragment.key);
  104. if (oldValue !== undefined) {
  105. keyedFragments.set(fragment.key, fragment.merge(oldValue));
  106. continue;
  107. }
  108. }
  109. keyedFragments.set(fragment.key || Symbol(), fragment);
  110. }
  111. const concatSource = new ConcatSource();
  112. const endContents = [];
  113. for (let fragment of keyedFragments.values()) {
  114. if (Array.isArray(fragment)) {
  115. fragment = fragment[0].mergeAll(fragment);
  116. }
  117. concatSource.add(fragment.getContent(context));
  118. const endContent = fragment.getEndContent(context);
  119. if (endContent) {
  120. endContents.push(endContent);
  121. }
  122. }
  123. concatSource.add(source);
  124. for (const content of endContents.reverse()) {
  125. concatSource.add(content);
  126. }
  127. return concatSource;
  128. } else {
  129. return source;
  130. }
  131. }
  132. /**
  133. * @param {ObjectSerializerContext} context context
  134. */
  135. serialize(context) {
  136. const { write } = context;
  137. write(this.content);
  138. write(this.stage);
  139. write(this.position);
  140. write(this.key);
  141. write(this.endContent);
  142. }
  143. /**
  144. * @param {ObjectDeserializerContext} context context
  145. */
  146. deserialize(context) {
  147. const { read } = context;
  148. this.content = read();
  149. this.stage = read();
  150. this.position = read();
  151. this.key = read();
  152. this.endContent = read();
  153. }
  154. }
  155. makeSerializable(InitFragment, "webpack/lib/InitFragment");
  156. InitFragment.prototype.merge =
  157. /** @type {TODO} */
  158. (undefined);
  159. InitFragment.STAGE_CONSTANTS = 10;
  160. InitFragment.STAGE_ASYNC_BOUNDARY = 20;
  161. InitFragment.STAGE_HARMONY_EXPORTS = 30;
  162. InitFragment.STAGE_HARMONY_IMPORTS = 40;
  163. InitFragment.STAGE_PROVIDES = 50;
  164. InitFragment.STAGE_ASYNC_DEPENDENCIES = 60;
  165. InitFragment.STAGE_ASYNC_HARMONY_IMPORTS = 70;
  166. module.exports = InitFragment;