metro 0.72.1 → 0.72.3

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 (63) hide show
  1. package/package.json +23 -21
  2. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js +3 -2
  3. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js.flow +2 -1
  4. package/src/DeltaBundler/Serializers/helpers/bytecode.js +2 -2
  5. package/src/DeltaBundler/Serializers/helpers/bytecode.js.flow +2 -1
  6. package/src/DeltaBundler/Worker.js +0 -1
  7. package/src/DeltaBundler/Worker.js.flow +0 -1
  8. package/src/DeltaBundler/graphOperations.js +6 -6
  9. package/src/DeltaBundler/graphOperations.js.flow +5 -5
  10. package/src/DeltaBundler.js.flow +1 -1
  11. package/src/HmrServer.js +9 -5
  12. package/src/HmrServer.js.flow +9 -4
  13. package/src/IncrementalBundler.js +16 -4
  14. package/src/IncrementalBundler.js.flow +17 -4
  15. package/src/ModuleGraph/node-haste/HasteFS.js +0 -1
  16. package/src/ModuleGraph/node-haste/HasteFS.js.flow +0 -1
  17. package/src/ModuleGraph/node-haste/Package.js.flow +5 -5
  18. package/src/ModuleGraph/node-haste/node-haste.js +8 -4
  19. package/src/ModuleGraph/node-haste/node-haste.js.flow +21 -14
  20. package/src/ModuleGraph/output/reverse-dependency-map-references.js +1 -2
  21. package/src/ModuleGraph/output/reverse-dependency-map-references.js.flow +0 -1
  22. package/src/ModuleGraph/output/util.js +2 -4
  23. package/src/ModuleGraph/output/util.js.flow +1 -2
  24. package/src/ModuleGraph/types.flow.js.flow +28 -6
  25. package/src/Server/MultipartResponse.js +10 -12
  26. package/src/Server/MultipartResponse.js.flow +97 -0
  27. package/src/Server.js +100 -48
  28. package/src/Server.js.flow +118 -49
  29. package/src/index.flow.js +16 -8
  30. package/src/index.flow.js.flow +14 -8
  31. package/src/index.js +0 -1
  32. package/src/index.js.flow +0 -1
  33. package/src/lib/CountingSet.js +0 -1
  34. package/src/lib/CountingSet.js.flow +0 -1
  35. package/src/lib/RamBundleParser.js +1 -0
  36. package/src/lib/RamBundleParser.js.flow +1 -0
  37. package/src/lib/TerminalReporter.js +22 -24
  38. package/src/lib/TerminalReporter.js.flow +20 -24
  39. package/src/lib/bundleToBytecode.js +3 -2
  40. package/src/lib/bundleToBytecode.js.flow +2 -2
  41. package/src/lib/getGraphId.js +16 -3
  42. package/src/lib/getGraphId.js.flow +10 -10
  43. package/src/lib/getPrependedScripts.js +13 -5
  44. package/src/lib/getPrependedScripts.js.flow +6 -1
  45. package/src/lib/parseCustomResolverOptions.js +26 -0
  46. package/src/lib/parseCustomResolverOptions.js.flow +38 -0
  47. package/src/lib/parseOptionsFromUrl.js +3 -0
  48. package/src/lib/parseOptionsFromUrl.js.flow +2 -0
  49. package/src/lib/splitBundleOptions.js +3 -0
  50. package/src/lib/splitBundleOptions.js.flow +3 -0
  51. package/src/lib/transformHelpers.js +27 -13
  52. package/src/lib/transformHelpers.js.flow +27 -7
  53. package/src/node-haste/DependencyGraph/ModuleResolution.js +18 -2
  54. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +5 -0
  55. package/src/node-haste/DependencyGraph.js +34 -9
  56. package/src/node-haste/DependencyGraph.js.flow +49 -11
  57. package/src/node-haste/Module.js +1 -0
  58. package/src/node-haste/Module.js.flow +1 -0
  59. package/src/node-haste/Package.js +0 -1
  60. package/src/node-haste/Package.js.flow +0 -1
  61. package/src/shared/output/bundle.js +0 -1
  62. package/src/shared/output/bundle.js.flow +0 -1
  63. package/src/shared/types.flow.js.flow +7 -0
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  import type {Moduleish} from '../../node-haste/DependencyGraph/ModuleResolution';
12
+ import type {ResolverInputOptions} from '../../shared/types.flow';
12
13
  import type {ResolveFn, TransformedCodeFile} from '../types.flow';
13
14
  import type {Path} from './node-haste.flow';
14
15
  import type {ModuleMapData, ModuleMapItem} from 'metro-file-map';
@@ -26,13 +27,7 @@ const ModuleCache = require('./ModuleCache');
26
27
  const defaults = require('metro-config/src/defaults/defaults');
27
28
  const path = require('path');
28
29
 
29
- type ResolveOptions = {
30
- /**
31
- * (Used by the resolver) The extensions tried (in order) to implicitly
32
- * locate a source file.
33
- */
34
- sourceExts: $ReadOnlyArray<string>,
35
-
30
+ type ResolveOptions = $ReadOnly<{
36
31
  /**
37
32
  * The additional extensions to include in the file map as source files that
38
33
  * can be explicitly imported.
@@ -41,16 +36,24 @@ type ResolveOptions = {
41
36
 
42
37
  assetExts: $ReadOnlyArray<string>,
43
38
  assetResolutions: $ReadOnlyArray<string>,
44
- +disableHierarchicalLookup: boolean,
45
- +emptyModulePath: string,
39
+ disableHierarchicalLookup: boolean,
40
+ emptyModulePath: string,
46
41
  extraNodeModules: {[id: string]: string, ...},
47
42
  mainFields: $ReadOnlyArray<string>,
48
43
  nodeModulesPaths: $ReadOnlyArray<string>,
49
- +platform: string,
44
+ platform: string,
50
45
  platforms?: $ReadOnlyArray<string>,
51
46
  resolveRequest?: ?CustomResolver,
47
+ resolverOptions: ResolverInputOptions,
48
+
49
+ /**
50
+ * (Used by the resolver) The extensions tried (in order) to implicitly
51
+ * locate a source file.
52
+ */
53
+ sourceExts: $ReadOnlyArray<string>,
54
+
52
55
  transformedFiles: {[path: Path]: TransformedCodeFile, ...},
53
- };
56
+ }>;
54
57
 
55
58
  const NATIVE_PLATFORM = 'native';
56
59
  const GENERIC_PLATFORM = 'g';
@@ -173,7 +176,6 @@ exports.createResolveFn = function (options: ResolveOptions): ResolveFn {
173
176
  extraNodeModules,
174
177
  isAssetFile,
175
178
  mainFields: options.mainFields,
176
- // $FlowFixMe -- error revealed by types-first codemod
177
179
  moduleCache,
178
180
  moduleMap: new ModuleMap({
179
181
  duplicates: new Map(),
@@ -213,7 +215,12 @@ exports.createResolveFn = function (options: ResolveOptions): ResolveFn {
213
215
  sourcePath != null
214
216
  ? new Module(sourcePath, moduleCache, getTransformedFile(sourcePath))
215
217
  : NULL_MODULE;
216
- // $FlowFixMe -- error revealed by types-first codemod
217
- return moduleResolver.resolveDependency(from, id, true, platform).path;
218
+ return moduleResolver.resolveDependency(
219
+ from,
220
+ id,
221
+ true,
222
+ platform,
223
+ options.resolverOptions,
224
+ ).path;
218
225
  };
219
226
  };
@@ -25,8 +25,7 @@ function reverseDependencyMapReferences({ types: t }) {
25
25
 
26
26
  if (node.callee.name === `${state.opts.globalPrefix}__d`) {
27
27
  // $FlowFixMe Flow error uncovered by typing Babel more strictly
28
- const lastArg = node.arguments[0].params.slice(-1)[0]; // $FlowFixMe Flow error uncovered by typing Babel more strictly
29
-
28
+ const lastArg = node.arguments[0].params.slice(-1)[0];
30
29
  const depMapName = lastArg && lastArg.name;
31
30
 
32
31
  if (depMapName == null) {
@@ -37,7 +37,6 @@ function reverseDependencyMapReferences({types: t}: {types: Types, ...}): {
37
37
  if (node.callee.name === `${state.opts.globalPrefix}__d`) {
38
38
  // $FlowFixMe Flow error uncovered by typing Babel more strictly
39
39
  const lastArg = node.arguments[0].params.slice(-1)[0];
40
- // $FlowFixMe Flow error uncovered by typing Babel more strictly
41
40
  const depMapName: ?string = lastArg && lastArg.name;
42
41
 
43
42
  if (depMapName == null) {
@@ -17,9 +17,7 @@ const mergeSourceMaps = require("../worker/mergeSourceMaps");
17
17
 
18
18
  const reverseDependencyMapReferences = require("./reverse-dependency-map-references");
19
19
 
20
- const { parseSync, transformFromAstSync } = require("@babel/core");
21
-
22
- const HermesParser = require("hermes-parser"); // flowlint-next-line untyped-import:off
20
+ const { parseSync, transformFromAstSync } = require("@babel/core"); // flowlint-next-line untyped-import:off
23
21
 
24
22
  const { passthroughSyntaxPlugins } = require("metro-react-native-babel-preset");
25
23
 
@@ -179,7 +177,7 @@ function inlineModuleIds(
179
177
  const sourceAst =
180
178
  isTypeScriptSource(path) || isTSXSource(path) || !hermesParser
181
179
  ? parseSync(code, babelConfig)
182
- : HermesParser.parse(code, {
180
+ : require("hermes-parser").parse(code, {
183
181
  babel: true,
184
182
  // $FlowFixMe[prop-missing]
185
183
  sourceType: babelConfig.sourceType,
@@ -18,7 +18,6 @@ const generate = require('../worker/generate');
18
18
  const mergeSourceMaps = require('../worker/mergeSourceMaps');
19
19
  const reverseDependencyMapReferences = require('./reverse-dependency-map-references');
20
20
  const {parseSync, transformFromAstSync} = require('@babel/core');
21
- const HermesParser = require('hermes-parser');
22
21
  // flowlint-next-line untyped-import:off
23
22
  const {passthroughSyntaxPlugins} = require('metro-react-native-babel-preset');
24
23
  const {addParamsToDefineCall} = require('metro-transform-plugins');
@@ -179,7 +178,7 @@ function inlineModuleIds(
179
178
  const sourceAst =
180
179
  isTypeScriptSource(path) || isTSXSource(path) || !hermesParser
181
180
  ? parseSync(code, babelConfig)
182
- : HermesParser.parse(code, {
181
+ : require('hermes-parser').parse(code, {
183
182
  babel: true,
184
183
  // $FlowFixMe[prop-missing]
185
184
  sourceType: babelConfig.sourceType,
@@ -117,11 +117,15 @@ export type OutputResult<M: MixedSourceMap> = {
117
117
  map: M,
118
118
  };
119
119
 
120
+ export type Replacements = $ReadOnly<{
121
+ [filename: string]: string | false,
122
+ }>;
123
+
120
124
  export type PackageData = {
121
- browser?: Object | string,
125
+ browser?: Replacements | string,
122
126
  main?: string,
123
127
  name?: string,
124
- 'react-native'?: Object | string,
128
+ 'react-native'?: Replacements | string,
125
129
  };
126
130
 
127
131
  export type ResolveFn = (id: string, source: ?string) => string;
@@ -259,14 +263,27 @@ export type AssetContentsByPath = {
259
263
  };
260
264
 
261
265
  export type ResolvedCodeFile = {
262
- +codeFile: TransformedCodeFile,
266
+ codeFile: TransformedCodeFile,
263
267
  /**
264
268
  * Imagine we have a source file that contains a `require('foo')`. The library
265
269
  * will resolve the path of the module `foo` and store it in this field along
266
270
  * all the other dependencies. For example, it could be
267
271
  * `{'foo': 'bar/foo.js', 'bar': 'node_modules/bar/index.js'}`.
268
272
  */
269
- +filePathsByDependencyName: {[dependencyName: string]: string, ...},
273
+ filePathsByDependencyName: {
274
+ [dependencyName: string]:
275
+ | string
276
+
277
+ // requireCond
278
+ | {
279
+ type: string,
280
+ condition: string,
281
+ modules: {
282
+ [string]: string | null,
283
+ },
284
+ },
285
+ ...
286
+ },
270
287
  };
271
288
 
272
289
  export type LibraryBoundCodeFile = {
@@ -294,8 +311,13 @@ export type Library = {
294
311
  * path it resolves to, ex. `beep/glo/foo.js`.
295
312
  */
296
313
  export type ResolvedLibrary = {
297
- +files: $ReadOnlyArray<ResolvedCodeFile>,
314
+ +files: $ReadOnlyArray<$DeepReadOnly<ResolvedCodeFile>>,
298
315
  /* cannot be a Map because it's JSONified later on */
299
316
  +assets: AssetContentsByPath,
300
- +isPartiallyResolved?: boolean,
301
317
  };
318
+
319
+ type DeepReadOnlyFn = (<T>(Array<T>) => $ReadOnlyArray<$DeepReadOnly<T>>) &
320
+ (<T: {}>(T) => $ReadOnly<$ObjMap<T, DeepReadOnlyFn>>) &
321
+ (<T>(T) => T);
322
+
323
+ type $DeepReadOnly<T> = $Call<DeepReadOnlyFn, T>;
@@ -4,6 +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
+ *
7
8
  * @format
8
9
  */
9
10
  "use strict";
@@ -14,17 +15,20 @@ const CRLF = "\r\n";
14
15
  const BOUNDARY = "3beqjf3apnqeu3h5jqorms4i";
15
16
 
16
17
  class MultipartResponse {
17
- static wrap(req, res) {
18
+ static wrapIfSupported(req, res) {
18
19
  if (accepts(req).types().includes("multipart/mixed")) {
19
20
  return new MultipartResponse(res);
20
- } // Ugly hack, ideally wrap function should always return a proxy
21
- // object with the same interface
22
-
23
- res.writeChunk = () => {}; // noop
21
+ }
24
22
 
25
23
  return res;
26
24
  }
27
25
 
26
+ static serializeHeaders(headers) {
27
+ return Object.keys(headers)
28
+ .map((key) => `${key}: ${headers[key]}`)
29
+ .join(CRLF);
30
+ }
31
+
28
32
  constructor(res) {
29
33
  this.res = res;
30
34
  this.headers = {};
@@ -47,7 +51,7 @@ class MultipartResponse {
47
51
  this.res.write(MultipartResponse.serializeHeaders(headers) + CRLF + CRLF);
48
52
  }
49
53
 
50
- if (data) {
54
+ if (data != null) {
51
55
  this.res.write(data);
52
56
  }
53
57
 
@@ -78,12 +82,6 @@ class MultipartResponse {
78
82
  this.writeChunk(this.headers, data, true);
79
83
  this.res.end();
80
84
  }
81
-
82
- static serializeHeaders(headers) {
83
- return Object.keys(headers)
84
- .map((key) => `${key}: ${headers[key]}`)
85
- .join(CRLF);
86
- }
87
85
  }
88
86
 
89
87
  module.exports = MultipartResponse;
@@ -0,0 +1,97 @@
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
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+ import type {IncomingMessage, ServerResponse} from 'http';
13
+ const accepts = require('accepts');
14
+
15
+ const CRLF = '\r\n';
16
+ const BOUNDARY = '3beqjf3apnqeu3h5jqorms4i';
17
+ type Data = string | Buffer | Uint8Array;
18
+ type Headers = {[string]: string | number};
19
+
20
+ class MultipartResponse {
21
+ static wrapIfSupported(
22
+ req: IncomingMessage,
23
+ res: ServerResponse,
24
+ ): MultipartResponse | ServerResponse {
25
+ if (accepts(req).types().includes('multipart/mixed')) {
26
+ return new MultipartResponse(res);
27
+ }
28
+
29
+ return res;
30
+ }
31
+
32
+ static serializeHeaders(headers: Headers): string {
33
+ return Object.keys(headers)
34
+ .map(key => `${key}: ${headers[key]}`)
35
+ .join(CRLF);
36
+ }
37
+
38
+ res: ServerResponse;
39
+ headers: Headers;
40
+
41
+ constructor(res: ServerResponse) {
42
+ this.res = res;
43
+ this.headers = {};
44
+ res.writeHead(200, {
45
+ 'Content-Type': `multipart/mixed; boundary="${BOUNDARY}"`,
46
+ });
47
+ res.write(
48
+ 'If you are seeing this, your client does not support multipart response',
49
+ );
50
+ }
51
+
52
+ writeChunk(
53
+ headers: Headers | null,
54
+ data?: Data,
55
+ isLast?: boolean = false,
56
+ ): void {
57
+ if (this.res.finished) {
58
+ return;
59
+ }
60
+
61
+ this.res.write(`${CRLF}--${BOUNDARY}${CRLF}`);
62
+ if (headers) {
63
+ this.res.write(MultipartResponse.serializeHeaders(headers) + CRLF + CRLF);
64
+ }
65
+
66
+ if (data != null) {
67
+ this.res.write(data);
68
+ }
69
+
70
+ if (isLast) {
71
+ this.res.write(`${CRLF}--${BOUNDARY}--${CRLF}`);
72
+ }
73
+ }
74
+
75
+ writeHead(status: number, headers?: Headers): void {
76
+ // We can't actually change the response HTTP status code
77
+ // because the headers have already been sent
78
+ this.setHeader('X-Http-Status', status);
79
+ if (!headers) {
80
+ return;
81
+ }
82
+ for (const key in headers) {
83
+ this.setHeader(key, headers[key]);
84
+ }
85
+ }
86
+
87
+ setHeader(name: string, value: string | number): void {
88
+ this.headers[name] = value;
89
+ }
90
+
91
+ end(data?: Data): void {
92
+ this.writeChunk(this.headers, data, true);
93
+ this.res.end();
94
+ }
95
+ }
96
+
97
+ module.exports = MultipartResponse;