metro 0.83.3 → 0.83.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/package.json +28 -24
  2. package/src/Assets.d.ts +83 -0
  3. package/src/Assets.js +42 -29
  4. package/src/Assets.js.flow +26 -15
  5. package/src/Bundler/util.d.ts +27 -0
  6. package/src/Bundler/util.js +25 -21
  7. package/src/Bundler/util.js.flow +2 -2
  8. package/src/Bundler.d.ts +14 -15
  9. package/src/Bundler.js.flow +1 -1
  10. package/src/DeltaBundler/DeltaCalculator.d.ts +71 -0
  11. package/src/DeltaBundler/DeltaCalculator.js +4 -4
  12. package/src/DeltaBundler/DeltaCalculator.js.flow +8 -8
  13. package/src/DeltaBundler/Graph.d.ts +135 -8
  14. package/src/DeltaBundler/Graph.js +16 -16
  15. package/src/DeltaBundler/Graph.js.flow +30 -30
  16. package/src/DeltaBundler/Serializers/baseJSBundle.d.ts +20 -0
  17. package/src/DeltaBundler/Serializers/baseJSBundle.js.flow +1 -1
  18. package/src/DeltaBundler/Serializers/getAllFiles.d.ts +22 -0
  19. package/src/DeltaBundler/Serializers/getAllFiles.js.flow +2 -2
  20. package/src/DeltaBundler/Serializers/getAssets.d.ts +25 -0
  21. package/src/DeltaBundler/Serializers/getAssets.js.flow +2 -2
  22. package/src/DeltaBundler/Serializers/getExplodedSourceMap.js.flow +2 -2
  23. package/src/DeltaBundler/Serializers/getRamBundleInfo.d.ts +36 -6
  24. package/src/DeltaBundler/Serializers/getRamBundleInfo.js.flow +8 -8
  25. package/src/DeltaBundler/Serializers/helpers/getInlineSourceMappingURL.d.ts +12 -0
  26. package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.d.ts +33 -0
  27. package/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.d.ts +17 -0
  28. package/src/DeltaBundler/Serializers/helpers/js.d.ts +30 -0
  29. package/src/DeltaBundler/Serializers/helpers/js.js +25 -21
  30. package/src/DeltaBundler/Serializers/helpers/js.js.flow +6 -6
  31. package/src/DeltaBundler/Serializers/helpers/processModules.d.ts +25 -0
  32. package/src/DeltaBundler/Serializers/helpers/processModules.js.flow +3 -3
  33. package/src/DeltaBundler/Serializers/hmrJSBundle.d.ts +30 -0
  34. package/src/DeltaBundler/Serializers/hmrJSBundle.js +25 -21
  35. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +5 -5
  36. package/src/DeltaBundler/Serializers/sourceMapGenerator.d.ts +29 -0
  37. package/src/DeltaBundler/Serializers/sourceMapGenerator.js.flow +6 -6
  38. package/src/DeltaBundler/Serializers/sourceMapObject.d.ts +23 -0
  39. package/src/DeltaBundler/Serializers/sourceMapObject.js.flow +2 -2
  40. package/src/DeltaBundler/Serializers/sourceMapString.d.ts +22 -0
  41. package/src/DeltaBundler/Serializers/sourceMapString.js.flow +2 -2
  42. package/src/DeltaBundler/Transformer.d.ts +38 -0
  43. package/src/DeltaBundler/Transformer.js.flow +3 -3
  44. package/src/DeltaBundler/Worker.d.ts +19 -27
  45. package/src/DeltaBundler/Worker.flow.js.flow +1 -1
  46. package/src/DeltaBundler/WorkerFarm.d.ts +70 -0
  47. package/src/DeltaBundler/WorkerFarm.js +1 -1
  48. package/src/DeltaBundler/WorkerFarm.js.flow +26 -13
  49. package/src/DeltaBundler/buildSubgraph.d.ts +30 -0
  50. package/src/DeltaBundler/buildSubgraph.js +4 -4
  51. package/src/DeltaBundler/buildSubgraph.js.flow +8 -8
  52. package/src/DeltaBundler/getTransformCacheKey.d.ts +18 -0
  53. package/src/DeltaBundler/mergeDeltas.d.ts +17 -0
  54. package/src/DeltaBundler/types.d.ts +97 -100
  55. package/src/DeltaBundler/types.js.flow +36 -34
  56. package/src/DeltaBundler.d.ts +14 -12
  57. package/src/DeltaBundler.js.flow +2 -2
  58. package/src/HmrServer.d.ts +79 -0
  59. package/src/HmrServer.js +34 -29
  60. package/src/HmrServer.js.flow +17 -12
  61. package/src/IncrementalBundler/GraphNotFoundError.d.ts +17 -0
  62. package/src/IncrementalBundler/ResourceNotFoundError.d.ts +15 -0
  63. package/src/IncrementalBundler/RevisionNotFoundError.d.ts +17 -0
  64. package/src/IncrementalBundler.d.ts +43 -45
  65. package/src/IncrementalBundler.js +29 -21
  66. package/src/IncrementalBundler.js.flow +13 -9
  67. package/src/ModuleGraph/worker/JsFileWrapping.d.ts +29 -0
  68. package/src/ModuleGraph/worker/JsFileWrapping.js +25 -21
  69. package/src/ModuleGraph/worker/JsFileWrapping.js.flow +10 -5
  70. package/src/ModuleGraph/worker/collectDependencies.d.ts +118 -14
  71. package/src/ModuleGraph/worker/collectDependencies.js +25 -21
  72. package/src/ModuleGraph/worker/collectDependencies.js.flow +27 -21
  73. package/src/ModuleGraph/worker/generateImportNames.d.ts +19 -0
  74. package/src/ModuleGraph/worker/generateImportNames.js.flow +4 -2
  75. package/src/ModuleGraph/worker/importLocationsPlugin.d.ts +26 -0
  76. package/src/ModuleGraph/worker/importLocationsPlugin.js.flow +7 -3
  77. package/src/Server/MultipartResponse.d.ts +5 -6
  78. package/src/Server/MultipartResponse.js.flow +1 -1
  79. package/src/Server/symbolicate.js.flow +4 -4
  80. package/src/Server.d.ts +220 -67
  81. package/src/Server.js +149 -45
  82. package/src/Server.js.flow +167 -57
  83. package/src/cli/parseKeyValueParamArray.d.ts +14 -0
  84. package/src/cli/parseKeyValueParamArray.js.flow +1 -1
  85. package/src/cli-utils.d.ts +19 -0
  86. package/src/cli-utils.js.flow +2 -2
  87. package/src/commands/build.d.ts +16 -0
  88. package/src/commands/build.js.flow +11 -10
  89. package/src/commands/dependencies.d.ts +16 -0
  90. package/src/commands/dependencies.js.flow +8 -4
  91. package/src/commands/serve.d.ts +16 -0
  92. package/src/commands/serve.js +2 -0
  93. package/src/commands/serve.js.flow +14 -9
  94. package/src/index.d.ts +97 -110
  95. package/src/index.flow.js +53 -35
  96. package/src/index.flow.js.flow +49 -29
  97. package/src/integration_tests/basic_bundle/AssetRegistry.js.flow +1 -1
  98. package/src/integration_tests/basic_bundle/ErrorBundle.js.flow +1 -1
  99. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-import.js.flow +1 -1
  100. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-multi-line-import-with-escapes.js.flow +1 -1
  101. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-multi-line-import.js.flow +1 -1
  102. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-require-with-embedded-comment.js.flow +1 -1
  103. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-require.js.flow +1 -1
  104. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-specifier-with-escapes.js.flow +1 -1
  105. package/src/integration_tests/basic_bundle/build-errors/inline-requires-cannot-resolve-import.js.flow +1 -1
  106. package/src/integration_tests/basic_bundle/build-errors/inline-requires-cannot-resolve-require.js.flow +1 -1
  107. package/src/integration_tests/basic_bundle/import-export/index.js +25 -21
  108. package/src/integration_tests/basic_bundle/import-export/index.js.flow +3 -3
  109. package/src/integration_tests/basic_bundle/import-export/utils.js.flow +2 -2
  110. package/src/integration_tests/basic_bundle/loadBundleAsyncForTest.js.flow +1 -1
  111. package/src/integration_tests/basic_bundle/optional-dependencies/index.js.flow +1 -1
  112. package/src/integration_tests/basic_bundle/require-context/conflict.js.flow +1 -1
  113. package/src/integration_tests/basic_bundle/require-context/empty.js.flow +1 -1
  114. package/src/integration_tests/basic_bundle/require-context/matching.js.flow +1 -1
  115. package/src/integration_tests/basic_bundle/require-context/mode-eager.js.flow +1 -1
  116. package/src/integration_tests/basic_bundle/require-context/mode-lazy-once.js.flow +1 -1
  117. package/src/integration_tests/basic_bundle/require-context/mode-lazy.js.flow +1 -1
  118. package/src/integration_tests/basic_bundle/require-context/mode-sync.js.flow +2 -2
  119. package/src/integration_tests/basic_bundle/require-context/utils.js.flow +1 -1
  120. package/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js.flow +1 -1
  121. package/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js.flow +1 -1
  122. package/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js.flow +1 -1
  123. package/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js.flow +1 -1
  124. package/src/integration_tests/execBundle.js.flow +1 -1
  125. package/src/lib/BatchProcessor.d.ts +54 -0
  126. package/src/lib/BatchProcessor.js +5 -2
  127. package/src/lib/BatchProcessor.js.flow +10 -7
  128. package/src/lib/CountingSet.d.ts +10 -10
  129. package/src/lib/CountingSet.js.flow +4 -4
  130. package/src/lib/JsonReporter.d.ts +37 -0
  131. package/src/lib/JsonReporter.js +5 -3
  132. package/src/lib/JsonReporter.js.flow +19 -17
  133. package/src/lib/RamBundleParser.d.ts +29 -0
  134. package/src/lib/RamBundleParser.js.flow +1 -1
  135. package/src/lib/TerminalReporter.d.ts +97 -6
  136. package/src/lib/TerminalReporter.js +37 -34
  137. package/src/lib/TerminalReporter.js.flow +21 -30
  138. package/src/lib/bundleProgressUtils.d.ts +26 -0
  139. package/src/lib/bundleProgressUtils.js +19 -0
  140. package/src/lib/bundleProgressUtils.js.flow +35 -0
  141. package/src/lib/bundleToString.d.ts +19 -0
  142. package/src/lib/contextModule.d.ts +20 -9
  143. package/src/lib/contextModule.js.flow +1 -1
  144. package/src/lib/contextModuleTemplates.d.ts +25 -0
  145. package/src/lib/contextModuleTemplates.js +25 -21
  146. package/src/lib/countLines.d.ts +12 -0
  147. package/src/lib/countLines.js +4 -3
  148. package/src/lib/countLines.js.flow +3 -4
  149. package/src/lib/createWebsocketServer.d.ts +43 -0
  150. package/src/lib/createWebsocketServer.js +9 -2
  151. package/src/lib/createWebsocketServer.js.flow +16 -9
  152. package/src/lib/debounceAsyncQueue.d.ts +15 -0
  153. package/src/lib/debounceAsyncQueue.js.flow +1 -1
  154. package/src/lib/formatBundlingError.d.ts +23 -0
  155. package/src/lib/formatBundlingError.js.flow +1 -1
  156. package/src/lib/getAppendScripts.d.ts +31 -0
  157. package/src/lib/getAppendScripts.js.flow +4 -4
  158. package/src/lib/getGraphId.d.ts +15 -1
  159. package/src/lib/getGraphId.js.flow +1 -1
  160. package/src/lib/getPreludeCode.d.ts +18 -0
  161. package/src/lib/getPreludeCode.js +4 -0
  162. package/src/lib/getPreludeCode.js.flow +10 -3
  163. package/src/lib/getPrependedScripts.d.ts +24 -0
  164. package/src/lib/getPrependedScripts.js +36 -22
  165. package/src/lib/getPrependedScripts.js.flow +10 -3
  166. package/src/lib/isResolvedDependency.d.ts +15 -0
  167. package/src/lib/logToConsole.d.ts +19 -0
  168. package/src/lib/logToConsole.js.flow +2 -2
  169. package/src/lib/parseBundleOptionsFromBundleRequestUrl.d.ts +17 -0
  170. package/src/lib/parseBundleOptionsFromBundleRequestUrl.js +25 -21
  171. package/src/lib/parseCustomResolverOptions.d.ts +16 -0
  172. package/src/lib/parseCustomResolverOptions.js.flow +2 -2
  173. package/src/lib/parseCustomTransformOptions.d.ts +16 -0
  174. package/src/lib/parseCustomTransformOptions.js.flow +1 -1
  175. package/src/lib/parseJsonBody.d.ts +27 -0
  176. package/src/lib/parseJsonBody.js.flow +11 -1
  177. package/src/lib/pathUtils.d.ts +14 -0
  178. package/src/lib/pathUtils.js +25 -21
  179. package/src/lib/pathUtils.js.flow +1 -1
  180. package/src/lib/relativizeSourceMap.d.ts +17 -0
  181. package/src/lib/reporting.d.ts +65 -81
  182. package/src/lib/reporting.js.flow +4 -4
  183. package/src/lib/splitBundleOptions.d.ts +16 -0
  184. package/src/lib/transformHelpers.d.ts +35 -0
  185. package/src/lib/transformHelpers.js +11 -9
  186. package/src/lib/transformHelpers.js.flow +17 -15
  187. package/src/node-haste/DependencyGraph/ModuleResolution.d.ts +126 -0
  188. package/src/node-haste/DependencyGraph/ModuleResolution.js +48 -42
  189. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +35 -32
  190. package/src/node-haste/DependencyGraph/createFileMap.d.ts +29 -0
  191. package/src/node-haste/DependencyGraph/createFileMap.js +56 -38
  192. package/src/node-haste/DependencyGraph/createFileMap.js.flow +44 -18
  193. package/src/node-haste/DependencyGraph.d.ts +57 -33
  194. package/src/node-haste/DependencyGraph.js +40 -31
  195. package/src/node-haste/DependencyGraph.js.flow +35 -37
  196. package/src/node-haste/Package.d.ts +21 -0
  197. package/src/node-haste/PackageCache.d.ts +34 -0
  198. package/src/node-haste/lib/AssetPaths.d.ts +29 -0
  199. package/src/node-haste/lib/AssetPaths.js +2 -2
  200. package/src/node-haste/lib/AssetPaths.js.flow +4 -4
  201. package/src/node-haste/lib/parsePlatformFilePath.d.ts +25 -0
  202. package/src/node-haste/lib/parsePlatformFilePath.js +6 -6
  203. package/src/node-haste/lib/parsePlatformFilePath.js.flow +4 -4
  204. package/src/shared/output/RamBundle/as-assets.d.ts +25 -0
  205. package/src/shared/output/RamBundle/as-assets.js.flow +6 -6
  206. package/src/shared/output/RamBundle/as-indexed-file.d.ts +38 -0
  207. package/src/shared/output/RamBundle/as-indexed-file.js.flow +5 -5
  208. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.d.ts +23 -0
  209. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.js.flow +5 -5
  210. package/src/shared/output/RamBundle/magic-number.d.ts +14 -0
  211. package/src/shared/output/RamBundle/util.d.ts +41 -0
  212. package/src/shared/output/RamBundle/util.js.flow +5 -5
  213. package/src/shared/output/RamBundle/write-sourcemap.d.ts +16 -0
  214. package/src/shared/output/RamBundle/write-sourcemap.js.flow +1 -1
  215. package/src/shared/output/RamBundle.d.ts +26 -0
  216. package/src/shared/output/RamBundle.js.flow +1 -1
  217. package/src/shared/output/bundle.d.ts +9 -17
  218. package/src/shared/output/bundle.flow.js.flow +3 -3
  219. package/src/shared/output/meta.d.ts +15 -0
  220. package/src/shared/output/meta.js +2 -2
  221. package/src/shared/output/meta.js.flow +1 -1
  222. package/src/shared/output/unbundle.d.ts +11 -0
  223. package/src/shared/output/writeFile.d.ts +16 -0
  224. package/src/shared/output/writeFile.js +8 -3
  225. package/src/shared/output/writeFile.js.flow +8 -2
  226. package/src/shared/types.d.ts +81 -66
  227. package/src/shared/types.js.flow +20 -5
  228. package/src/Asset.d.ts +0 -25
  229. package/src/ModuleGraph/test-helpers.js +0 -75
@@ -10,7 +10,6 @@
10
10
 
11
11
  export interface ReadOnlyCountingSet<T> extends Iterable<T> {
12
12
  has(item: T): boolean;
13
- [Symbol.iterator](): Iterator<T>;
14
13
  readonly size: number;
15
14
  count(item: T): number;
16
15
  forEach<ThisT>(
@@ -23,26 +22,27 @@ export interface ReadOnlyCountingSet<T> extends Iterable<T> {
23
22
  thisArg: ThisT,
24
23
  ): void;
25
24
  }
26
-
27
- export default class CountingSet<T> implements ReadOnlyCountingSet<T> {
25
+ /**
26
+ * A Set that only deletes a given item when the number of delete(item) calls
27
+ * matches the number of add(item) calls. Iteration and `size` are in terms of
28
+ * *unique* items.
29
+ */
30
+ declare class CountingSet<T> implements ReadOnlyCountingSet<T> {
28
31
  constructor(items?: Iterable<T>);
29
- get size(): number;
30
32
  has(item: T): boolean;
31
33
  add(item: T): void;
32
34
  delete(item: T): void;
33
35
  keys(): Iterator<T>;
34
36
  values(): Iterator<T>;
37
+ entries(): Iterator<[T, T]>;
35
38
  [Symbol.iterator](): Iterator<T>;
39
+ get size(): number;
36
40
  count(item: T): number;
37
41
  clear(): void;
38
42
  forEach<ThisT>(
39
- callbackFn: (
40
- this: ThisT,
41
- value: T,
42
- key: T,
43
- set: ReadOnlyCountingSet<T>,
44
- ) => unknown,
43
+ callbackFn: (this: ThisT, value: T, key: T, set: CountingSet<T>) => unknown,
45
44
  thisArg: ThisT,
46
45
  ): void;
47
46
  toJSON(): unknown;
48
47
  }
48
+ export default CountingSet;
@@ -11,7 +11,6 @@
11
11
 
12
12
  export interface ReadOnlyCountingSet<T> extends Iterable<T> {
13
13
  has(item: T): boolean;
14
- @@iterator(): Iterator<T>;
15
14
  +size: number;
16
15
  count(item: T): number;
17
16
  forEach<ThisT>(
@@ -20,7 +19,7 @@ export interface ReadOnlyCountingSet<T> extends Iterable<T> {
20
19
  value: T,
21
20
  key: T,
22
21
  set: ReadOnlyCountingSet<T>,
23
- ) => mixed,
22
+ ) => unknown,
24
23
 
25
24
  // NOTE: Should be optional, but Flow seems happy to infer undefined here
26
25
  // which is what we want.
@@ -88,6 +87,7 @@ export default class CountingSet<T> implements ReadOnlyCountingSet<T> {
88
87
 
89
88
  /*::
90
89
  // For Flow's benefit
90
+ // $FlowFixMe[duplicate-class-member]
91
91
  @@iterator(): Iterator<T> {
92
92
  return this.values();
93
93
  }
@@ -108,7 +108,7 @@ export default class CountingSet<T> implements ReadOnlyCountingSet<T> {
108
108
  }
109
109
 
110
110
  forEach<ThisT>(
111
- callbackFn: (this: ThisT, value: T, key: T, set: CountingSet<T>) => mixed,
111
+ callbackFn: (this: ThisT, value: T, key: T, set: CountingSet<T>) => unknown,
112
112
  thisArg: ThisT,
113
113
  ): void {
114
114
  for (const item of this) {
@@ -121,7 +121,7 @@ export default class CountingSet<T> implements ReadOnlyCountingSet<T> {
121
121
  // extend to custom collection classes. Instead let's assume values are
122
122
  // sortable ( = strings) and make this look like an array with some stable
123
123
  // order.
124
- toJSON(): mixed {
124
+ toJSON(): unknown {
125
125
  return [...this].sort();
126
126
  }
127
127
  }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ * @oncall react_native
9
+ */
10
+
11
+ import type {Writable} from 'stream';
12
+
13
+ export type SerializedError = {
14
+ message: string;
15
+ stack: string;
16
+ errors?: ReadonlyArray<SerializedError>;
17
+ cause?: SerializedError;
18
+ };
19
+ export type SerializedEvent<
20
+ TEvent extends {readonly [$$Key$$: string]: unknown},
21
+ > = TEvent extends {error: Error}
22
+ ? Omit<Omit<TEvent, 'error'>, keyof {error: SerializedError}> & {
23
+ error: SerializedError;
24
+ }
25
+ : TEvent;
26
+ declare class JsonReporter<
27
+ TEvent extends {readonly [$$Key$$: string]: unknown},
28
+ > {
29
+ _stream: Writable;
30
+ constructor(stream: Writable);
31
+ /**
32
+ * There is a special case for errors because they have non-enumerable fields.
33
+ * (Perhaps we should switch in favor of plain object?)
34
+ */
35
+ update(event: TEvent): void;
36
+ }
37
+ export default JsonReporter;
@@ -9,15 +9,17 @@ class JsonReporter {
9
9
  this._stream = stream;
10
10
  }
11
11
  update(event) {
12
+ let eventToWrite = event;
12
13
  if (event.error instanceof Error) {
13
14
  const { message, stack } = event.error;
14
- event = Object.assign(event, {
15
+ eventToWrite = {
16
+ ...eventToWrite,
15
17
  error: serializeError(event.error),
16
18
  message,
17
19
  stack,
18
- });
20
+ };
19
21
  }
20
- this._stream.write(JSON.stringify(event) + "\n");
22
+ this._stream.write(JSON.stringify(eventToWrite) + "\n");
21
23
  }
22
24
  }
23
25
  exports.default = JsonReporter;
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @flow
7
+ * @flow strict-local
8
8
  * @format
9
9
  * @oncall react_native
10
10
  */
@@ -14,23 +14,24 @@ import type {Writable} from 'stream';
14
14
  export type SerializedError = {
15
15
  message: string,
16
16
  stack: string,
17
- errors?: $ReadOnlyArray<SerializedError>,
17
+ errors?: ReadonlyArray<SerializedError>,
18
18
  cause?: SerializedError,
19
19
  ...
20
20
  };
21
21
 
22
- export type SerializedEvent<TEvent: {[string]: any, ...}> = TEvent extends {
23
- error: Error,
24
- ...
25
- }
26
- ? {
27
- ...Omit<TEvent, 'error'>,
28
- error: SerializedError,
29
- ...
30
- }
31
- : TEvent;
22
+ export type SerializedEvent<TEvent: {+[string]: unknown, ...}> =
23
+ TEvent extends {
24
+ error: Error,
25
+ ...
26
+ }
27
+ ? {
28
+ ...Omit<TEvent, 'error'>,
29
+ error: SerializedError,
30
+ ...
31
+ }
32
+ : TEvent;
32
33
 
33
- export default class JsonReporter<TEvent: {[string]: any, ...}> {
34
+ export default class JsonReporter<TEvent: {+[string]: unknown, ...}> {
34
35
  _stream: Writable;
35
36
 
36
37
  constructor(stream: Writable) {
@@ -42,19 +43,20 @@ export default class JsonReporter<TEvent: {[string]: any, ...}> {
42
43
  * (Perhaps we should switch in favor of plain object?)
43
44
  */
44
45
  update(event: TEvent): void {
46
+ let eventToWrite = event;
45
47
  if (event.error instanceof Error) {
46
48
  const {message, stack} = event.error;
47
- // $FlowFixMe[unsafe-object-assign]
48
- event = Object.assign(event, {
49
+ eventToWrite = {
50
+ ...eventToWrite,
49
51
  error: serializeError(event.error),
50
52
  // TODO: Preexisting issue - this writes message, stack, etc. as
51
53
  // top-level siblings of event.error (which was serialized to {}), whereas it was presumably
52
54
  // intended to nest them _under_ error. Fix this in a breaking change.
53
55
  message,
54
56
  stack,
55
- });
57
+ };
56
58
  }
57
- this._stream.write(JSON.stringify(event) + '\n');
59
+ this._stream.write(JSON.stringify(eventToWrite) + '\n');
58
60
  }
59
61
  }
60
62
 
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ */
9
+
10
+ /**
11
+ * Implementation of a RAM bundle parser in JS.
12
+ *
13
+ * It receives a Buffer as an input and implements two main methods, which are
14
+ * able to run in constant time no matter the size of the bundle:
15
+ *
16
+ * getStartupCode(): returns the runtime and the startup code of the bundle.
17
+ * getModule(): returns the code for the specified module.
18
+ */
19
+ declare class RamBundleParser {
20
+ _buffer: Buffer;
21
+ _numModules: number;
22
+ _startupCodeLength: number;
23
+ _startOffset: number;
24
+ constructor(buffer: Buffer);
25
+ _readPosition(pos: number): number;
26
+ getStartupCode(): string;
27
+ getModule(id: number): string;
28
+ }
29
+ export default RamBundleParser;
@@ -4,8 +4,8 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @format
8
7
  * @flow strict
8
+ * @format
9
9
  */
10
10
 
11
11
  import MAGIC_NUMBER from '../shared/output/RamBundle/magic-number';
@@ -8,9 +8,17 @@
8
8
  * @oncall react_native
9
9
  */
10
10
 
11
- import {ReportableEvent} from './reporting';
12
- import {Terminal} from 'metro-core';
11
+ import type {BundleDetails, ReportableEvent} from './reporting';
12
+ import type {Terminal} from 'metro-core';
13
+ import type {HealthCheckResult, WatcherStatus} from 'metro-file-map';
13
14
 
15
+ type BundleProgress = {
16
+ bundleDetails: BundleDetails;
17
+ transformedFileCount: number;
18
+ totalFileCount: number;
19
+ ratio: number;
20
+ isPrefetch?: boolean;
21
+ };
14
22
  export type TerminalReportableEvent =
15
23
  | ReportableEvent
16
24
  | {
@@ -18,10 +26,93 @@ export type TerminalReportableEvent =
18
26
  type: 'bundle_transform_progressed_throttled';
19
27
  transformedFileCount: number;
20
28
  totalFileCount: number;
21
- };
22
-
23
- export class TerminalReporter {
24
- constructor(terminal: Terminal);
29
+ }
30
+ | {
31
+ type: 'unstable_server_log';
32
+ level: 'info' | 'warn' | 'error';
33
+ data: string | Array<unknown>;
34
+ }
35
+ | {type: 'unstable_server_menu_updated'; message: string}
36
+ | {type: 'unstable_server_menu_cleared'};
37
+ type BuildPhase = 'in_progress' | 'done' | 'failed';
38
+ interface SnippetError extends Error {
39
+ code?: string;
40
+ filename?: string;
41
+ snippet?: string;
42
+ }
43
+ /**
44
+ * We try to print useful information to the terminal for interactive builds.
45
+ * This implements the `Reporter` interface from the './reporting' module.
46
+ */
47
+ declare class TerminalReporter {
48
+ /**
49
+ * The bundle builds for which we are actively maintaining the status on the
50
+ * terminal, ie. showing a progress bar. There can be several bundles being
51
+ * built at the same time.
52
+ */
53
+ _activeBundles: Map<string, BundleProgress>;
54
+ _interactionStatus: null | undefined | string;
55
+ _scheduleUpdateBundleProgress: {
56
+ (data: {
57
+ buildID: string;
58
+ transformedFileCount: number;
59
+ totalFileCount: number;
60
+ }): void;
61
+ cancel(): void;
62
+ };
63
+ _prevHealthCheckResult: null | undefined | HealthCheckResult;
25
64
  readonly terminal: Terminal;
65
+ constructor(terminal: Terminal);
66
+ /**
67
+ * Construct a message that represents the progress of a
68
+ * single bundle build, for example:
69
+ *
70
+ * BUNDLE path/to/bundle.js ▓▓▓▓▓░░░░░░░░░░░ 36.6% (4790/7922)
71
+ */
72
+ _getBundleStatusMessage(
73
+ $$PARAM_0$$: BundleProgress,
74
+ phase: BuildPhase,
75
+ ): string;
76
+ _logBundleBuildDone(buildID: string): void;
77
+ _logBundleBuildFailed(buildID: string): void;
78
+ _logInitializing(port: number, hasReducedPerformance: boolean): void;
79
+ _logInitializingFailed(port: number, error: SnippetError): void;
80
+ /**
81
+ * This function is only concerned with logging and should not do state
82
+ * or terminal status updates.
83
+ */
84
+ _log(event: TerminalReportableEvent): void;
85
+ /**
86
+ * We do not want to log the whole stacktrace for bundling error, because
87
+ * these are operational errors, not programming errors, and the stacktrace
88
+ * is not actionable to end users.
89
+ */
90
+ _logBundlingError(error: SnippetError): void;
91
+ _logWorkerChunk(origin: 'stdout' | 'stderr', chunk: string): void;
92
+ _updateBundleProgress($$PARAM_0$$: {
93
+ buildID: string;
94
+ transformedFileCount: number;
95
+ totalFileCount: number;
96
+ }): void;
97
+ /**
98
+ * This function is exclusively concerned with updating the internal state.
99
+ * No logging or status updates should be done at this point.
100
+ */
101
+ _updateState(event: TerminalReportableEvent): void;
102
+ /**
103
+ * Return a status message that is always consistent with the current state
104
+ * of the application. Having this single function ensures we don't have
105
+ * different callsites overriding each other status messages.
106
+ */
107
+ _getStatusMessage(): string;
108
+ _logHmrClientError(e: Error): void;
109
+ _logWarning(message: string): void;
110
+ _logWatcherHealthCheckResult(result: HealthCheckResult): void;
111
+ _logWatcherStatus(status: WatcherStatus): void;
112
+ /**
113
+ * Single entry point for reporting events. That allows us to implement the
114
+ * corresponding JSON reporter easily and have a consistent reporting.
115
+ */
26
116
  update(event: TerminalReportableEvent): void;
27
117
  }
118
+ export default TerminalReporter;
@@ -4,34 +4,39 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true,
5
5
  });
6
6
  exports.default = void 0;
7
+ var _bundleProgressUtils = require("./bundleProgressUtils");
7
8
  var _logToConsole = _interopRequireDefault(require("./logToConsole"));
8
9
  var reporting = _interopRequireWildcard(require("./reporting"));
9
10
  var _chalk = _interopRequireDefault(require("chalk"));
10
11
  var _lodash = _interopRequireDefault(require("lodash.throttle"));
11
12
  var _metroCore = require("metro-core");
12
13
  var _path = _interopRequireDefault(require("path"));
13
- function _getRequireWildcardCache(e) {
14
- if ("function" != typeof WeakMap) return null;
15
- var r = new WeakMap(),
16
- t = new WeakMap();
17
- return (_getRequireWildcardCache = function (e) {
18
- return e ? t : r;
19
- })(e);
20
- }
21
- function _interopRequireWildcard(e, r) {
22
- if (!r && e && e.__esModule) return e;
23
- if (null === e || ("object" != typeof e && "function" != typeof e))
24
- return { default: e };
25
- var t = _getRequireWildcardCache(r);
26
- if (t && t.has(e)) return t.get(e);
27
- var n = { __proto__: null },
28
- a = Object.defineProperty && Object.getOwnPropertyDescriptor;
29
- for (var u in e)
30
- if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
31
- var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
32
- i && (i.get || i.set) ? Object.defineProperty(n, u, i) : (n[u] = e[u]);
14
+ function _interopRequireWildcard(e, t) {
15
+ if ("function" == typeof WeakMap)
16
+ var r = new WeakMap(),
17
+ n = new WeakMap();
18
+ return (_interopRequireWildcard = function (e, t) {
19
+ if (!t && e && e.__esModule) return e;
20
+ var o,
21
+ i,
22
+ f = { __proto__: null, default: e };
23
+ if (null === e || ("object" != typeof e && "function" != typeof e))
24
+ return f;
25
+ if ((o = t ? n : r)) {
26
+ if (o.has(e)) return o.get(e);
27
+ o.set(e, f);
33
28
  }
34
- return ((n.default = e), t && t.set(e, n), n);
29
+ for (const t in e)
30
+ "default" !== t &&
31
+ {}.hasOwnProperty.call(e, t) &&
32
+ ((i =
33
+ (o = Object.defineProperty) &&
34
+ Object.getOwnPropertyDescriptor(e, t)) &&
35
+ (i.get || i.set)
36
+ ? o(f, t, i)
37
+ : (f[t] = e[t]));
38
+ return f;
39
+ })(e, t);
35
40
  }
36
41
  function _interopRequireDefault(e) {
37
42
  return e && e.__esModule ? e : { default: e };
@@ -60,9 +65,6 @@ class TerminalReporter {
60
65
  },
61
66
  phase,
62
67
  ) {
63
- if (isPrefetch) {
64
- bundleType = "PREBUNDLE";
65
- }
66
68
  const localPath = _path.default.relative(".", entryFile);
67
69
  const filledBar = Math.floor(ratio * MAX_PROGRESS_BAR_CHAR_WIDTH);
68
70
  const bundleTypeColor =
@@ -77,11 +79,13 @@ class TerminalReporter {
77
79
  _chalk.default.bgWhite.white(
78
80
  LIGHT_BLOCK_CHAR.repeat(MAX_PROGRESS_BAR_CHAR_WIDTH - filledBar),
79
81
  ) +
80
- _chalk.default.bold(` ${(100 * ratio).toFixed(1)}% `) +
82
+ _chalk.default.bold(` ${Math.floor(100 * ratio)}% `) +
81
83
  _chalk.default.dim(`(${transformedFileCount}/${totalFileCount})`)
82
84
  : "";
83
85
  return (
84
- bundleTypeColor.inverse.bold(` ${bundleType.toUpperCase()} `) +
86
+ bundleTypeColor.inverse.bold(
87
+ ` ${isPrefetch === true ? "PREBUNDLE" : bundleType.toUpperCase()} `,
88
+ ) +
85
89
  _chalk.default.reset.dim(` ${_path.default.dirname(localPath)}/`) +
86
90
  _chalk.default.bold(_path.default.basename(localPath)) +
87
91
  " " +
@@ -261,8 +265,9 @@ class TerminalReporter {
261
265
  message = error.stack;
262
266
  }
263
267
  }
264
- if (error.filename && !message.includes(error.filename)) {
265
- message += ` [${error.filename}]`;
268
+ const filename = error.filename;
269
+ if (filename?.length && !message.includes(filename)) {
270
+ message += ` [${filename}]`;
266
271
  }
267
272
  if (error.snippet != null) {
268
273
  message += "\n" + error.snippet;
@@ -283,12 +288,10 @@ class TerminalReporter {
283
288
  if (currentProgress == null) {
284
289
  return;
285
290
  }
286
- const ratio = Math.min(
287
- Math.max(
288
- Math.pow(transformedFileCount / Math.max(totalFileCount, 10), 2),
289
- currentProgress.ratio,
290
- ),
291
- 0.999,
291
+ const ratio = (0, _bundleProgressUtils.calculateBundleProgressRatio)(
292
+ transformedFileCount,
293
+ totalFileCount,
294
+ currentProgress.ratio,
292
295
  );
293
296
  Object.assign(currentProgress, {
294
297
  ratio,
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
- * @flow
7
+ * @flow strict-local
8
8
  * @format
9
9
  * @oncall react_native
10
10
  */
@@ -13,9 +13,11 @@ import type {BundleDetails, ReportableEvent} from './reporting';
13
13
  import type {Terminal} from 'metro-core';
14
14
  import type {HealthCheckResult, WatcherStatus} from 'metro-file-map';
15
15
 
16
+ import {calculateBundleProgressRatio} from './bundleProgressUtils';
16
17
  import logToConsole from './logToConsole';
17
18
  import * as reporting from './reporting';
18
19
  import chalk from 'chalk';
20
+ // $FlowFixMe[untyped-import] lodash.throttle
19
21
  import throttle from 'lodash.throttle';
20
22
  import {AmbiguousModuleResolutionError} from 'metro-core';
21
23
  import path from 'path';
@@ -41,7 +43,7 @@ export type TerminalReportableEvent =
41
43
  | {
42
44
  type: 'unstable_server_log',
43
45
  level: 'info' | 'warn' | 'error',
44
- data: string | Array<mixed>,
46
+ data: string | Array<unknown>,
45
47
  ...
46
48
  }
47
49
  | {
@@ -56,11 +58,11 @@ export type TerminalReportableEvent =
56
58
 
57
59
  type BuildPhase = 'in_progress' | 'done' | 'failed';
58
60
 
59
- type SnippetError = ErrnoError &
60
- interface {
61
- filename?: string,
62
- snippet?: string,
63
- };
61
+ interface SnippetError extends Error {
62
+ code?: string;
63
+ filename?: string;
64
+ snippet?: string;
65
+ }
64
66
 
65
67
  const DARK_BLOCK_CHAR = '\u2593';
66
68
  const LIGHT_BLOCK_CHAR = '\u2591';
@@ -117,10 +119,6 @@ export default class TerminalReporter {
117
119
  }: BundleProgress,
118
120
  phase: BuildPhase,
119
121
  ): string {
120
- if (isPrefetch) {
121
- bundleType = 'PREBUNDLE';
122
- }
123
-
124
122
  const localPath = path.relative('.', entryFile);
125
123
  const filledBar = Math.floor(ratio * MAX_PROGRESS_BAR_CHAR_WIDTH);
126
124
  const bundleTypeColor =
@@ -135,12 +133,14 @@ export default class TerminalReporter {
135
133
  chalk.bgWhite.white(
136
134
  LIGHT_BLOCK_CHAR.repeat(MAX_PROGRESS_BAR_CHAR_WIDTH - filledBar),
137
135
  ) +
138
- chalk.bold(` ${(100 * ratio).toFixed(1)}% `) +
136
+ chalk.bold(` ${Math.floor(100 * ratio)}% `) +
139
137
  chalk.dim(`(${transformedFileCount}/${totalFileCount})`)
140
138
  : '';
141
139
 
142
140
  return (
143
- bundleTypeColor.inverse.bold(` ${bundleType.toUpperCase()} `) +
141
+ bundleTypeColor.inverse.bold(
142
+ ` ${isPrefetch === true ? 'PREBUNDLE' : bundleType.toUpperCase()} `,
143
+ ) +
144
144
  chalk.reset.dim(` ${path.dirname(localPath)}/`) +
145
145
  chalk.bold(path.basename(localPath)) +
146
146
  ' ' +
@@ -273,6 +273,7 @@ export default class TerminalReporter {
273
273
  case 'dep_graph_loading':
274
274
  const color = event.hasReducedPerformance ? chalk.red : chalk.blue;
275
275
  // eslint-disable-next-line import/no-commonjs
276
+ // $FlowFixMe[untyped-import] package.json
276
277
  const version = 'v' + require('../../package.json').version;
277
278
  this.terminal.log(
278
279
  color.bold(
@@ -331,9 +332,9 @@ export default class TerminalReporter {
331
332
  }
332
333
  }
333
334
 
334
- if (error.filename && !message.includes(error.filename)) {
335
- // $FlowFixMe[incompatible-type]
336
- message += ` [${error.filename}]`;
335
+ const filename = error.filename;
336
+ if (filename?.length && !message.includes(filename)) {
337
+ message += ` [${filename}]`;
337
338
  }
338
339
 
339
340
  if (error.snippet != null) {
@@ -352,14 +353,6 @@ export default class TerminalReporter {
352
353
  });
353
354
  }
354
355
 
355
- /**
356
- * Because we know the `totalFileCount` is going to progressively increase
357
- * starting with 1:
358
- * - We use Math.max(totalFileCount, 10) to prevent the ratio to raise too
359
- * quickly when the total file count is low. (e.g 1/2 5/6)
360
- * - We prevent the ratio from going backwards.
361
- * - Instead, we use Math.pow(ratio, 2) to as a conservative measure of progress.
362
- */
363
356
  _updateBundleProgress({
364
357
  buildID,
365
358
  transformedFileCount,
@@ -375,12 +368,10 @@ export default class TerminalReporter {
375
368
  return;
376
369
  }
377
370
 
378
- const ratio = Math.min(
379
- Math.max(
380
- Math.pow(transformedFileCount / Math.max(totalFileCount, 10), 2),
381
- currentProgress.ratio,
382
- ),
383
- 0.999, // make sure not to go above 99.9% to not get rounded to 100%,
371
+ const ratio = calculateBundleProgressRatio(
372
+ transformedFileCount,
373
+ totalFileCount,
374
+ currentProgress.ratio,
384
375
  );
385
376
 
386
377
  // $FlowFixMe[unsafe-object-assign]
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @format
8
+ * @oncall react_native
9
+ */
10
+
11
+ /**
12
+ * Calculates a conservative progress ratio for bundle building.
13
+ *
14
+ * Because we know the `totalFileCount` is going to progressively increase
15
+ * starting with 1:
16
+ * - We use Math.max(totalFileCount, 10) to prevent the ratio from raising too
17
+ * quickly when the total file count is low. (e.g 1/2 5/6)
18
+ * - We use Math.pow(ratio, 2) as a conservative measure of progress.
19
+ * - The ratio is capped at 0.999 to ensure we don't display 100% until done.
20
+ * - If previousRatio is provided, the ratio will not go backwards.
21
+ */
22
+ export declare function calculateBundleProgressRatio(
23
+ transformedFileCount: number,
24
+ totalFileCount: number,
25
+ previousRatio?: number,
26
+ ): number;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true,
5
+ });
6
+ exports.calculateBundleProgressRatio = calculateBundleProgressRatio;
7
+ function calculateBundleProgressRatio(
8
+ transformedFileCount,
9
+ totalFileCount,
10
+ previousRatio,
11
+ ) {
12
+ const baseRatio = Math.pow(
13
+ transformedFileCount / Math.max(totalFileCount, 10),
14
+ 2,
15
+ );
16
+ const ratio =
17
+ previousRatio != null ? Math.max(baseRatio, previousRatio) : baseRatio;
18
+ return Math.min(ratio, 0.999);
19
+ }