metro 0.66.2 → 0.69.0

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 (255) hide show
  1. package/package.json +22 -24
  2. package/src/Assets.js +9 -9
  3. package/src/Assets.js.flow +5 -7
  4. package/src/Bundler/util.js +16 -15
  5. package/src/Bundler/util.js.flow +11 -10
  6. package/src/Bundler.js +21 -8
  7. package/src/Bundler.js.flow +19 -7
  8. package/src/DeltaBundler/DeltaCalculator.js +15 -15
  9. package/src/DeltaBundler/DeltaCalculator.js.flow +7 -7
  10. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js +13 -13
  11. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js.flow +6 -7
  12. package/src/DeltaBundler/Serializers/baseJSBundle.js +4 -4
  13. package/src/DeltaBundler/Serializers/baseJSBundle.js.flow +4 -4
  14. package/src/DeltaBundler/Serializers/getAllFiles.js +1 -1
  15. package/src/DeltaBundler/Serializers/getAllFiles.js.flow +3 -3
  16. package/src/DeltaBundler/Serializers/getAssets.js +3 -3
  17. package/src/DeltaBundler/Serializers/getAssets.js.flow +4 -5
  18. package/src/DeltaBundler/Serializers/getExplodedSourceMap.js +4 -4
  19. package/src/DeltaBundler/Serializers/getExplodedSourceMap.js.flow +4 -4
  20. package/src/DeltaBundler/Serializers/getRamBundleInfo.js +20 -20
  21. package/src/DeltaBundler/Serializers/getRamBundleInfo.js.flow +9 -10
  22. package/src/DeltaBundler/Serializers/helpers/bytecode.js +11 -11
  23. package/src/DeltaBundler/Serializers/helpers/bytecode.js.flow +5 -6
  24. package/src/DeltaBundler/Serializers/helpers/getInlineSourceMappingURL.js +1 -1
  25. package/src/DeltaBundler/Serializers/helpers/getInlineSourceMappingURL.js.flow +1 -1
  26. package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.js +2 -2
  27. package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.js.flow +4 -4
  28. package/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js +1 -1
  29. package/src/DeltaBundler/Serializers/helpers/getTransitiveDependencies.js.flow +1 -1
  30. package/src/DeltaBundler/Serializers/helpers/js.js +6 -6
  31. package/src/DeltaBundler/Serializers/helpers/js.js.flow +11 -12
  32. package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js +4 -4
  33. package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js.flow +3 -3
  34. package/src/DeltaBundler/Serializers/helpers/processModules.js +4 -4
  35. package/src/DeltaBundler/Serializers/helpers/processModules.js.flow +3 -3
  36. package/src/DeltaBundler/Serializers/hmrJSBundle.js +9 -9
  37. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +9 -8
  38. package/src/DeltaBundler/Serializers/sourceMapGenerator.js +6 -6
  39. package/src/DeltaBundler/Serializers/sourceMapGenerator.js.flow +3 -4
  40. package/src/DeltaBundler/Serializers/sourceMapObject.js +5 -5
  41. package/src/DeltaBundler/Serializers/sourceMapObject.js.flow +4 -4
  42. package/src/DeltaBundler/Serializers/sourceMapString.js +2 -2
  43. package/src/DeltaBundler/Serializers/sourceMapString.js.flow +3 -3
  44. package/src/DeltaBundler/Transformer.js +9 -9
  45. package/src/DeltaBundler/Transformer.js.flow +7 -9
  46. package/src/DeltaBundler/Worker.js +8 -11
  47. package/src/DeltaBundler/Worker.js.flow +8 -11
  48. package/src/DeltaBundler/WorkerFarm.js +14 -17
  49. package/src/DeltaBundler/WorkerFarm.js.flow +7 -10
  50. package/src/DeltaBundler/__fixtures__/hasteImpl.js +2 -2
  51. package/src/DeltaBundler/computeDelta.js +2 -2
  52. package/src/DeltaBundler/computeDelta.js.flow +2 -2
  53. package/src/DeltaBundler/getTransformCacheKey.js +4 -4
  54. package/src/DeltaBundler/getTransformCacheKey.js.flow +5 -6
  55. package/src/DeltaBundler/mergeDeltas.js +2 -2
  56. package/src/DeltaBundler/mergeDeltas.js.flow +1 -1
  57. package/src/DeltaBundler/traverseDependencies.js +22 -22
  58. package/src/DeltaBundler/traverseDependencies.js.flow +3 -3
  59. package/src/DeltaBundler/types.flow.js +1 -1
  60. package/src/DeltaBundler/types.flow.js.flow +1 -1
  61. package/src/DeltaBundler.js +5 -5
  62. package/src/DeltaBundler.js.flow +4 -4
  63. package/src/HmrServer.js +71 -55
  64. package/src/HmrServer.js.flow +40 -37
  65. package/src/IncrementalBundler/GraphNotFoundError.js +1 -1
  66. package/src/IncrementalBundler/GraphNotFoundError.js.flow +1 -1
  67. package/src/IncrementalBundler/ResourceNotFoundError.js +1 -1
  68. package/src/IncrementalBundler/ResourceNotFoundError.js.flow +1 -1
  69. package/src/IncrementalBundler/RevisionNotFoundError.js +1 -1
  70. package/src/IncrementalBundler/RevisionNotFoundError.js.flow +1 -1
  71. package/src/IncrementalBundler.js +44 -32
  72. package/src/IncrementalBundler.js.flow +30 -24
  73. package/src/ModuleGraph/module.js +3 -3
  74. package/src/ModuleGraph/module.js.flow +1 -1
  75. package/src/ModuleGraph/node-haste/HasteFS.js +2 -2
  76. package/src/ModuleGraph/node-haste/HasteFS.js.flow +1 -1
  77. package/src/ModuleGraph/node-haste/Module.js +1 -1
  78. package/src/ModuleGraph/node-haste/Module.js.flow +1 -1
  79. package/src/ModuleGraph/node-haste/ModuleCache.js +1 -1
  80. package/src/ModuleGraph/node-haste/ModuleCache.js.flow +3 -3
  81. package/src/ModuleGraph/node-haste/Package.js +3 -3
  82. package/src/ModuleGraph/node-haste/Package.js.flow +3 -3
  83. package/src/ModuleGraph/node-haste/node-haste.flow.js +1 -1
  84. package/src/ModuleGraph/node-haste/node-haste.flow.js.flow +1 -1
  85. package/src/ModuleGraph/node-haste/node-haste.js +42 -32
  86. package/src/ModuleGraph/node-haste/node-haste.js.flow +28 -23
  87. package/src/ModuleGraph/output/indexed-ram-bundle.js +17 -17
  88. package/src/ModuleGraph/output/indexed-ram-bundle.js.flow +41 -9
  89. package/src/ModuleGraph/output/multiple-files-ram-bundle.js +18 -18
  90. package/src/ModuleGraph/output/multiple-files-ram-bundle.js.flow +21 -9
  91. package/src/ModuleGraph/output/plain-bundle.js +6 -6
  92. package/src/ModuleGraph/output/plain-bundle.js.flow +7 -7
  93. package/src/ModuleGraph/output/reverse-dependency-map-references.js +3 -3
  94. package/src/ModuleGraph/output/reverse-dependency-map-references.js.flow +4 -8
  95. package/src/ModuleGraph/output/util.js +31 -30
  96. package/src/ModuleGraph/output/util.js.flow +15 -16
  97. package/src/ModuleGraph/silent-console.js +2 -2
  98. package/src/ModuleGraph/silent-console.js.flow +1 -1
  99. package/src/ModuleGraph/test-helpers.js +7 -7
  100. package/src/ModuleGraph/types.flow.js +1 -1
  101. package/src/ModuleGraph/types.flow.js.flow +7 -7
  102. package/src/ModuleGraph/worker/JsFileWrapping.js +7 -7
  103. package/src/ModuleGraph/worker/JsFileWrapping.js.flow +3 -2
  104. package/src/ModuleGraph/worker/Platforms.js +2 -2
  105. package/src/ModuleGraph/worker/Platforms.js.flow +1 -1
  106. package/src/ModuleGraph/worker/collectDependencies.js +24 -25
  107. package/src/ModuleGraph/worker/collectDependencies.js.flow +15 -17
  108. package/src/ModuleGraph/worker/generate.js +2 -2
  109. package/src/ModuleGraph/worker/generate.js.flow +3 -3
  110. package/src/ModuleGraph/worker/generateImportNames.js +5 -5
  111. package/src/ModuleGraph/worker/generateImportNames.js.flow +3 -5
  112. package/src/ModuleGraph/worker/mergeSourceMaps.js +8 -7
  113. package/src/ModuleGraph/worker/mergeSourceMaps.js.flow +3 -3
  114. package/src/Server/MultipartResponse.js +4 -8
  115. package/src/Server/symbolicate.js +8 -8
  116. package/src/Server/symbolicate.js.flow +17 -5
  117. package/src/Server.js +202 -150
  118. package/src/Server.js.flow +129 -87
  119. package/src/cli-utils.js +4 -4
  120. package/src/cli-utils.js.flow +15 -13
  121. package/src/cli.js +4 -4
  122. package/src/cli.js.flow +2 -4
  123. package/src/commands/build.js +23 -24
  124. package/src/commands/build.js.flow +6 -9
  125. package/src/commands/dependencies.js +25 -19
  126. package/src/commands/serve.js +17 -18
  127. package/src/commands/serve.js.flow +6 -9
  128. package/src/index.js +116 -51
  129. package/src/index.js.flow +107 -41
  130. package/src/integration_tests/basic_bundle/AssetRegistry.js +2 -2
  131. package/src/integration_tests/basic_bundle/AssetRegistry.js.flow +1 -1
  132. package/src/integration_tests/basic_bundle/Bar.js +2 -2
  133. package/src/integration_tests/basic_bundle/Bar.js.flow +1 -1
  134. package/src/integration_tests/basic_bundle/ErrorBundle.js +2 -2
  135. package/src/integration_tests/basic_bundle/ErrorBundle.js.flow +1 -1
  136. package/src/integration_tests/basic_bundle/Foo.js +2 -2
  137. package/src/integration_tests/basic_bundle/Foo.js.flow +1 -1
  138. package/src/integration_tests/basic_bundle/TestBundle.js +3 -2
  139. package/src/integration_tests/basic_bundle/TestBundle.js.flow +3 -1
  140. package/src/integration_tests/basic_bundle/TestPolyfill.js +1 -1
  141. package/src/integration_tests/basic_bundle/TestPolyfill.js.flow +1 -1
  142. package/src/integration_tests/basic_bundle/TypeScript.ts +1 -1
  143. package/src/integration_tests/basic_bundle/import-export/export-1.js +2 -2
  144. package/src/integration_tests/basic_bundle/import-export/export-1.js.flow +1 -1
  145. package/src/integration_tests/basic_bundle/import-export/export-2.js +2 -2
  146. package/src/integration_tests/basic_bundle/import-export/export-2.js.flow +1 -1
  147. package/src/integration_tests/basic_bundle/import-export/export-3.js +2 -2
  148. package/src/integration_tests/basic_bundle/import-export/export-3.js.flow +1 -1
  149. package/src/integration_tests/basic_bundle/import-export/export-4.js +2 -2
  150. package/src/integration_tests/basic_bundle/import-export/export-4.js.flow +1 -1
  151. package/src/integration_tests/basic_bundle/import-export/export-5.js +2 -2
  152. package/src/integration_tests/basic_bundle/import-export/export-5.js.flow +1 -1
  153. package/src/integration_tests/basic_bundle/import-export/export-6.js +2 -2
  154. package/src/integration_tests/basic_bundle/import-export/export-6.js.flow +1 -1
  155. package/src/integration_tests/basic_bundle/import-export/export-null.js +2 -2
  156. package/src/integration_tests/basic_bundle/import-export/export-null.js.flow +1 -1
  157. package/src/integration_tests/basic_bundle/import-export/export-primitive-default.js +2 -2
  158. package/src/integration_tests/basic_bundle/import-export/export-primitive-default.js.flow +1 -1
  159. package/src/integration_tests/basic_bundle/import-export/index.js +8 -8
  160. package/src/integration_tests/basic_bundle/import-export/index.js.flow +1 -1
  161. package/src/integration_tests/basic_bundle/polyfill.js +1 -1
  162. package/src/integration_tests/execBundle.js +2 -2
  163. package/src/integration_tests/execBundle.js.flow +1 -1
  164. package/src/integration_tests/metro.config.js +8 -8
  165. package/src/lib/BatchProcessor.js +3 -3
  166. package/src/lib/BatchProcessor.js.flow +1 -1
  167. package/src/lib/JsonReporter.js +2 -2
  168. package/src/lib/JsonReporter.js.flow +1 -1
  169. package/src/lib/RamBundleParser.js +1 -1
  170. package/src/lib/RamBundleParser.js.flow +1 -1
  171. package/src/lib/TerminalReporter.js +14 -14
  172. package/src/lib/TerminalReporter.js.flow +9 -10
  173. package/src/lib/bundleToBytecode.js +6 -7
  174. package/src/lib/bundleToBytecode.js.flow +8 -7
  175. package/src/lib/bundleToString.js +3 -3
  176. package/src/lib/bundleToString.js.flow +5 -4
  177. package/src/lib/countLines.js +3 -7
  178. package/src/lib/countLines.js.flow +4 -7
  179. package/src/lib/createModuleIdFactory.js +2 -2
  180. package/src/lib/createModuleIdFactory.js.flow +1 -1
  181. package/src/lib/{attachWebsocketServer.js → createWebsocketServer.js} +18 -17
  182. package/src/lib/{attachWebsocketServer.js.flow → createWebsocketServer.js.flow} +12 -21
  183. package/src/lib/debounceAsyncQueue.js +2 -2
  184. package/src/lib/debounceAsyncQueue.js.flow +1 -1
  185. package/src/lib/formatBundlingError.js +21 -21
  186. package/src/lib/formatBundlingError.js.flow +6 -8
  187. package/src/lib/getAppendScripts.js +26 -27
  188. package/src/lib/getAppendScripts.js.flow +7 -8
  189. package/src/lib/getGraphId.js +3 -3
  190. package/src/lib/getGraphId.js.flow +3 -3
  191. package/src/lib/getMaxWorkers.js +2 -2
  192. package/src/lib/getMaxWorkers.js.flow +1 -1
  193. package/src/lib/getPreludeCode.js +2 -2
  194. package/src/lib/getPreludeCode.js.flow +1 -1
  195. package/src/lib/getPrependedScripts.js +15 -15
  196. package/src/lib/getPrependedScripts.js.flow +7 -8
  197. package/src/lib/logToConsole.js +1 -1
  198. package/src/lib/logToConsole.js.flow +3 -3
  199. package/src/lib/parseCustomTransformOptions.js +2 -2
  200. package/src/lib/parseCustomTransformOptions.js.flow +3 -3
  201. package/src/lib/parseOptionsFromUrl.js +7 -7
  202. package/src/lib/parseOptionsFromUrl.js.flow +29 -9
  203. package/src/lib/relativizeSourceMap.js +1 -1
  204. package/src/lib/relativizeSourceMap.js.flow +3 -3
  205. package/src/lib/reporting.js +5 -5
  206. package/src/lib/reporting.js.flow +16 -3
  207. package/src/lib/splitBundleOptions.js +5 -5
  208. package/src/lib/splitBundleOptions.js.flow +1 -1
  209. package/src/lib/transformHelpers.js +10 -10
  210. package/src/lib/transformHelpers.js.flow +5 -5
  211. package/src/node-haste/DependencyGraph/ModuleResolution.js +51 -30
  212. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +46 -42
  213. package/src/node-haste/DependencyGraph/createHasteMap.js +99 -0
  214. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +88 -0
  215. package/src/node-haste/DependencyGraph/{assets/empty-module.js → types.js} +2 -2
  216. package/src/node-haste/DependencyGraph/types.js.flow +88 -0
  217. package/src/node-haste/DependencyGraph.js +49 -117
  218. package/src/node-haste/DependencyGraph.js.flow +36 -111
  219. package/src/node-haste/Module.js +1 -1
  220. package/src/node-haste/Module.js.flow +3 -3
  221. package/src/node-haste/ModuleCache.js +54 -13
  222. package/src/node-haste/ModuleCache.js.flow +53 -12
  223. package/src/node-haste/Package.js +7 -9
  224. package/src/node-haste/Package.js.flow +1 -8
  225. package/src/node-haste/lib/AssetPaths.js +5 -5
  226. package/src/node-haste/lib/AssetPaths.js.flow +2 -4
  227. package/src/node-haste/lib/parsePlatformFilePath.js +4 -4
  228. package/src/node-haste/lib/parsePlatformFilePath.js.flow +1 -1
  229. package/src/shared/output/RamBundle/as-assets.js +16 -17
  230. package/src/shared/output/RamBundle/as-assets.js.flow +11 -15
  231. package/src/shared/output/RamBundle/as-indexed-file.js +14 -15
  232. package/src/shared/output/RamBundle/as-indexed-file.js.flow +8 -12
  233. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.js +7 -7
  234. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.js.flow +4 -4
  235. package/src/shared/output/RamBundle/magic-number.js +1 -1
  236. package/src/shared/output/RamBundle/magic-number.js.flow +1 -1
  237. package/src/shared/output/RamBundle/util.js +21 -19
  238. package/src/shared/output/RamBundle/util.js.flow +5 -8
  239. package/src/shared/output/RamBundle/write-sourcemap.js +1 -1
  240. package/src/shared/output/RamBundle/write-sourcemap.js.flow +1 -1
  241. package/src/shared/output/RamBundle.js +2 -2
  242. package/src/shared/output/RamBundle.js.flow +4 -5
  243. package/src/shared/output/bundle.js +6 -6
  244. package/src/shared/output/bundle.js.flow +4 -5
  245. package/src/shared/output/meta.js +4 -5
  246. package/src/shared/output/meta.js.flow +2 -4
  247. package/src/shared/output/unbundle.js +1 -1
  248. package/src/shared/output/unbundle.js.flow +1 -1
  249. package/src/shared/output/writeFile.js +1 -1
  250. package/src/shared/output/writeFile.js.flow +1 -1
  251. package/src/shared/types.flow.js +1 -1
  252. package/src/shared/types.flow.js.flow +2 -2
  253. package/src/node-haste/DependencyGraph/assets/empty-module.js.flow +0 -9
  254. package/src/node-haste/types.js +0 -10
  255. package/src/node-haste/types.js.flow +0 -23
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
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.
@@ -10,22 +10,20 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const MetroApi = require('../index');
14
- const TerminalReporter = require('../lib/TerminalReporter');
13
+ import type {RunBuildOptions} from '../index';
14
+ import type {YargArguments} from 'metro-config/src/configTypes.flow';
15
+ import typeof Yargs from 'yargs';
15
16
 
16
17
  const {makeAsyncCommand} = require('../cli-utils');
18
+ const MetroApi = require('../index');
19
+ const TerminalReporter = require('../lib/TerminalReporter');
17
20
  const {loadConfig} = require('metro-config');
18
21
  const {Terminal} = require('metro-core');
19
22
 
20
- import type {RunBuildOptions} from '../index';
21
- import type {YargArguments} from 'metro-config/src/configTypes.flow';
22
- import typeof Yargs from 'yargs';
23
-
24
23
  const term = new Terminal(process.stdout);
25
24
  const updateReporter = new TerminalReporter(term);
26
25
 
27
26
  module.exports = (): ({|
28
- // $FlowFixMe[value-as-type]
29
27
  builder: (yargs: Yargs) => void,
30
28
  command: string,
31
29
  description: string,
@@ -36,7 +34,6 @@ module.exports = (): ({|
36
34
  description:
37
35
  'Generates a JavaScript bundle containing the specified entrypoint and its descendants',
38
36
 
39
- // $FlowFixMe[value-as-type]
40
37
  builder: (yargs: Yargs): void => {
41
38
  yargs.option('project-roots', {
42
39
  alias: 'P',
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
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.
@@ -8,19 +8,21 @@
8
8
  */
9
9
  "use strict";
10
10
 
11
+ const { makeAsyncCommand } = require("../cli-utils");
12
+
11
13
  const Server = require("../Server");
12
14
 
13
15
  const denodeify = require("denodeify");
14
16
 
15
17
  const fs = require("fs");
16
18
 
17
- const path = require("path");
18
-
19
- const { makeAsyncCommand } = require("../cli-utils");
20
-
21
19
  const { loadConfig } = require("metro-config");
22
20
 
21
+ const path = require("path");
22
+
23
23
  async function dependencies(args, config) {
24
+ var _config$server$unstab;
25
+
24
26
  const rootModuleAbsolutePath = args.entryFile;
25
27
 
26
28
  if (!fs.existsSync(rootModuleAbsolutePath)) {
@@ -31,7 +33,10 @@ async function dependencies(args, config) {
31
33
 
32
34
  config.cacheStores = [];
33
35
  const relativePath = path.relative(
34
- config.projectRoot,
36
+ (_config$server$unstab = config.server.unstable_serverRoot) !== null &&
37
+ _config$server$unstab !== void 0
38
+ ? _config$server$unstab
39
+ : config.projectRoot,
35
40
  rootModuleAbsolutePath
36
41
  );
37
42
  const options = {
@@ -39,7 +44,7 @@ async function dependencies(args, config) {
39
44
  entryFile: relativePath,
40
45
  dev: args.dev,
41
46
  minify: false,
42
- generateSourceMaps: !args.dev
47
+ generateSourceMaps: !args.dev,
43
48
  };
44
49
  const writeToFile = args.output;
45
50
  const outStream = writeToFile
@@ -47,13 +52,13 @@ async function dependencies(args, config) {
47
52
  : process.stdout;
48
53
  const server = new Server(config);
49
54
  const deps = await server.getOrderedDependencyPaths(options);
50
- deps.forEach(modulePath => {
55
+ deps.forEach((modulePath) => {
51
56
  // Temporary hack to disable listing dependencies not under this directory.
52
57
  // Long term, we need either
53
58
  // (a) JS code to not depend on anything outside this directory, or
54
59
  // (b) Come up with a way to declare this dependency in Buck.
55
60
  const isInsideProjectRoots =
56
- config.watchFolders.filter(root => modulePath.startsWith(root)).length >
61
+ config.watchFolders.filter((root) => modulePath.startsWith(root)).length >
57
62
  0;
58
63
 
59
64
  if (isInsideProjectRoots) {
@@ -69,44 +74,45 @@ async function dependencies(args, config) {
69
74
  module.exports = () => ({
70
75
  command: "get-dependencies",
71
76
  description: "List dependencies",
72
- builder: yargs => {
77
+ builder: (yargs) => {
73
78
  yargs.option("entry-file", {
74
79
  type: "string",
75
80
  demandOption: true,
76
- describe: "Absolute path to the root JS file"
81
+ describe: "Absolute path to the root JS file",
77
82
  });
78
83
  yargs.option("output", {
79
84
  type: "string",
80
- describe: "File name where to store the output, ex. /tmp/dependencies.txt"
85
+ describe:
86
+ "File name where to store the output, ex. /tmp/dependencies.txt",
81
87
  });
82
88
  yargs.option("platform", {
83
89
  type: "string",
84
- describe: "The platform extension used for selecting modules"
90
+ describe: "The platform extension used for selecting modules",
85
91
  });
86
92
  yargs.option("transformer", {
87
93
  type: "string",
88
- describe: "Specify a custom transformer to be used"
94
+ describe: "Specify a custom transformer to be used",
89
95
  });
90
96
  yargs.option("max-workers", {
91
97
  type: "number",
92
98
  describe:
93
99
  "Specifies the maximum number of workers the worker-pool " +
94
100
  "will spawn for transforming files. This defaults to the number of the " +
95
- "cores available on your machine."
101
+ "cores available on your machine.",
96
102
  });
97
103
  yargs.option("dev", {
98
104
  type: "boolean",
99
105
  default: true,
100
- describe: "If false, skip all dev-only code path"
106
+ describe: "If false, skip all dev-only code path",
101
107
  });
102
108
  yargs.option("verbose", {
103
109
  type: "boolean",
104
110
  default: false,
105
- description: "Enables logging"
111
+ description: "Enables logging",
106
112
  });
107
113
  },
108
- handler: makeAsyncCommand(async argv => {
114
+ handler: makeAsyncCommand(async (argv) => {
109
115
  const config = await loadConfig(argv);
110
116
  await dependencies(argv, config);
111
- })
117
+ }),
112
118
  });
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
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.
@@ -9,9 +9,9 @@
9
9
  */
10
10
  "use strict";
11
11
 
12
- const MetroApi = require("../index");
12
+ const { makeAsyncCommand, watchFile } = require("../cli-utils");
13
13
 
14
- const { watchFile, makeAsyncCommand } = require("../cli-utils");
14
+ const MetroApi = require("../index");
15
15
 
16
16
  const { loadConfig, resolveConfig } = require("metro-config");
17
17
 
@@ -20,55 +20,54 @@ const { promisify } = require("util");
20
20
  module.exports = () => ({
21
21
  command: "serve",
22
22
  description: "Starts Metro on the given port, building bundles on the fly",
23
- // $FlowFixMe[value-as-type]
24
- builder: yargs => {
23
+ builder: (yargs) => {
25
24
  yargs.option("project-roots", {
26
25
  alias: "P",
27
26
  type: "string",
28
- array: true
27
+ array: true,
29
28
  });
30
29
  yargs.option("host", {
31
30
  alias: "h",
32
31
  type: "string",
33
- default: "localhost"
32
+ default: "localhost",
34
33
  });
35
34
  yargs.option("port", {
36
35
  alias: "p",
37
36
  type: "number",
38
- default: 8080
37
+ default: 8080,
39
38
  });
40
39
  yargs.option("max-workers", {
41
40
  alias: "j",
42
- type: "number"
41
+ type: "number",
43
42
  });
44
43
  yargs.option("secure", {
45
44
  type: "boolean",
46
- describe: "(deprecated)"
45
+ describe: "(deprecated)",
47
46
  });
48
47
  yargs.option("secure-key", {
49
48
  type: "string",
50
- describe: "(deprecated)"
49
+ describe: "(deprecated)",
51
50
  });
52
51
  yargs.option("secure-cert", {
53
52
  type: "string",
54
- describe: "(deprecated)"
53
+ describe: "(deprecated)",
55
54
  });
56
55
  yargs.option("secure-server-options", {
57
56
  alias: "s",
58
57
  type: "string",
59
- describe: "Use dot notation for object path"
58
+ describe: "Use dot notation for object path",
60
59
  });
61
60
  yargs.option("hmr-enabled", {
62
61
  alias: "hmr",
63
- type: "boolean"
62
+ type: "boolean",
64
63
  });
65
64
  yargs.option("config", {
66
65
  alias: "c",
67
- type: "string"
66
+ type: "string",
68
67
  }); // Deprecated
69
68
 
70
69
  yargs.option("reset-cache", {
71
- type: "boolean"
70
+ type: "boolean",
72
71
  }); // Examples
73
72
 
74
73
  yargs.example(
@@ -76,7 +75,7 @@ module.exports = () => ({
76
75
  '-s.cert="$(cat path/to/cert)" -s.key="$(cat path/to/key")'
77
76
  );
78
77
  },
79
- handler: makeAsyncCommand(async argv => {
78
+ handler: makeAsyncCommand(async (argv) => {
80
79
  let server = null;
81
80
  let restarting = false;
82
81
 
@@ -107,5 +106,5 @@ module.exports = () => ({
107
106
  } else {
108
107
  await restart();
109
108
  }
110
- })
109
+ }),
111
110
  });
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
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.
@@ -10,18 +10,16 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const MetroApi = require('../index');
14
-
15
- const {watchFile, makeAsyncCommand} = require('../cli-utils');
16
- const {loadConfig, resolveConfig} = require('metro-config');
17
- const {promisify} = require('util');
18
-
19
13
  import type {RunServerOptions} from '../index';
20
14
  import type {YargArguments} from 'metro-config/src/configTypes.flow';
21
15
  import typeof Yargs from 'yargs';
22
16
 
17
+ const {makeAsyncCommand, watchFile} = require('../cli-utils');
18
+ const MetroApi = require('../index');
19
+ const {loadConfig, resolveConfig} = require('metro-config');
20
+ const {promisify} = require('util');
21
+
23
22
  module.exports = (): ({|
24
- // $FlowFixMe[value-as-type]
25
23
  builder: (yargs: Yargs) => void,
26
24
  command: $TEMPORARY$string<'serve'>,
27
25
  description: string,
@@ -31,7 +29,6 @@ module.exports = (): ({|
31
29
 
32
30
  description: 'Starts Metro on the given port, building bundles on the fly',
33
31
 
34
- // $FlowFixMe[value-as-type]
35
32
  builder: (yargs: Yargs): void => {
36
33
  yargs.option('project-roots', {
37
34
  alias: 'P',
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
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.
@@ -9,13 +9,21 @@
9
9
  */
10
10
  "use strict";
11
11
 
12
- const IncrementalBundler = require("./IncrementalBundler");
12
+ const makeBuildCommand = require("./commands/build");
13
+
14
+ const makeDependenciesCommand = require("./commands/dependencies");
15
+
16
+ const makeServeCommand = require("./commands/serve");
13
17
 
14
18
  const MetroHmrServer = require("./HmrServer");
15
19
 
20
+ const IncrementalBundler = require("./IncrementalBundler");
21
+
22
+ const createWebsocketServer = require("./lib/createWebsocketServer");
23
+
16
24
  const MetroServer = require("./Server");
17
25
 
18
- const attachWebsocketServer = require("./lib/attachWebsocketServer");
26
+ const outputBundle = require("./shared/output/bundle");
19
27
 
20
28
  const chalk = require("chalk");
21
29
 
@@ -25,17 +33,13 @@ const http = require("http");
25
33
 
26
34
  const https = require("https");
27
35
 
28
- const makeBuildCommand = require("./commands/build");
36
+ const { getDefaultConfig, loadConfig, mergeConfig } = require("metro-config");
29
37
 
30
- const makeDependenciesCommand = require("./commands/dependencies");
31
-
32
- const makeServeCommand = require("./commands/serve");
33
-
34
- const outputBundle = require("./shared/output/bundle");
38
+ const { InspectorProxy } = require("metro-inspector-proxy");
35
39
 
36
- const { loadConfig, mergeConfig, getDefaultConfig } = require("metro-config");
40
+ const { parse } = require("url");
37
41
 
38
- const { InspectorProxy } = require("metro-inspector-proxy");
42
+ const ws = require("ws");
39
43
 
40
44
  async function getConfig(config) {
41
45
  const defaultConfig = await getDefaultConfig(config.projectRoot);
@@ -44,20 +48,47 @@ async function getConfig(config) {
44
48
 
45
49
  async function runMetro(config, options) {
46
50
  const mergedConfig = await getConfig(config);
47
- mergedConfig.reporter.update({
51
+ const {
52
+ reporter,
53
+ server: { port },
54
+ } = mergedConfig;
55
+ reporter.update({
48
56
  hasReducedPerformance: options
49
57
  ? Boolean(options.hasReducedPerformance)
50
58
  : false,
51
- port: mergedConfig.server.port,
52
- type: "initialize_started"
59
+ port,
60
+ type: "initialize_started",
53
61
  });
54
- return new MetroServer(mergedConfig, options);
62
+ const { waitForBundler = false, ...serverOptions } =
63
+ options !== null && options !== void 0 ? options : {};
64
+ const server = new MetroServer(mergedConfig, serverOptions);
65
+ const readyPromise = server
66
+ .ready()
67
+ .then(() => {
68
+ reporter.update({
69
+ type: "initialize_done",
70
+ port,
71
+ });
72
+ })
73
+ .catch((error) => {
74
+ reporter.update({
75
+ type: "initialize_failed",
76
+ port,
77
+ error,
78
+ });
79
+ });
80
+
81
+ if (waitForBundler) {
82
+ await readyPromise;
83
+ }
84
+
85
+ return server;
55
86
  }
56
87
 
57
88
  exports.runMetro = runMetro;
58
89
  exports.loadConfig = loadConfig;
59
90
 
60
- exports.createConnectMiddleware = async function(config, options) {
91
+ const createConnectMiddleware = async function (config, options) {
61
92
  const metroServer = await runMetro(config, options);
62
93
  let enhancedMiddleware = metroServer.processRequest; // Enhance the resulting middleware using the config options
63
94
 
@@ -70,15 +101,23 @@ exports.createConnectMiddleware = async function(config, options) {
70
101
 
71
102
  return {
72
103
  attachHmrServer(httpServer) {
73
- attachWebsocketServer({
74
- httpServer,
75
- path: "/hot",
76
- // $FlowFixMe[method-unbinding]
104
+ const wss = createWebsocketServer({
77
105
  websocketServer: new MetroHmrServer(
78
106
  metroServer.getBundler(),
79
107
  metroServer.getCreateModuleId(),
80
108
  config
81
- )
109
+ ),
110
+ });
111
+ httpServer.on("upgrade", (request, socket, head) => {
112
+ const { pathname } = parse(request.url);
113
+
114
+ if (pathname === "/hot") {
115
+ wss.handleUpgrade(request, socket, head, (ws) => {
116
+ wss.emit("connection", ws, request);
117
+ });
118
+ } else {
119
+ socket.destroy();
120
+ }
82
121
  });
83
122
  },
84
123
 
@@ -87,10 +126,12 @@ exports.createConnectMiddleware = async function(config, options) {
87
126
 
88
127
  end() {
89
128
  metroServer.end();
90
- }
129
+ },
91
130
  };
92
131
  };
93
132
 
133
+ exports.createConnectMiddleware = createConnectMiddleware;
134
+
94
135
  exports.runServer = async (
95
136
  config,
96
137
  {
@@ -103,7 +144,10 @@ exports.runServer = async (
103
144
  //deprecated
104
145
  secureCert,
105
146
  // deprecated
106
- secureKey // deprecated
147
+ secureKey,
148
+ // deprecated
149
+ waitForBundler = false,
150
+ websocketEndpoints = {},
107
151
  }
108
152
  ) => {
109
153
  if (secure != null || secureCert != null || secureKey != null) {
@@ -119,13 +163,13 @@ exports.runServer = async (
119
163
  const connect = require("connect");
120
164
 
121
165
  const serverApp = connect();
122
- const {
123
- attachHmrServer,
124
- middleware,
125
- end
126
- } = await exports.createConnectMiddleware(config, {
127
- hasReducedPerformance
128
- });
166
+ const { middleware, end, metroServer } = await createConnectMiddleware(
167
+ config,
168
+ {
169
+ hasReducedPerformance,
170
+ waitForBundler,
171
+ }
172
+ );
129
173
  serverApp.use(middleware);
130
174
  let inspectorProxy = null;
131
175
 
@@ -142,7 +186,7 @@ exports.runServer = async (
142
186
  options = Object.assign(
143
187
  {
144
188
  key: fs.readFileSync(secureKey),
145
- cert: fs.readFileSync(secureCert)
189
+ cert: fs.readFileSync(secureCert),
146
190
  },
147
191
  secureServerOptions
148
192
  );
@@ -153,7 +197,7 @@ exports.runServer = async (
153
197
  httpServer = http.createServer(serverApp);
154
198
  }
155
199
 
156
- httpServer.on("error", error => {
200
+ httpServer.on("error", (error) => {
157
201
  if (onError) {
158
202
  onError(error);
159
203
  }
@@ -166,14 +210,40 @@ exports.runServer = async (
166
210
  onReady(httpServer);
167
211
  }
168
212
 
169
- attachHmrServer(httpServer);
213
+ Object.assign(websocketEndpoints, {
214
+ ...(inspectorProxy
215
+ ? { ...inspectorProxy.createWebSocketListeners(httpServer) }
216
+ : {}),
217
+ "/hot": createWebsocketServer({
218
+ websocketServer: new MetroHmrServer(
219
+ metroServer.getBundler(),
220
+ metroServer.getCreateModuleId(),
221
+ config
222
+ ),
223
+ }),
224
+ });
225
+ httpServer.on("upgrade", (request, socket, head) => {
226
+ const { pathname } = parse(request.url);
227
+
228
+ if (pathname != null && websocketEndpoints[pathname]) {
229
+ websocketEndpoints[pathname].handleUpgrade(
230
+ request,
231
+ socket,
232
+ head,
233
+ (ws) => {
234
+ websocketEndpoints[pathname].emit("connection", ws, request);
235
+ }
236
+ );
237
+ } else {
238
+ socket.destroy();
239
+ }
240
+ });
170
241
 
171
242
  if (inspectorProxy) {
172
- inspectorProxy.addWebSocketListener(httpServer); // TODO(hypuk): Refactor inspectorProxy.processRequest into separate request handlers
243
+ // TODO(hypuk): Refactor inspectorProxy.processRequest into separate request handlers
173
244
  // so that we could provide routes (/json/list and /json/version) here.
174
245
  // Currently this causes Metro to give warning about T31407894.
175
246
  // $FlowFixMe[method-unbinding] added when improving typing for this parameters
176
-
177
247
  serverApp.use(inspectorProxy.processRequest.bind(inspectorProxy));
178
248
  }
179
249
 
@@ -183,7 +253,7 @@ exports.runServer = async (
183
253
  // timeout of 120 seconds to respond to a request.
184
254
 
185
255
  httpServer.timeout = 0;
186
- httpServer.on("error", error => {
256
+ httpServer.on("error", (error) => {
187
257
  end();
188
258
  reject(error);
189
259
  });
@@ -206,11 +276,11 @@ exports.runBuild = async (
206
276
  out,
207
277
  platform = "web",
208
278
  sourceMap = false,
209
- sourceMapUrl
279
+ sourceMapUrl,
210
280
  }
211
281
  ) => {
212
282
  const metroServer = await runMetro(config, {
213
- watch: false
283
+ watch: false,
214
284
  });
215
285
 
216
286
  try {
@@ -222,7 +292,7 @@ exports.runBuild = async (
222
292
  platform,
223
293
  sourceMapUrl: sourceMap === false ? undefined : sourceMapUrl,
224
294
  createModuleIdFactory: config.serializer.createModuleIdFactory,
225
- onProgress
295
+ onProgress,
226
296
  };
227
297
 
228
298
  if (onBegin) {
@@ -243,7 +313,7 @@ exports.runBuild = async (
243
313
  bundleOutput,
244
314
  sourcemapOutput,
245
315
  dev,
246
- platform
316
+ platform,
247
317
  }; // eslint-disable-next-line no-console
248
318
 
249
319
  await output.save(metroBundle, outputOptions, console.log);
@@ -255,7 +325,7 @@ exports.runBuild = async (
255
325
  }
256
326
  };
257
327
 
258
- exports.buildGraph = async function(
328
+ exports.buildGraph = async function (
259
329
  config,
260
330
  {
261
331
  customTransformOptions = Object.create(null),
@@ -264,7 +334,7 @@ exports.buildGraph = async function(
264
334
  minify = false,
265
335
  onProgress,
266
336
  platform = "web",
267
- type = "module"
337
+ type = "module",
268
338
  }
269
339
  ) {
270
340
  const mergedConfig = await getConfig(config);
@@ -277,15 +347,14 @@ exports.buildGraph = async function(
277
347
  dev,
278
348
  minify,
279
349
  platform,
280
- type
350
+ type,
281
351
  });
282
352
  } finally {
283
353
  bundler.end();
284
354
  }
285
355
  };
286
356
 
287
- exports.attachMetroCli = function(
288
- // $FlowFixMe[value-as-type]
357
+ exports.attachMetroCli = function (
289
358
  yargs,
290
359
  { build = {}, serve = {}, dependencies = {} } = {}
291
360
  ) {
@@ -300,12 +369,8 @@ exports.attachMetroCli = function(
300
369
  }
301
370
 
302
371
  if (dependencies) {
303
- const {
304
- command,
305
- description,
306
- builder,
307
- handler
308
- } = makeDependenciesCommand();
372
+ const { command, description, builder, handler } =
373
+ makeDependenciesCommand();
309
374
  yargs.command(command, description, builder, handler);
310
375
  }
311
376