metro 0.76.0 → 0.76.2

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 (84) hide show
  1. package/package.json +22 -24
  2. package/src/Asset.d.ts +25 -0
  3. package/src/Bundler.d.ts +39 -0
  4. package/src/DeltaBundler/Graph.d.ts +40 -0
  5. package/src/DeltaBundler/Serializers/baseJSBundle.js +1 -0
  6. package/src/DeltaBundler/Serializers/baseJSBundle.js.flow +1 -0
  7. package/src/DeltaBundler/Serializers/getRamBundleInfo.d.ts +18 -0
  8. package/src/DeltaBundler/Serializers/helpers/js.js +22 -6
  9. package/src/DeltaBundler/Serializers/helpers/js.js.flow +24 -6
  10. package/src/DeltaBundler/Serializers/helpers/processModules.js +2 -0
  11. package/src/DeltaBundler/Serializers/helpers/processModules.js.flow +3 -0
  12. package/src/DeltaBundler/Serializers/hmrJSBundle.js +1 -0
  13. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +1 -0
  14. package/src/DeltaBundler/Transformer.js +1 -2
  15. package/src/DeltaBundler/Transformer.js.flow +1 -2
  16. package/src/DeltaBundler/Worker.d.ts +47 -0
  17. package/src/DeltaBundler/types.d.ts +169 -0
  18. package/src/DeltaBundler.d.ts +58 -0
  19. package/src/HmrServer.js +1 -5
  20. package/src/HmrServer.js.flow +0 -5
  21. package/src/IncrementalBundler.d.ts +97 -0
  22. package/src/IncrementalBundler.js +1 -0
  23. package/src/IncrementalBundler.js.flow +1 -0
  24. package/src/ModuleGraph/worker/collectDependencies.d.ts +27 -0
  25. package/src/ModuleGraph/worker/collectDependencies.js +42 -25
  26. package/src/ModuleGraph/worker/collectDependencies.js.flow +41 -36
  27. package/src/Server/MultipartResponse.d.ts +31 -0
  28. package/src/Server/symbolicate.js +33 -5
  29. package/src/Server/symbolicate.js.flow +40 -9
  30. package/src/Server.d.ts +113 -0
  31. package/src/Server.js +16 -136
  32. package/src/Server.js.flow +16 -156
  33. package/src/cli/parseKeyValueParamArray.js +34 -0
  34. package/src/cli/parseKeyValueParamArray.js.flow +31 -0
  35. package/src/cli-utils.js.flow +2 -6
  36. package/src/cli.js +2 -0
  37. package/src/cli.js.flow +1 -0
  38. package/src/commands/build.js +34 -7
  39. package/src/commands/build.js.flow +50 -8
  40. package/src/commands/dependencies.js +11 -9
  41. package/src/commands/dependencies.js.flow +128 -0
  42. package/src/commands/serve.js +11 -4
  43. package/src/commands/serve.js.flow +26 -6
  44. package/src/index.d.ts +161 -0
  45. package/src/index.flow.js +17 -4
  46. package/src/index.flow.js.flow +41 -10
  47. package/src/lib/CountingSet.d.ts +48 -0
  48. package/src/lib/TerminalReporter.d.ts +27 -0
  49. package/src/lib/TerminalReporter.js +1 -4
  50. package/src/lib/TerminalReporter.js.flow +1 -5
  51. package/src/lib/contextModule.d.ts +22 -0
  52. package/src/lib/getGraphId.d.ts +11 -0
  53. package/src/lib/getGraphId.js +0 -1
  54. package/src/lib/getGraphId.js.flow +0 -1
  55. package/src/lib/getPrependedScripts.js +0 -9
  56. package/src/lib/getPrependedScripts.js.flow +0 -9
  57. package/src/lib/parseOptionsFromUrl.js +1 -16
  58. package/src/lib/parseOptionsFromUrl.js.flow +0 -17
  59. package/src/lib/reporting.d.ts +140 -0
  60. package/src/lib/reporting.js.flow +0 -1
  61. package/src/lib/splitBundleOptions.js +0 -1
  62. package/src/lib/splitBundleOptions.js.flow +0 -1
  63. package/src/lib/transformHelpers.js +0 -1
  64. package/src/lib/transformHelpers.js.flow +0 -1
  65. package/src/node-haste/DependencyGraph/createHasteMap.js +0 -1
  66. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +0 -1
  67. package/src/node-haste/DependencyGraph.d.ts +59 -0
  68. package/src/node-haste/DependencyGraph.js +7 -1
  69. package/src/node-haste/DependencyGraph.js.flow +8 -1
  70. package/src/shared/output/RamBundle/as-assets.js +1 -0
  71. package/src/shared/output/RamBundle/as-assets.js.flow +1 -0
  72. package/src/shared/output/RamBundle/write-sourcemap.js +1 -0
  73. package/src/shared/output/RamBundle/write-sourcemap.js.flow +1 -0
  74. package/src/shared/output/bundle.d.ts +31 -0
  75. package/src/shared/types.d.ts +138 -0
  76. package/src/shared/types.flow.js.flow +2 -2
  77. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js +0 -81
  78. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js.flow +0 -103
  79. package/src/DeltaBundler/Serializers/helpers/bytecode.js +0 -61
  80. package/src/DeltaBundler/Serializers/helpers/bytecode.js.flow +0 -75
  81. package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js +0 -40
  82. package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js.flow +0 -51
  83. package/src/lib/bundleToBytecode.js +0 -79
  84. package/src/lib/bundleToBytecode.js.flow +0 -97
@@ -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
+ * @flow strict-local
8
+ * @format
9
+ * @oncall react_native
10
+ */
11
+
12
+ import querystring from 'querystring';
13
+
14
+ export default function coerceKeyValueArray(
15
+ keyValueArray: $ReadOnlyArray<string>,
16
+ ): {
17
+ [key: string]: string,
18
+ __proto__: null,
19
+ } {
20
+ const result: {[key: string]: string, __proto__: null} = Object.create(null);
21
+ for (const item of keyValueArray) {
22
+ if (item.indexOf('=') === -1) {
23
+ throw new Error('Expected parameter to include "=" but found: ' + item);
24
+ }
25
+ if (item.indexOf('&') !== -1) {
26
+ throw new Error('Parameter cannot include "&" but found: ' + item);
27
+ }
28
+ Object.assign(result, querystring.parse(item));
29
+ }
30
+ return result;
31
+ }
@@ -11,8 +11,6 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- import type {YargArguments} from 'metro-config/src/configTypes.flow';
15
-
16
14
  const fs = require('fs');
17
15
 
18
16
  exports.watchFile = async function (
@@ -27,10 +25,8 @@ exports.watchFile = async function (
27
25
  };
28
26
 
29
27
  exports.makeAsyncCommand =
30
- (
31
- command: (argv: YargArguments) => Promise<void>,
32
- ): ((argv: YargArguments) => void) =>
33
- (argv: YargArguments) => {
28
+ <T>(command: (argv: T) => Promise<void>): ((argv: T) => void) =>
29
+ (argv: T) => {
34
30
  Promise.resolve(command(argv)).catch(error => {
35
31
  console.error(error.stack);
36
32
  process.exitCode = 1;
package/src/cli.js CHANGED
@@ -17,4 +17,6 @@ try {
17
17
  } catch {}
18
18
  const { attachMetroCli } = require("./index");
19
19
  const yargs = require("yargs");
20
+
21
+ // $FlowFixMe[unused-promise]
20
22
  attachMetroCli(yargs.demandCommand(1)).argv;
package/src/cli.js.flow CHANGED
@@ -20,4 +20,5 @@ try {
20
20
  const {attachMetroCli} = require('./index');
21
21
  const yargs = require('yargs');
22
22
 
23
+ // $FlowFixMe[unused-promise]
23
24
  attachMetroCli(yargs.demandCommand(1)).argv;
@@ -1,3 +1,11 @@
1
+ "use strict";
2
+
3
+ var _parseKeyValueParamArray = _interopRequireDefault(
4
+ require("../cli/parseKeyValueParamArray")
5
+ );
6
+ function _interopRequireDefault(obj) {
7
+ return obj && obj.__esModule ? obj : { default: obj };
8
+ }
1
9
  /**
2
10
  * Copyright (c) Meta Platforms, Inc. and affiliates.
3
11
  *
@@ -9,8 +17,6 @@
9
17
  * @oncall react_native
10
18
  */
11
19
 
12
- "use strict";
13
-
14
20
  const { makeAsyncCommand } = require("../cli-utils");
15
21
  const TerminalReporter = require("../lib/TerminalReporter");
16
22
  const { loadConfig } = require("metro-config");
@@ -64,6 +70,21 @@ module.exports = () => ({
64
70
  alias: "c",
65
71
  type: "string",
66
72
  });
73
+ yargs.option("transform-option", {
74
+ type: "string",
75
+ array: true,
76
+ alias: "transformer-option",
77
+ coerce: _parseKeyValueParamArray.default,
78
+ describe:
79
+ "Custom transform options of the form key=value. URL-encoded. May be specified multiple times.",
80
+ });
81
+ yargs.option("resolver-option", {
82
+ type: "string",
83
+ array: true,
84
+ coerce: _parseKeyValueParamArray.default,
85
+ describe:
86
+ "Custom resolver options of the form key=value. URL-encoded. May be specified multiple times.",
87
+ });
67
88
 
68
89
  // Deprecated
69
90
  yargs.option("reset-cache", {
@@ -72,8 +93,17 @@ module.exports = () => ({
72
93
  },
73
94
  handler: makeAsyncCommand(async (argv) => {
74
95
  const config = await loadConfig(argv);
75
- // $FlowExpectedError YargArguments and RunBuildOptions are used interchangeable but their types are not yet compatible
76
- const options = argv;
96
+ const options = {
97
+ entry: argv.entry,
98
+ dev: argv.dev,
99
+ out: argv.out,
100
+ minify: argv.minify,
101
+ platform: argv.platform,
102
+ sourceMap: argv.sourceMap,
103
+ sourceMapUrl: argv.sourceMapUrl,
104
+ customResolverOptions: argv.resolverOption,
105
+ customTransformOptions: argv.transformOption,
106
+ };
77
107
 
78
108
  // Inline require() to avoid circular dependency with ../index
79
109
  const MetroApi = require("../index");
@@ -89,9 +119,6 @@ module.exports = () => ({
89
119
  entryFile: options.entry,
90
120
  minify: !!options.minify,
91
121
  platform: options.platform,
92
- // Bytecode bundles in Metro are not meant for production use. Instead,
93
- // the Hermes Bytecode Compiler should be invoked on the resulting JS bundle from Metro.
94
- runtimeBytecodeVersion: null,
95
122
  },
96
123
  });
97
124
  },
@@ -9,10 +9,11 @@
9
9
  * @oncall react_native
10
10
  */
11
11
 
12
- 'use strict';
12
+ import parseKeyValueParamArray from '../cli/parseKeyValueParamArray';
13
13
 
14
+ import type {CustomTransformOptions} from 'metro-babel-transformer';
15
+ import type {CustomResolverOptions} from 'metro-resolver';
14
16
  import type {RunBuildOptions} from '../index';
15
- import type {YargArguments} from 'metro-config/src/configTypes.flow';
16
17
  import typeof Yargs from 'yargs';
17
18
  import type {ModuleObject} from 'yargs';
18
19
 
@@ -24,6 +25,24 @@ const {Terminal} = require('metro-core');
24
25
  const term = new Terminal(process.stdout);
25
26
  const updateReporter = new TerminalReporter(term);
26
27
 
28
+ type Args = $ReadOnly<{
29
+ config?: string,
30
+ dev?: boolean,
31
+ entry: string,
32
+ legacyBundler?: boolean,
33
+ maxWorkers?: number,
34
+ minify?: boolean,
35
+ out: string,
36
+ outputType?: string,
37
+ platform?: string,
38
+ projectRoots?: $ReadOnlyArray<string>,
39
+ resetCache?: boolean,
40
+ sourceMap?: boolean,
41
+ sourceMapUrl?: string,
42
+ transformOption: CustomTransformOptions,
43
+ resolverOption: CustomResolverOptions,
44
+ }>;
45
+
27
46
  module.exports = (): {
28
47
  ...ModuleObject,
29
48
  handler: Function,
@@ -54,14 +73,40 @@ module.exports = (): {
54
73
 
55
74
  yargs.option('config', {alias: 'c', type: 'string'});
56
75
 
76
+ yargs.option('transform-option', {
77
+ type: 'string',
78
+ array: true,
79
+ alias: 'transformer-option',
80
+ coerce: (parseKeyValueParamArray: $FlowFixMe),
81
+ describe:
82
+ 'Custom transform options of the form key=value. URL-encoded. May be specified multiple times.',
83
+ });
84
+
85
+ yargs.option('resolver-option', {
86
+ type: 'string',
87
+ array: true,
88
+ coerce: (parseKeyValueParamArray: $FlowFixMe),
89
+ describe:
90
+ 'Custom resolver options of the form key=value. URL-encoded. May be specified multiple times.',
91
+ });
92
+
57
93
  // Deprecated
58
94
  yargs.option('reset-cache', {type: 'boolean'});
59
95
  },
60
96
 
61
- handler: makeAsyncCommand(async (argv: YargArguments) => {
97
+ handler: makeAsyncCommand(async (argv: Args) => {
62
98
  const config = await loadConfig(argv);
63
- // $FlowExpectedError YargArguments and RunBuildOptions are used interchangeable but their types are not yet compatible
64
- const options = (argv: RunBuildOptions);
99
+ const options: RunBuildOptions = {
100
+ entry: argv.entry,
101
+ dev: argv.dev,
102
+ out: argv.out,
103
+ minify: argv.minify,
104
+ platform: argv.platform,
105
+ sourceMap: argv.sourceMap,
106
+ sourceMapUrl: argv.sourceMapUrl,
107
+ customResolverOptions: argv.resolverOption,
108
+ customTransformOptions: argv.transformOption,
109
+ };
65
110
 
66
111
  // Inline require() to avoid circular dependency with ../index
67
112
  const MetroApi = require('../index');
@@ -78,9 +123,6 @@ module.exports = (): {
78
123
  entryFile: options.entry,
79
124
  minify: !!options.minify,
80
125
  platform: options.platform,
81
- // Bytecode bundles in Metro are not meant for production use. Instead,
82
- // the Hermes Bytecode Compiler should be invoked on the resulting JS bundle from Metro.
83
- runtimeBytecodeVersion: null,
84
126
  },
85
127
  });
86
128
  },
@@ -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
  * @oncall react_native
9
10
  */
@@ -12,10 +13,10 @@
12
13
 
13
14
  const { makeAsyncCommand } = require("../cli-utils");
14
15
  const Server = require("../Server");
15
- const denodeify = require("denodeify");
16
16
  const fs = require("fs");
17
17
  const { loadConfig } = require("metro-config");
18
18
  const path = require("path");
19
+ const { promisify } = require("util");
19
20
  async function dependencies(args, config) {
20
21
  const rootModuleAbsolutePath = args.entryFile;
21
22
  if (!fs.existsSync(rootModuleAbsolutePath)) {
@@ -23,6 +24,8 @@ async function dependencies(args, config) {
23
24
  new Error(`File ${rootModuleAbsolutePath} does not exist`)
24
25
  );
25
26
  }
27
+
28
+ // $FlowFixMe[cannot-write]
26
29
  config.cacheStores = [];
27
30
  const relativePath = path.relative(
28
31
  config.server.unstable_serverRoot ?? config.projectRoot,
@@ -35,10 +38,8 @@ async function dependencies(args, config) {
35
38
  minify: false,
36
39
  generateSourceMaps: !args.dev,
37
40
  };
38
- const writeToFile = args.output;
39
- const outStream = writeToFile
40
- ? fs.createWriteStream(args.output)
41
- : process.stdout;
41
+ const outStream =
42
+ args.output != null ? fs.createWriteStream(args.output) : process.stdout;
42
43
  const server = new Server(config);
43
44
  const deps = await server.getOrderedDependencyPaths(options);
44
45
  deps.forEach((modulePath) => {
@@ -54,13 +55,14 @@ async function dependencies(args, config) {
54
55
  }
55
56
  });
56
57
  server.end();
57
- return writeToFile
58
- ? denodeify(outStream.end).bind(outStream)()
58
+ return args.output != null
59
+ ? // $FlowFixMe[method-unbinding]
60
+ promisify(outStream.end).bind(outStream)()
59
61
  : Promise.resolve();
60
62
  }
61
63
  module.exports = () => ({
62
- command: "get-dependencies",
63
- desc: "List dependencies",
64
+ command: "get-dependencies [entryFile]",
65
+ desc: "List all dependencies that will be bundled for a given entry point",
64
66
  builder: (yargs) => {
65
67
  yargs.option("entry-file", {
66
68
  type: "string",
@@ -0,0 +1,128 @@
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
8
+ * @format
9
+ * @oncall react_native
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ import type {ConfigT} from 'metro-config';
15
+ import typeof Yargs from 'yargs';
16
+ import type {ModuleObject} from 'yargs';
17
+
18
+ const {makeAsyncCommand} = require('../cli-utils');
19
+ const Server = require('../Server');
20
+ const fs = require('fs');
21
+ const {loadConfig} = require('metro-config');
22
+ const path = require('path');
23
+ const {promisify} = require('util');
24
+
25
+ type Args = $ReadOnly<{
26
+ entryFile: string,
27
+ output?: string,
28
+ platform?: string,
29
+ transformer?: string,
30
+ maxWorkers?: number,
31
+ dev: boolean,
32
+ verbose: boolean,
33
+ }>;
34
+
35
+ async function dependencies(args: Args, config: ConfigT) {
36
+ const rootModuleAbsolutePath = args.entryFile;
37
+ if (!fs.existsSync(rootModuleAbsolutePath)) {
38
+ return Promise.reject(
39
+ new Error(`File ${rootModuleAbsolutePath} does not exist`),
40
+ );
41
+ }
42
+
43
+ // $FlowFixMe[cannot-write]
44
+ config.cacheStores = [];
45
+
46
+ const relativePath = path.relative(
47
+ config.server.unstable_serverRoot ?? config.projectRoot,
48
+ rootModuleAbsolutePath,
49
+ );
50
+
51
+ const options = {
52
+ platform: args.platform,
53
+ entryFile: relativePath,
54
+ dev: args.dev,
55
+ minify: false,
56
+ generateSourceMaps: !args.dev,
57
+ };
58
+
59
+ const outStream =
60
+ args.output != null ? fs.createWriteStream(args.output) : process.stdout;
61
+
62
+ const server = new Server(config);
63
+ const deps = await server.getOrderedDependencyPaths(options);
64
+ deps.forEach(modulePath => {
65
+ // Temporary hack to disable listing dependencies not under this directory.
66
+ // Long term, we need either
67
+ // (a) JS code to not depend on anything outside this directory, or
68
+ // (b) Come up with a way to declare this dependency in Buck.
69
+ const isInsideProjectRoots =
70
+ config.watchFolders.filter(root => modulePath.startsWith(root)).length >
71
+ 0;
72
+ if (isInsideProjectRoots) {
73
+ outStream.write(modulePath + '\n');
74
+ }
75
+ });
76
+
77
+ server.end();
78
+ return args.output != null
79
+ ? // $FlowFixMe[method-unbinding]
80
+ promisify(outStream.end).bind(outStream)()
81
+ : Promise.resolve();
82
+ }
83
+
84
+ module.exports = (): {...ModuleObject, handler: Function} => ({
85
+ command: 'get-dependencies [entryFile]',
86
+ desc: 'List all dependencies that will be bundled for a given entry point',
87
+ builder: (yargs: Yargs) => {
88
+ yargs.option('entry-file', {
89
+ type: 'string',
90
+ demandOption: true,
91
+ describe: 'Absolute path to the root JS file',
92
+ });
93
+ yargs.option('output', {
94
+ type: 'string',
95
+ describe:
96
+ 'File name where to store the output, ex. /tmp/dependencies.txt',
97
+ });
98
+ yargs.option('platform', {
99
+ type: 'string',
100
+ describe: 'The platform extension used for selecting modules',
101
+ });
102
+ yargs.option('transformer', {
103
+ type: 'string',
104
+ describe: 'Specify a custom transformer to be used',
105
+ });
106
+ yargs.option('max-workers', {
107
+ type: 'number',
108
+ describe:
109
+ 'Specifies the maximum number of workers the worker-pool ' +
110
+ 'will spawn for transforming files. This defaults to the number of the ' +
111
+ 'cores available on your machine.',
112
+ });
113
+ yargs.option('dev', {
114
+ type: 'boolean',
115
+ default: true,
116
+ describe: 'If false, skip all dev-only code path',
117
+ });
118
+ yargs.option('verbose', {
119
+ type: 'boolean',
120
+ default: false,
121
+ description: 'Enables logging',
122
+ });
123
+ },
124
+ handler: makeAsyncCommand(async (argv: Args) => {
125
+ const config = await loadConfig(argv);
126
+ await dependencies(argv, config);
127
+ }),
128
+ });
@@ -94,12 +94,19 @@ module.exports = () => ({
94
94
 
95
95
  // Inline require() to avoid circular dependency with ../index
96
96
  const MetroApi = require("../index");
97
-
98
- // $FlowExpectedError YargArguments and RunBuildOptions are used interchangeable but their types are not yet compatible
99
- server = await MetroApi.runServer(config, argv);
97
+ const {
98
+ config: _config,
99
+ hmrEnabled: _hmrEnabled,
100
+ maxWorkers: _maxWorkers,
101
+ port: _port,
102
+ projectRoots: _projectRoots,
103
+ resetCache: _resetCache,
104
+ ...runServerOptions
105
+ } = argv;
106
+ server = await MetroApi.runServer(config, runServerOptions);
100
107
  restarting = false;
101
108
  }
102
- const foundConfig = await resolveConfig(argv.config, argv.cwd);
109
+ const foundConfig = await resolveConfig(argv.config);
103
110
  if (foundConfig) {
104
111
  await watchFile(foundConfig.filepath, restart);
105
112
  } else {
@@ -11,8 +11,6 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- import type {RunServerOptions} from '../index';
15
- import type {YargArguments} from 'metro-config/src/configTypes.flow';
16
14
  import typeof Yargs from 'yargs';
17
15
  import type {ModuleObject} from 'yargs';
18
16
 
@@ -20,6 +18,20 @@ const {makeAsyncCommand, watchFile} = require('../cli-utils');
20
18
  const {loadConfig, resolveConfig} = require('metro-config');
21
19
  const {promisify} = require('util');
22
20
 
21
+ type Args = $ReadOnly<{
22
+ projectRoots?: $ReadOnlyArray<string>,
23
+ host: string,
24
+ port: number,
25
+ maxWorkers?: number,
26
+ secure?: boolean,
27
+ secureKey?: string,
28
+ secureCert?: string,
29
+ secureServerOptions?: string,
30
+ hmrEnabled?: boolean,
31
+ config?: string,
32
+ resetCache?: boolean,
33
+ }>;
34
+
23
35
  module.exports = (): {
24
36
  ...ModuleObject,
25
37
  handler: Function,
@@ -63,7 +75,7 @@ module.exports = (): {
63
75
  );
64
76
  },
65
77
 
66
- handler: makeAsyncCommand(async (argv: YargArguments) => {
78
+ handler: makeAsyncCommand(async (argv: Args) => {
67
79
  let server = null;
68
80
  let restarting = false;
69
81
 
@@ -86,13 +98,21 @@ module.exports = (): {
86
98
  // Inline require() to avoid circular dependency with ../index
87
99
  const MetroApi = require('../index');
88
100
 
89
- // $FlowExpectedError YargArguments and RunBuildOptions are used interchangeable but their types are not yet compatible
90
- server = await MetroApi.runServer(config, (argv: RunServerOptions));
101
+ const {
102
+ config: _config,
103
+ hmrEnabled: _hmrEnabled,
104
+ maxWorkers: _maxWorkers,
105
+ port: _port,
106
+ projectRoots: _projectRoots,
107
+ resetCache: _resetCache,
108
+ ...runServerOptions
109
+ } = argv;
110
+ server = await MetroApi.runServer(config, runServerOptions);
91
111
 
92
112
  restarting = false;
93
113
  }
94
114
 
95
- const foundConfig = await resolveConfig(argv.config, argv.cwd);
115
+ const foundConfig = await resolveConfig(argv.config);
96
116
 
97
117
  if (foundConfig) {
98
118
  await watchFile(foundConfig.filepath, restart);
package/src/index.d.ts ADDED
@@ -0,0 +1,161 @@
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
+ export * from './Asset';
12
+ export * from './DeltaBundler/types';
13
+ export * from './ModuleGraph/worker/collectDependencies';
14
+ export * from './Server';
15
+ export * from './lib/reporting';
16
+
17
+ import type {EventEmitter} from 'events';
18
+ import type {IncomingMessage, Server as HttpServer} from 'http';
19
+ import type {Server as HttpsServer} from 'https';
20
+ import type {
21
+ ConfigT,
22
+ InputConfigT,
23
+ MetroConfig,
24
+ Middleware,
25
+ } from 'metro-config';
26
+ import type {CustomTransformOptions} from 'metro-babel-transformer';
27
+ import type {ReadOnlyGraph} from './DeltaBundler/types';
28
+ import type {Duplex} from 'stream';
29
+ import Yargs = require('yargs');
30
+ import type {default as MetroServer, ServerOptions} from './Server';
31
+ import type {OutputOptions, RequestOptions} from './shared/types';
32
+
33
+ export {loadConfig, mergeConfig, resolveConfig} from 'metro-config';
34
+ export {Terminal} from 'metro-core';
35
+
36
+ export {HttpServer, HttpsServer};
37
+
38
+ interface MetroMiddleWare {
39
+ attachHmrServer: (httpServer: HttpServer | HttpsServer) => void;
40
+ end: () => void;
41
+ metroServer: MetroServer;
42
+ middleware: Middleware;
43
+ }
44
+
45
+ export interface RunMetroOptions extends ServerOptions {
46
+ waitForBundler?: boolean;
47
+ }
48
+
49
+ interface WebsocketServer extends EventEmitter {
50
+ handleUpgrade<T = WebsocketServer>(
51
+ request: IncomingMessage,
52
+ socket: Duplex,
53
+ upgradeHead: Buffer,
54
+ callback: (client: T, request: IncomingMessage) => void,
55
+ ): void;
56
+ }
57
+
58
+ export interface RunServerOptions {
59
+ hasReducedPerformance?: boolean;
60
+ host?: string;
61
+ onError?: (error: Error & {code?: string}) => void;
62
+ onReady?: (server: HttpServer | HttpsServer) => void;
63
+ runInspectorProxy?: boolean;
64
+ secureServerOptions?: Record<string, unknown>;
65
+
66
+ /** @deprecated since version 0.61 */
67
+ secure?: boolean;
68
+
69
+ /** @deprecated since version 0.61 */
70
+ secureCert?: string;
71
+
72
+ /** @deprecated since version 0.61 */
73
+ secureKey?: string;
74
+
75
+ waitForBundler?: boolean;
76
+ watch?: boolean;
77
+ websocketEndpoints?: {
78
+ [path: string]: WebsocketServer;
79
+ };
80
+ }
81
+
82
+ export interface RunBuildOptions {
83
+ entry: string;
84
+ dev?: boolean;
85
+ out?: string;
86
+ onBegin?: () => void;
87
+ onComplete?: () => void;
88
+ onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
89
+ minify?: boolean;
90
+ output?: {
91
+ build: (
92
+ server: MetroServer,
93
+ options: RequestOptions,
94
+ ) => Promise<{
95
+ code: string;
96
+ map: string;
97
+ }>;
98
+ save: (
99
+ entry: {
100
+ code: string;
101
+ map: string;
102
+ },
103
+ options: OutputOptions,
104
+ postSave: (...args: string[]) => void,
105
+ ) => Promise<unknown>;
106
+ };
107
+ platform?: string;
108
+ sourceMap?: boolean;
109
+ sourceMapUrl?: string;
110
+ }
111
+
112
+ interface BuildGraphOptions {
113
+ entries: ReadonlyArray<string>;
114
+ customTransformOptions?: CustomTransformOptions;
115
+ dev?: boolean;
116
+ minify?: boolean;
117
+ onProgress?: (transformedFileCount: number, totalFileCount: number) => void;
118
+ platform?: string;
119
+ type?: 'module' | 'script';
120
+ }
121
+
122
+ export {MetroConfig};
123
+
124
+ export function runMetro(
125
+ config: InputConfigT,
126
+ options?: RunMetroOptions,
127
+ ): Promise<MetroServer>;
128
+
129
+ export function createConnectMiddleWare(
130
+ config: ConfigT,
131
+ options?: RunMetroOptions,
132
+ ): Promise<MetroMiddleWare>;
133
+
134
+ export function runServer(
135
+ config: ConfigT,
136
+ options: RunServerOptions,
137
+ ): Promise<HttpServer | HttpsServer>;
138
+
139
+ export function runBuild(
140
+ config: ConfigT,
141
+ options: RunBuildOptions,
142
+ ): Promise<void>;
143
+
144
+ export function buildGraph(
145
+ config: ConfigT,
146
+ options: BuildGraphOptions,
147
+ ): Promise<ReadOnlyGraph<void>>;
148
+
149
+ type BuildCommandOptions = Record<string, unknown> | null;
150
+ type ServeCommandOptions = Record<string, unknown> | null;
151
+
152
+ interface AttachMetroCLIOptions {
153
+ build?: BuildCommandOptions;
154
+ serve?: ServeCommandOptions;
155
+ dependencies?: unknown;
156
+ }
157
+
158
+ export function attachMetroCli(
159
+ yargs: typeof Yargs,
160
+ options?: AttachMetroCLIOptions,
161
+ ): typeof Yargs;