metro 0.83.4 → 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 (161) hide show
  1. package/package.json +17 -15
  2. package/src/Assets.d.ts +83 -0
  3. package/src/Assets.js +2 -9
  4. package/src/Bundler/util.d.ts +27 -0
  5. package/src/Bundler/util.js +2 -9
  6. package/src/Bundler.d.ts +38 -0
  7. package/src/Bundler.js +1 -5
  8. package/src/DeltaBundler/DeltaCalculator.d.ts +71 -0
  9. package/src/DeltaBundler/DeltaCalculator.js +1 -5
  10. package/src/DeltaBundler/Graph.d.ts +167 -0
  11. package/src/DeltaBundler/Graph.js +1 -5
  12. package/src/DeltaBundler/Serializers/baseJSBundle.d.ts +20 -0
  13. package/src/DeltaBundler/Serializers/baseJSBundle.js +1 -5
  14. package/src/DeltaBundler/Serializers/getAllFiles.d.ts +22 -0
  15. package/src/DeltaBundler/Serializers/getAssets.d.ts +25 -0
  16. package/src/DeltaBundler/Serializers/getAssets.js +1 -5
  17. package/src/DeltaBundler/Serializers/getExplodedSourceMap.d.ts +26 -0
  18. package/src/DeltaBundler/Serializers/getRamBundleInfo.d.ts +48 -0
  19. package/src/DeltaBundler/Serializers/getRamBundleInfo.js +1 -5
  20. package/src/DeltaBundler/Serializers/helpers/getInlineSourceMappingURL.d.ts +12 -0
  21. package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.d.ts +33 -0
  22. package/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.d.ts +17 -0
  23. package/src/DeltaBundler/Serializers/helpers/js.d.ts +30 -0
  24. package/src/DeltaBundler/Serializers/helpers/js.js +2 -9
  25. package/src/DeltaBundler/Serializers/helpers/processModules.d.ts +25 -0
  26. package/src/DeltaBundler/Serializers/hmrJSBundle.d.ts +30 -0
  27. package/src/DeltaBundler/Serializers/hmrJSBundle.js +2 -9
  28. package/src/DeltaBundler/Serializers/sourceMapGenerator.d.ts +29 -0
  29. package/src/DeltaBundler/Serializers/sourceMapGenerator.js +1 -5
  30. package/src/DeltaBundler/Serializers/sourceMapObject.d.ts +23 -0
  31. package/src/DeltaBundler/Serializers/sourceMapString.d.ts +22 -0
  32. package/src/DeltaBundler/Transformer.d.ts +38 -0
  33. package/src/DeltaBundler/Transformer.js +1 -5
  34. package/src/DeltaBundler/Worker.d.ts +37 -0
  35. package/src/DeltaBundler/Worker.flow.js +1 -5
  36. package/src/DeltaBundler/WorkerFarm.d.ts +70 -0
  37. package/src/DeltaBundler/buildSubgraph.d.ts +30 -0
  38. package/src/DeltaBundler/buildSubgraph.js +1 -5
  39. package/src/DeltaBundler/getTransformCacheKey.d.ts +18 -0
  40. package/src/DeltaBundler/getTransformCacheKey.js +1 -5
  41. package/src/DeltaBundler/mergeDeltas.d.ts +17 -0
  42. package/src/DeltaBundler/types.d.ts +163 -0
  43. package/src/DeltaBundler/types.js +1 -5
  44. package/src/DeltaBundler.d.ts +60 -0
  45. package/src/DeltaBundler.js +1 -5
  46. package/src/HmrServer.d.ts +79 -0
  47. package/src/HmrServer.js +2 -9
  48. package/src/IncrementalBundler/GraphNotFoundError.d.ts +17 -0
  49. package/src/IncrementalBundler/ResourceNotFoundError.d.ts +15 -0
  50. package/src/IncrementalBundler/RevisionNotFoundError.d.ts +17 -0
  51. package/src/IncrementalBundler.d.ts +96 -0
  52. package/src/IncrementalBundler.js +2 -9
  53. package/src/ModuleGraph/worker/JsFileWrapping.d.ts +29 -0
  54. package/src/ModuleGraph/worker/JsFileWrapping.js +2 -9
  55. package/src/ModuleGraph/worker/collectDependencies.d.ts +131 -0
  56. package/src/ModuleGraph/worker/collectDependencies.js +2 -9
  57. package/src/ModuleGraph/worker/generateImportNames.d.ts +19 -0
  58. package/src/ModuleGraph/worker/generateImportNames.js +1 -5
  59. package/src/ModuleGraph/worker/importLocationsPlugin.d.ts +26 -0
  60. package/src/Server/MultipartResponse.d.ts +30 -0
  61. package/src/Server/MultipartResponse.js +1 -5
  62. package/src/Server/symbolicate.d.ts +31 -0
  63. package/src/Server.d.ts +271 -0
  64. package/src/Server.js +47 -15
  65. package/src/Server.js.flow +57 -10
  66. package/src/cli/parseKeyValueParamArray.d.ts +14 -0
  67. package/src/cli-utils.d.ts +19 -0
  68. package/src/cli-utils.js +1 -5
  69. package/src/commands/build.d.ts +16 -0
  70. package/src/commands/build.js +1 -5
  71. package/src/commands/dependencies.d.ts +16 -0
  72. package/src/commands/dependencies.js +1 -5
  73. package/src/commands/serve.d.ts +16 -0
  74. package/src/index.d.ts +180 -0
  75. package/src/index.flow.js +26 -19
  76. package/src/index.flow.js.flow +25 -10
  77. package/src/integration_tests/basic_bundle/build-errors/cannot-resolve-import.js +1 -5
  78. package/src/integration_tests/basic_bundle/build-errors/inline-requires-cannot-resolve-import.js +1 -5
  79. package/src/integration_tests/basic_bundle/import-export/index.js +2 -9
  80. package/src/integration_tests/execBundle.js +1 -5
  81. package/src/lib/BatchProcessor.d.ts +54 -0
  82. package/src/lib/BatchProcessor.js +1 -5
  83. package/src/lib/CountingSet.d.ts +48 -0
  84. package/src/lib/JsonReporter.d.ts +37 -0
  85. package/src/lib/RamBundleParser.d.ts +29 -0
  86. package/src/lib/RamBundleParser.js +1 -5
  87. package/src/lib/TerminalReporter.d.ts +118 -0
  88. package/src/lib/TerminalReporter.js +8 -16
  89. package/src/lib/TerminalReporter.js.flow +6 -15
  90. package/src/lib/bundleProgressUtils.d.ts +26 -0
  91. package/src/lib/bundleProgressUtils.js +19 -0
  92. package/src/lib/bundleProgressUtils.js.flow +35 -0
  93. package/src/lib/bundleToString.d.ts +19 -0
  94. package/src/lib/contextModule.d.ts +33 -0
  95. package/src/lib/contextModule.js +1 -5
  96. package/src/lib/contextModuleTemplates.d.ts +25 -0
  97. package/src/lib/contextModuleTemplates.js +1 -4
  98. package/src/lib/countLines.d.ts +12 -0
  99. package/src/lib/createWebsocketServer.d.ts +43 -0
  100. package/src/lib/createWebsocketServer.js +1 -5
  101. package/src/lib/debounceAsyncQueue.d.ts +15 -0
  102. package/src/lib/formatBundlingError.d.ts +23 -0
  103. package/src/lib/formatBundlingError.js +1 -5
  104. package/src/lib/getAppendScripts.d.ts +31 -0
  105. package/src/lib/getAppendScripts.js +1 -5
  106. package/src/lib/getGraphId.d.ts +25 -0
  107. package/src/lib/getGraphId.js +1 -5
  108. package/src/lib/getPreludeCode.d.ts +18 -0
  109. package/src/lib/getPrependedScripts.d.ts +24 -0
  110. package/src/lib/getPrependedScripts.js +2 -9
  111. package/src/lib/isResolvedDependency.d.ts +15 -0
  112. package/src/lib/logToConsole.d.ts +19 -0
  113. package/src/lib/logToConsole.js +1 -5
  114. package/src/lib/parseBundleOptionsFromBundleRequestUrl.d.ts +17 -0
  115. package/src/lib/parseBundleOptionsFromBundleRequestUrl.js +2 -9
  116. package/src/lib/parseCustomResolverOptions.d.ts +16 -0
  117. package/src/lib/parseCustomTransformOptions.d.ts +16 -0
  118. package/src/lib/parseJsonBody.d.ts +27 -0
  119. package/src/lib/pathUtils.d.ts +14 -0
  120. package/src/lib/pathUtils.js +1 -4
  121. package/src/lib/relativizeSourceMap.d.ts +17 -0
  122. package/src/lib/relativizeSourceMap.js +1 -5
  123. package/src/lib/reporting.d.ts +128 -0
  124. package/src/lib/reporting.js +1 -5
  125. package/src/lib/splitBundleOptions.d.ts +16 -0
  126. package/src/lib/transformHelpers.d.ts +35 -0
  127. package/src/lib/transformHelpers.js +1 -5
  128. package/src/node-haste/DependencyGraph/ModuleResolution.d.ts +126 -0
  129. package/src/node-haste/DependencyGraph/ModuleResolution.js +2 -9
  130. package/src/node-haste/DependencyGraph/createFileMap.d.ts +29 -0
  131. package/src/node-haste/DependencyGraph/createFileMap.js +2 -9
  132. package/src/node-haste/DependencyGraph.d.ts +86 -0
  133. package/src/node-haste/DependencyGraph.js +1 -5
  134. package/src/node-haste/Package.d.ts +21 -0
  135. package/src/node-haste/Package.js +1 -5
  136. package/src/node-haste/PackageCache.d.ts +34 -0
  137. package/src/node-haste/PackageCache.js +1 -5
  138. package/src/node-haste/lib/AssetPaths.d.ts +29 -0
  139. package/src/node-haste/lib/AssetPaths.js +1 -5
  140. package/src/node-haste/lib/parsePlatformFilePath.d.ts +25 -0
  141. package/src/node-haste/lib/parsePlatformFilePath.js +1 -5
  142. package/src/shared/output/RamBundle/as-assets.d.ts +25 -0
  143. package/src/shared/output/RamBundle/as-assets.js +1 -5
  144. package/src/shared/output/RamBundle/as-indexed-file.d.ts +38 -0
  145. package/src/shared/output/RamBundle/as-indexed-file.js +1 -5
  146. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.d.ts +23 -0
  147. package/src/shared/output/RamBundle/magic-number.d.ts +14 -0
  148. package/src/shared/output/RamBundle/util.d.ts +41 -0
  149. package/src/shared/output/RamBundle/util.js +1 -5
  150. package/src/shared/output/RamBundle/write-sourcemap.d.ts +16 -0
  151. package/src/shared/output/RamBundle/write-sourcemap.js +1 -5
  152. package/src/shared/output/RamBundle.d.ts +26 -0
  153. package/src/shared/output/RamBundle.js +1 -5
  154. package/src/shared/output/bundle.d.ts +27 -0
  155. package/src/shared/output/bundle.flow.js +1 -5
  156. package/src/shared/output/meta.d.ts +15 -0
  157. package/src/shared/output/meta.js +1 -5
  158. package/src/shared/output/unbundle.d.ts +11 -0
  159. package/src/shared/output/writeFile.d.ts +16 -0
  160. package/src/shared/output/writeFile.js +1 -5
  161. package/src/shared/types.d.ts +145 -0
@@ -0,0 +1,131 @@
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
+ import type {ReadonlySourceLocation} from '../../shared/types';
11
+ import type {NodePath} from '@babel/traverse';
12
+ import type {
13
+ CallExpression,
14
+ File as BabelNodeFile,
15
+ Identifier,
16
+ StringLiteral,
17
+ } from '@babel/types';
18
+ import type {
19
+ AllowOptionalDependencies,
20
+ AsyncDependencyType,
21
+ } from 'metro/private/DeltaBundler/types';
22
+
23
+ export type Dependency = Readonly<{data: DependencyData; name: string}>;
24
+ export type ContextMode = 'sync' | 'eager' | 'lazy' | 'lazy-once';
25
+ type ContextFilter = Readonly<{pattern: string; flags: string}>;
26
+ export type RequireContextParams = Readonly<{
27
+ recursive: boolean;
28
+ filter: Readonly<ContextFilter>;
29
+ /** Mode for resolving dynamic dependencies. Defaults to `sync` */
30
+ mode: ContextMode;
31
+ }>;
32
+ type DependencyData = Readonly<{
33
+ key: string;
34
+ asyncType: AsyncDependencyType | null;
35
+ isESMImport: boolean;
36
+ isOptional?: boolean;
37
+ locs: ReadonlyArray<ReadonlySourceLocation>;
38
+ /** Context for requiring a collection of modules. */
39
+ contextParams?: RequireContextParams;
40
+ }>;
41
+ export type MutableInternalDependency = Omit<
42
+ DependencyData,
43
+ keyof {locs: Array<ReadonlySourceLocation>; index: number; name: string}
44
+ > & {locs: Array<ReadonlySourceLocation>; index: number; name: string};
45
+ export type InternalDependency = Readonly<MutableInternalDependency>;
46
+ export type State = {
47
+ asyncRequireModulePathStringLiteral: null | undefined | StringLiteral;
48
+ dependencyCalls: Set<string>;
49
+ dependencyRegistry: DependencyRegistry;
50
+ dependencyTransformer: DependencyTransformer;
51
+ dynamicRequires: DynamicRequiresBehavior;
52
+ dependencyMapIdentifier: null | undefined | Identifier;
53
+ keepRequireNames: boolean;
54
+ allowOptionalDependencies: AllowOptionalDependencies;
55
+ /** Enable `require.context` statements which can be used to import multiple files in a directory. */
56
+ unstable_allowRequireContext: boolean;
57
+ unstable_isESMImportAtSource:
58
+ | null
59
+ | undefined
60
+ | (($$PARAM_0$$: ReadonlySourceLocation) => boolean);
61
+ };
62
+ export type Options = Readonly<{
63
+ asyncRequireModulePath: string;
64
+ dependencyMapName: null | undefined | string;
65
+ dynamicRequires: DynamicRequiresBehavior;
66
+ inlineableCalls: ReadonlyArray<string>;
67
+ keepRequireNames: boolean;
68
+ allowOptionalDependencies: AllowOptionalDependencies;
69
+ dependencyTransformer?: DependencyTransformer;
70
+ /** Enable `require.context` statements which can be used to import multiple files in a directory. */
71
+ unstable_allowRequireContext: boolean;
72
+ unstable_isESMImportAtSource?:
73
+ | null
74
+ | undefined
75
+ | (($$PARAM_0$$: ReadonlySourceLocation) => boolean);
76
+ }>;
77
+ export type CollectedDependencies = Readonly<{
78
+ ast: BabelNodeFile;
79
+ dependencyMapName: string;
80
+ dependencies: ReadonlyArray<Dependency>;
81
+ }>;
82
+ export interface DependencyTransformer {
83
+ transformSyncRequire(
84
+ path: NodePath<CallExpression>,
85
+ dependency: InternalDependency,
86
+ state: State,
87
+ ): void;
88
+ transformImportCall(
89
+ path: NodePath,
90
+ dependency: InternalDependency,
91
+ state: State,
92
+ ): void;
93
+ transformImportMaybeSyncCall(
94
+ path: NodePath,
95
+ dependency: InternalDependency,
96
+ state: State,
97
+ ): void;
98
+ transformPrefetch(
99
+ path: NodePath,
100
+ dependency: InternalDependency,
101
+ state: State,
102
+ ): void;
103
+ transformIllegalDynamicRequire(path: NodePath, state: State): void;
104
+ }
105
+ export type DynamicRequiresBehavior = 'throwAtRuntime' | 'reject';
106
+ /**
107
+ * Transform all the calls to `require()` and `import()` in a file into ID-
108
+ * independent code, and return the list of dependencies. For example, a call
109
+ * like `require('Foo')` could be transformed to `require(_depMap[3], 'Foo')`
110
+ * where `_depMap` is provided by the outer scope. As such, we don't need to
111
+ * know the actual module ID.
112
+ *
113
+ * The second argument is only provided for debugging purposes.
114
+ */
115
+ declare function collectDependencies(
116
+ ast: BabelNodeFile,
117
+ options: Options,
118
+ ): CollectedDependencies;
119
+ export default collectDependencies;
120
+ export type ImportQualifier = Readonly<{
121
+ name: string;
122
+ asyncType: AsyncDependencyType | null;
123
+ isESMImport: boolean;
124
+ optional: boolean;
125
+ contextParams?: RequireContextParams;
126
+ }>;
127
+ declare class DependencyRegistry {
128
+ _dependencies: Map<string, InternalDependency>;
129
+ registerDependency(qualifier: ImportQualifier): InternalDependency;
130
+ getDependencies(): Array<InternalDependency>;
131
+ }
@@ -20,10 +20,7 @@ function _interopRequireWildcard(e, t) {
20
20
  if (!t && e && e.__esModule) return e;
21
21
  var o,
22
22
  i,
23
- f = {
24
- __proto__: null,
25
- default: e,
26
- };
23
+ f = { __proto__: null, default: e };
27
24
  if (null === e || ("object" != typeof e && "function" != typeof e))
28
25
  return f;
29
26
  if ((o = t ? n : r)) {
@@ -43,11 +40,7 @@ function _interopRequireWildcard(e, t) {
43
40
  })(e, t);
44
41
  }
45
42
  function _interopRequireDefault(e) {
46
- return e && e.__esModule
47
- ? e
48
- : {
49
- default: e,
50
- };
43
+ return e && e.__esModule ? e : { default: e };
51
44
  }
52
45
  function collectDependencies(ast, options) {
53
46
  const visited = new WeakSet();
@@ -0,0 +1,19 @@
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
+ import type {Node} from '@babel/types';
11
+ /**
12
+ * Select unused names for "metroImportDefault" and "metroImportAll", by
13
+ * calling "generateUid".
14
+ */
15
+ declare function generateImportNames(ast: Node): {
16
+ importAll: string;
17
+ importDefault: string;
18
+ };
19
+ export default generateImportNames;
@@ -7,11 +7,7 @@ exports.default = generateImportNames;
7
7
  var _traverse = _interopRequireDefault(require("@babel/traverse"));
8
8
  var _nullthrows = _interopRequireDefault(require("nullthrows"));
9
9
  function _interopRequireDefault(e) {
10
- return e && e.__esModule
11
- ? e
12
- : {
13
- default: e,
14
- };
10
+ return e && e.__esModule ? e : { default: e };
15
11
  }
16
12
  function generateImportNames(ast) {
17
13
  let importDefault;
@@ -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
+ import type {ReadonlySourceLocation} from '../../shared/types';
12
+ import type {PluginObj} from '@babel/core';
13
+ import type * as $$IMPORT_TYPEOF_1$$ from '@babel/types';
14
+ import type {MetroBabelFileMetadata} from 'metro-babel-transformer';
15
+
16
+ type Types = typeof $$IMPORT_TYPEOF_1$$;
17
+ type ImportDeclarationLocs = Set<string>;
18
+ type State = {
19
+ importDeclarationLocs: ImportDeclarationLocs;
20
+ file: {metadata?: MetroBabelFileMetadata};
21
+ };
22
+ declare function importLocationsPlugin($$PARAM_0$$: {
23
+ types: Types;
24
+ }): PluginObj<State>;
25
+ declare function locToKey(loc: ReadonlySourceLocation): string;
26
+ export {importLocationsPlugin, locToKey};
@@ -0,0 +1,30 @@
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 {IncomingMessage, ServerResponse} from 'http';
12
+
13
+ type Data = string | Buffer | Uint8Array;
14
+ type Headers = {[$$Key$$: string]: string | number};
15
+ declare class MultipartResponse {
16
+ static wrapIfSupported(
17
+ req: IncomingMessage,
18
+ res: ServerResponse,
19
+ ): MultipartResponse | ServerResponse;
20
+ static serializeHeaders(headers: Headers): string;
21
+ res: ServerResponse;
22
+ headers: Headers;
23
+ constructor(res: ServerResponse);
24
+ writeChunk(headers: Headers | null, data?: Data, isLast?: boolean): void;
25
+ writeHead(status: number, headers?: Headers): void;
26
+ setHeader(name: string, value: string | number): void;
27
+ end(data?: Data): void;
28
+ once(name: string, fn: () => unknown): this;
29
+ }
30
+ export default MultipartResponse;
@@ -6,11 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _accepts = _interopRequireDefault(require("accepts"));
8
8
  function _interopRequireDefault(e) {
9
- return e && e.__esModule
10
- ? e
11
- : {
12
- default: e,
13
- };
9
+ return e && e.__esModule ? e : { default: e };
14
10
  }
15
11
  const CRLF = "\r\n";
16
12
  const BOUNDARY = "3beqjf3apnqeu3h5jqorms4i";
@@ -0,0 +1,31 @@
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 {ExplodedSourceMap} from '../DeltaBundler/Serializers/getExplodedSourceMap';
12
+ import type {ConfigT} from 'metro-config';
13
+
14
+ export type StackFrameInput = {
15
+ readonly file: null | undefined | string;
16
+ readonly lineNumber: null | undefined | number;
17
+ readonly column: null | undefined | number;
18
+ readonly methodName: null | undefined | string;
19
+ };
20
+ export type IntermediateStackFrame = Omit<
21
+ StackFrameInput,
22
+ keyof {collapse?: boolean}
23
+ > & {collapse?: boolean};
24
+ export type StackFrameOutput = Readonly<IntermediateStackFrame>;
25
+ declare function symbolicate(
26
+ stack: ReadonlyArray<StackFrameInput>,
27
+ maps: Iterable<[string, ExplodedSourceMap]>,
28
+ config: ConfigT,
29
+ extraData: unknown,
30
+ ): Promise<ReadonlyArray<StackFrameOutput>>;
31
+ export default symbolicate;
@@ -0,0 +1,271 @@
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 {AssetData} from './Assets';
12
+ import type {ExplodedSourceMap} from './DeltaBundler/Serializers/getExplodedSourceMap';
13
+ import type {RamBundleInfo} from './DeltaBundler/Serializers/getRamBundleInfo';
14
+ import type {
15
+ Module,
16
+ ReadOnlyDependencies,
17
+ ReadOnlyGraph,
18
+ TransformInputOptions,
19
+ } from './DeltaBundler/types';
20
+ import type {RevisionId} from './IncrementalBundler';
21
+ import type {GraphId} from './lib/getGraphId';
22
+ import type {Reporter} from './lib/reporting';
23
+ import type {
24
+ BuildOptions,
25
+ BundleOptions,
26
+ GraphOptions,
27
+ ResolverInputOptions,
28
+ SplitBundleOptions,
29
+ } from './shared/types';
30
+ import type {IncomingMessage} from 'connect';
31
+ import type {ServerResponse} from 'http';
32
+ import type {ConfigT, RootPerfLogger} from 'metro-config';
33
+ import type {
34
+ ActionLogEntryData,
35
+ ActionStartLogEntry,
36
+ } from 'metro-core/private/Logger';
37
+ import type {CustomResolverOptions} from 'metro-resolver/private/types';
38
+ import type {CustomTransformOptions} from 'metro-transform-worker';
39
+
40
+ import IncrementalBundler from './IncrementalBundler';
41
+ import MultipartResponse from './Server/MultipartResponse';
42
+ import {SourcePathsMode} from './shared/types';
43
+ import {Logger} from 'metro-core';
44
+
45
+ export type SegmentLoadData = {
46
+ [$$Key$$: number]: [Array<number>, null | undefined | number];
47
+ };
48
+ export type BundleMetadata = {
49
+ hash: string;
50
+ otaBuildNumber: null | undefined | string;
51
+ mobileConfigs: Array<string>;
52
+ segmentHashes: Array<string>;
53
+ segmentLoadData: SegmentLoadData;
54
+ };
55
+ type ProcessStartContext = Omit<
56
+ SplitBundleOptions,
57
+ keyof {
58
+ readonly buildNumber: number;
59
+ readonly bundleOptions: BundleOptions;
60
+ readonly graphId: GraphId;
61
+ readonly graphOptions: GraphOptions;
62
+ readonly mres: MultipartResponse | ServerResponse;
63
+ readonly req: IncomingMessage;
64
+ readonly revisionId?: null | undefined | RevisionId;
65
+ readonly bundlePerfLogger: RootPerfLogger;
66
+ readonly requestStartTimestamp: number;
67
+ }
68
+ > & {
69
+ readonly buildNumber: number;
70
+ readonly bundleOptions: BundleOptions;
71
+ readonly graphId: GraphId;
72
+ readonly graphOptions: GraphOptions;
73
+ readonly mres: MultipartResponse | ServerResponse;
74
+ readonly req: IncomingMessage;
75
+ readonly revisionId?: null | undefined | RevisionId;
76
+ readonly bundlePerfLogger: RootPerfLogger;
77
+ readonly requestStartTimestamp: number;
78
+ };
79
+ type ProcessDeleteContext = {
80
+ readonly graphId: GraphId;
81
+ readonly req: IncomingMessage;
82
+ readonly res: ServerResponse;
83
+ };
84
+ type ProcessEndContext<T> = Omit<
85
+ ProcessStartContext,
86
+ keyof {readonly result: T}
87
+ > & {readonly result: T};
88
+ export type ServerOptions = Readonly<{
89
+ hasReducedPerformance?: boolean;
90
+ onBundleBuilt?: (bundlePath: string) => void;
91
+ watch?: boolean;
92
+ }>;
93
+ type FetchTiming = {
94
+ graphId: GraphId;
95
+ startTime: number;
96
+ endTime: number | null;
97
+ isPrefetch: boolean;
98
+ };
99
+ declare class Server {
100
+ _bundler: IncrementalBundler;
101
+ _config: ConfigT;
102
+ _createModuleId: (path: string) => number;
103
+ _isEnded: boolean;
104
+ _logger: typeof Logger;
105
+ _nextBundleBuildNumber: number;
106
+ _platforms: Set<string>;
107
+ _reporter: Reporter;
108
+ _serverOptions: ServerOptions | void;
109
+ _allowedSuffixesForSourceRequests: ReadonlyArray<string>;
110
+ _sourceRequestRoutingMap: ReadonlyArray<
111
+ [pathnamePrefix: string, normalizedRootDir: string]
112
+ >;
113
+ _fetchTimings: Array<FetchTiming>;
114
+ _activeFetchCount: number;
115
+ constructor(config: ConfigT, options?: ServerOptions);
116
+ end(): void;
117
+ getBundler(): IncrementalBundler;
118
+ getCreateModuleId(): (path: string) => number;
119
+ _serializeGraph(
120
+ $$PARAM_0$$: Readonly<{
121
+ splitOptions: SplitBundleOptions;
122
+ prepend: ReadonlyArray<Module>;
123
+ graph: ReadOnlyGraph;
124
+ }>,
125
+ ): Promise<{code: string; map: string}>;
126
+ build(
127
+ bundleOptions: BundleOptions,
128
+ $$PARAM_1$$?: BuildOptions,
129
+ ): Promise<{code: string; map: string; assets?: ReadonlyArray<AssetData>}>;
130
+ getRamBundleInfo(options: BundleOptions): Promise<RamBundleInfo>;
131
+ getAssets(options: BundleOptions): Promise<ReadonlyArray<AssetData>>;
132
+ _getAssetsFromDependencies(
133
+ dependencies: ReadOnlyDependencies,
134
+ platform: null | undefined | string,
135
+ ): Promise<ReadonlyArray<AssetData>>;
136
+ getOrderedDependencyPaths(options: {
137
+ readonly dev: boolean;
138
+ readonly entryFile: string;
139
+ readonly minify: boolean;
140
+ readonly platform: null | undefined | string;
141
+ }): Promise<Array<string>>;
142
+ _rangeRequestMiddleware(
143
+ req: IncomingMessage,
144
+ res: ServerResponse,
145
+ data: string | Buffer,
146
+ assetPath: string,
147
+ ): Buffer | string;
148
+ _processSingleAssetRequest(
149
+ req: IncomingMessage,
150
+ res: ServerResponse,
151
+ ): Promise<void>;
152
+ processRequest: (
153
+ $$PARAM_0$$: IncomingMessage,
154
+ $$PARAM_1$$: ServerResponse,
155
+ $$PARAM_2$$: (e: null | undefined | Error) => void,
156
+ ) => void;
157
+ _parseOptions(url: string): BundleOptions;
158
+ _rewriteAndNormalizeUrl(requestUrl: string): string;
159
+ _processRequest(
160
+ req: IncomingMessage,
161
+ res: ServerResponse,
162
+ next: ($$PARAM_0$$: null | undefined | Error) => void,
163
+ ): Promise<void>;
164
+ _processSourceRequest(
165
+ relativeFilePathname: string,
166
+ rootDir: string,
167
+ res: ServerResponse,
168
+ ): Promise<void>;
169
+ _createRequestProcessor<T>($$PARAM_0$$: {
170
+ readonly bundleType: 'assets' | 'bundle' | 'map';
171
+ readonly createStartEntry: (
172
+ context: ProcessStartContext,
173
+ ) => ActionLogEntryData;
174
+ readonly createEndEntry: (
175
+ context: ProcessEndContext<T>,
176
+ ) => Partial<ActionStartLogEntry>;
177
+ readonly build: (context: ProcessStartContext) => Promise<T>;
178
+ readonly delete?: (context: ProcessDeleteContext) => Promise<void>;
179
+ readonly finish: (context: ProcessEndContext<T>) => void;
180
+ }): (
181
+ req: IncomingMessage,
182
+ res: ServerResponse,
183
+ bundleOptions: BundleOptions,
184
+ buildContext: Readonly<{
185
+ buildNumber: number;
186
+ bundlePerfLogger: RootPerfLogger;
187
+ }>,
188
+ ) => Promise<void>;
189
+ _processBundleRequest: (
190
+ req: IncomingMessage,
191
+ res: ServerResponse,
192
+ bundleOptions: BundleOptions,
193
+ buildContext: Readonly<{
194
+ buildNumber: number;
195
+ bundlePerfLogger: RootPerfLogger;
196
+ }>,
197
+ ) => Promise<void>;
198
+ _getSortedModules(graph: ReadOnlyGraph): ReadonlyArray<Module>;
199
+ _processSourceMapRequest: (
200
+ req: IncomingMessage,
201
+ res: ServerResponse,
202
+ bundleOptions: BundleOptions,
203
+ buildContext: Readonly<{
204
+ buildNumber: number;
205
+ bundlePerfLogger: RootPerfLogger;
206
+ }>,
207
+ ) => Promise<void>;
208
+ _processAssetsRequest: (
209
+ req: IncomingMessage,
210
+ res: ServerResponse,
211
+ bundleOptions: BundleOptions,
212
+ buildContext: Readonly<{
213
+ buildNumber: number;
214
+ bundlePerfLogger: RootPerfLogger;
215
+ }>,
216
+ ) => Promise<void>;
217
+ _symbolicate(req: IncomingMessage, res: ServerResponse): Promise<void>;
218
+ _explodedSourceMapForBundleOptions(
219
+ bundleOptions: BundleOptions,
220
+ ): Promise<ExplodedSourceMap>;
221
+ _resolveRelativePath(
222
+ filePath: string,
223
+ $$PARAM_1$$: Readonly<{
224
+ relativeTo: 'project' | 'server';
225
+ resolverOptions: ResolverInputOptions;
226
+ transformOptions: TransformInputOptions;
227
+ }>,
228
+ ): Promise<string>;
229
+ getNewBuildNumber(): number;
230
+ getPlatforms(): ReadonlyArray<string>;
231
+ getWatchFolders(): ReadonlyArray<string>;
232
+ static DEFAULT_GRAPH_OPTIONS: Readonly<{
233
+ customResolverOptions: CustomResolverOptions;
234
+ customTransformOptions: CustomTransformOptions;
235
+ dev: boolean;
236
+ minify: boolean;
237
+ unstable_transformProfile: 'default';
238
+ }>;
239
+ static DEFAULT_BUNDLE_OPTIONS: Omit<
240
+ typeof Server.DEFAULT_GRAPH_OPTIONS,
241
+ keyof {
242
+ excludeSource: false;
243
+ inlineSourceMap: false;
244
+ lazy: false;
245
+ modulesOnly: false;
246
+ onProgress: null;
247
+ runModule: true;
248
+ shallow: false;
249
+ sourceMapUrl: null;
250
+ sourceUrl: null;
251
+ sourcePaths: SourcePathsMode;
252
+ }
253
+ > & {
254
+ excludeSource: false;
255
+ inlineSourceMap: false;
256
+ lazy: false;
257
+ modulesOnly: false;
258
+ onProgress: null;
259
+ runModule: true;
260
+ shallow: false;
261
+ sourceMapUrl: null;
262
+ sourceUrl: null;
263
+ sourcePaths: SourcePathsMode;
264
+ };
265
+ _getServerRootDir(): string;
266
+ _getEntryPointAbsolutePath(entryFile: string): string;
267
+ ready(): Promise<void>;
268
+ _shouldAddModuleToIgnoreList(module: Module): boolean;
269
+ _getModuleSourceUrl(module: Module, mode: SourcePathsMode): string;
270
+ }
271
+ export default Server;
package/src/Server.js CHANGED
@@ -25,6 +25,7 @@ var _IncrementalBundler = _interopRequireDefault(
25
25
  var _ResourceNotFoundError = _interopRequireDefault(
26
26
  require("./IncrementalBundler/ResourceNotFoundError"),
27
27
  );
28
+ var _bundleProgressUtils = require("./lib/bundleProgressUtils");
28
29
  var _bundleToString = _interopRequireDefault(require("./lib/bundleToString"));
29
30
  var _formatBundlingError = _interopRequireDefault(
30
31
  require("./lib/formatBundlingError"),
@@ -66,10 +67,7 @@ function _interopRequireWildcard(e, t) {
66
67
  if (!t && e && e.__esModule) return e;
67
68
  var o,
68
69
  i,
69
- f = {
70
- __proto__: null,
71
- default: e,
72
- };
70
+ f = { __proto__: null, default: e };
73
71
  if (null === e || ("object" != typeof e && "function" != typeof e))
74
72
  return f;
75
73
  if ((o = t ? n : r)) {
@@ -89,11 +87,7 @@ function _interopRequireWildcard(e, t) {
89
87
  })(e, t);
90
88
  }
91
89
  function _interopRequireDefault(e) {
92
- return e && e.__esModule
93
- ? e
94
- : {
95
- default: e,
96
- };
90
+ return e && e.__esModule ? e : { default: e };
97
91
  }
98
92
  const debug = require("debug")("Metro:Server");
99
93
  const { createActionStartEntry, createActionEndEntry, log } = _metroCore.Logger;
@@ -136,6 +130,8 @@ class Server {
136
130
  ]),
137
131
  ];
138
132
  this._isEnded = false;
133
+ this._fetchTimings = [];
134
+ this._activeFetchCount = 0;
139
135
  this._createModuleId = config.serializer.createModuleIdFactory();
140
136
  this._bundler = new _IncrementalBundler.default(config, {
141
137
  hasReducedPerformance: options && options.hasReducedPerformance,
@@ -684,14 +680,16 @@ class Server {
684
680
  }
685
681
  const mres = _MultipartResponse.default.wrapIfSupported(req, res);
686
682
  let onProgress = null;
687
- let lastProgress = -1;
683
+ let lastRatio = -1;
688
684
  if (this._config.reporter) {
689
685
  onProgress = (transformedFileCount, totalFileCount) => {
690
- const currentProgress = parseInt(
691
- (transformedFileCount / totalFileCount) * 100,
692
- 10,
686
+ const newRatio = (0,
687
+ _bundleProgressUtils.calculateBundleProgressRatio)(
688
+ transformedFileCount,
689
+ totalFileCount,
690
+ lastRatio,
693
691
  );
694
- if (currentProgress > lastProgress || totalFileCount < 10) {
692
+ if (newRatio > lastRatio) {
695
693
  if (mres instanceof _MultipartResponse.default) {
696
694
  mres.writeChunk(
697
695
  {
@@ -700,13 +698,14 @@ class Server {
700
698
  JSON.stringify({
701
699
  done: transformedFileCount,
702
700
  total: totalFileCount,
701
+ percent: Math.floor(newRatio * 100),
703
702
  }),
704
703
  );
705
704
  }
706
705
  if (res.socket != null && res.socket.uncork != null) {
707
706
  res.socket.uncork();
708
707
  }
709
- lastProgress = currentProgress;
708
+ lastRatio = newRatio;
710
709
  }
711
710
  this._reporter.update({
712
711
  buildID: getBuildID(buildNumber),
@@ -748,8 +747,16 @@ class Server {
748
747
  const logEntry = log(
749
748
  createActionStartEntry(createStartEntry(startContext)),
750
749
  );
750
+ const fetchTiming = {
751
+ graphId,
752
+ startTime: requestStartTimestamp,
753
+ endTime: null,
754
+ isPrefetch: req.method === "HEAD",
755
+ };
751
756
  let result;
752
757
  try {
758
+ this._fetchTimings.push(fetchTiming);
759
+ this._activeFetchCount++;
753
760
  result = await build(startContext);
754
761
  } catch (error) {
755
762
  const formattedError = (0, _formatBundlingError.default)(error);
@@ -779,6 +786,31 @@ class Server {
779
786
  debug("Bundling error", error);
780
787
  buildContext.bundlePerfLogger.end("FAIL");
781
788
  return;
789
+ } finally {
790
+ fetchTiming.endTime =
791
+ _perf_hooks.performance.timeOrigin + _perf_hooks.performance.now();
792
+ if (!fetchTiming.isPrefetch) {
793
+ buildContext.bundlePerfLogger.annotate({
794
+ bool: {
795
+ had_competing_prefetch: this._fetchTimings
796
+ .filter((t) => t.isPrefetch && t.graphId !== graphId)
797
+ .some((prefetch) => {
798
+ const prefetchEndTime =
799
+ prefetch.endTime ?? Number.MAX_SAFE_INTEGER;
800
+ const fetchEndTime =
801
+ fetchTiming.endTime ?? Number.MAX_SAFE_INTEGER;
802
+ return (
803
+ prefetch.startTime < fetchEndTime &&
804
+ prefetchEndTime > fetchTiming.startTime
805
+ );
806
+ }),
807
+ },
808
+ });
809
+ }
810
+ this._activeFetchCount--;
811
+ if (this._activeFetchCount === 0) {
812
+ this._fetchTimings = [];
813
+ }
782
814
  }
783
815
  const endContext = {
784
816
  ...startContext,