metro 0.65.2 → 0.67.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 +27 -27
  2. package/src/Assets.js +9 -9
  3. package/src/Assets.js.flow +5 -7
  4. package/src/Bundler/util.js +17 -15
  5. package/src/Bundler/util.js.flow +12 -10
  6. package/src/Bundler.js +22 -9
  7. package/src/Bundler.js.flow +20 -7
  8. package/src/DeltaBundler/DeltaCalculator.js +17 -15
  9. package/src/DeltaBundler/DeltaCalculator.js.flow +9 -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 +18 -8
  31. package/src/DeltaBundler/Serializers/helpers/js.js.flow +19 -10
  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 +16 -14
  45. package/src/DeltaBundler/Transformer.js.flow +15 -15
  46. package/src/DeltaBundler/Worker.js +8 -11
  47. package/src/DeltaBundler/Worker.js.flow +8 -11
  48. package/src/DeltaBundler/WorkerFarm.js +15 -18
  49. package/src/DeltaBundler/WorkerFarm.js.flow +9 -11
  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 +15 -1
  61. package/src/DeltaBundler.js +5 -5
  62. package/src/DeltaBundler.js.flow +4 -4
  63. package/src/HmrServer.js +73 -55
  64. package/src/HmrServer.js.flow +42 -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 +46 -32
  72. package/src/IncrementalBundler.js.flow +32 -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 +5 -3
  76. package/src/ModuleGraph/node-haste/HasteFS.js.flow +3 -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 +41 -31
  86. package/src/ModuleGraph/node-haste/node-haste.js.flow +28 -22
  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 +61 -40
  96. package/src/ModuleGraph/output/util.js.flow +51 -28
  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 +47 -31
  102. package/src/ModuleGraph/worker/JsFileWrapping.js +16 -16
  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 +16 -18
  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 +204 -150
  118. package/src/Server.js.flow +131 -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 +19 -19
  127. package/src/commands/serve.js.flow +7 -9
  128. package/src/index.js +117 -50
  129. package/src/index.js.flow +108 -40
  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 +2 -2
  139. package/src/integration_tests/basic_bundle/TestBundle.js.flow +1 -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 +17 -17
  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 +10 -6
  166. package/src/lib/BatchProcessor.js.flow +6 -1
  167. package/src/lib/JsonReporter.js +3 -2
  168. package/src/lib/JsonReporter.js.flow +2 -1
  169. package/src/lib/RamBundleParser.js +1 -1
  170. package/src/lib/RamBundleParser.js.flow +1 -1
  171. package/src/lib/TerminalReporter.js +15 -15
  172. package/src/lib/TerminalReporter.js.flow +15 -16
  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} +13 -23
  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 +18 -20
  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 +16 -15
  196. package/src/lib/getPrependedScripts.js.flow +8 -8
  197. package/src/lib/logToConsole.js +12 -5
  198. package/src/lib/logToConsole.js.flow +19 -6
  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 +18 -4
  207. package/src/lib/splitBundleOptions.js +5 -5
  208. package/src/lib/splitBundleOptions.js.flow +1 -1
  209. package/src/lib/transformHelpers.js +11 -10
  210. package/src/lib/transformHelpers.js.flow +6 -13
  211. package/src/node-haste/DependencyGraph/ModuleResolution.js +51 -30
  212. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +49 -48
  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 +51 -118
  218. package/src/node-haste/DependencyGraph.js.flow +38 -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 +2 -2
  224. package/src/node-haste/Package.js.flow +1 -1
  225. package/src/node-haste/lib/AssetPaths.js +5 -5
  226. package/src/node-haste/lib/AssetPaths.js.flow +7 -6
  227. package/src/node-haste/lib/parsePlatformFilePath.js +4 -4
  228. package/src/node-haste/lib/parsePlatformFilePath.js.flow +2 -2
  229. package/src/shared/output/RamBundle/as-assets.js +14 -15
  230. package/src/shared/output/RamBundle/as-assets.js.flow +9 -11
  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 +6 -4
  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.
@@ -9,23 +9,53 @@
9
9
  */
10
10
  "use strict";
11
11
 
12
- const Resolver = require("metro-resolver");
12
+ const { codeFrameColumns } = require("@babel/code-frame");
13
13
 
14
14
  const fs = require("fs");
15
15
 
16
16
  const invariant = require("invariant");
17
17
 
18
+ const Resolver = require("metro-resolver");
19
+
18
20
  const path = require("path");
19
21
 
20
22
  const util = require("util");
21
23
 
22
- const { codeFrameColumns } = require("@babel/code-frame");
23
-
24
24
  class ModuleResolver {
25
- static EMPTY_MODULE = require.resolve("./assets/empty-module.js");
26
-
25
+ // A module representing the project root, used as the origin when resolving `emptyModulePath`.
26
+ // An empty module, the result of resolving `emptyModulePath` from the project root.
27
27
  constructor(options) {
28
28
  this._options = options;
29
+ const { projectRoot, moduleCache } = this._options;
30
+ this._projectRootFakeModule = {
31
+ path: path.join(projectRoot, "_"),
32
+ getPackage: () =>
33
+ moduleCache.getPackageOf(this._projectRootFakeModule.path),
34
+
35
+ isHaste() {
36
+ throw new Error("not implemented");
37
+ },
38
+
39
+ getName() {
40
+ throw new Error("not implemented");
41
+ },
42
+ };
43
+ }
44
+
45
+ _getEmptyModule() {
46
+ let emptyModule = this._cachedEmptyModule;
47
+
48
+ if (!emptyModule) {
49
+ emptyModule = this.resolveDependency(
50
+ this._projectRootFakeModule,
51
+ this._options.emptyModulePath,
52
+ false,
53
+ null
54
+ );
55
+ this._cachedEmptyModule = emptyModule;
56
+ }
57
+
58
+ return emptyModule;
29
59
  }
30
60
 
31
61
  _redirectRequire(fromModule, modulePath) {
@@ -51,7 +81,6 @@ class ModuleResolver {
51
81
  ); // Since the redirected path is still relative to the package root,
52
82
  // we have to transform it back to be module-relative (as it
53
83
  // originally was)
54
- // $FlowFixMe[incompatible-type]
55
84
 
56
85
  if (redirectedPath !== false) {
57
86
  redirectedPath =
@@ -66,7 +95,7 @@ class ModuleResolver {
66
95
  }
67
96
  } else {
68
97
  const pck = path.isAbsolute(modulePath)
69
- ? moduleCache.getModule(modulePath).getPackage()
98
+ ? moduleCache.getPackageOf(modulePath)
70
99
  : fromModule.getPackage();
71
100
 
72
101
  if (pck) {
@@ -87,15 +116,15 @@ class ModuleResolver {
87
116
  {
88
117
  ...this._options,
89
118
  originModulePath: fromModule.path,
90
- redirectModulePath: modulePath =>
119
+ redirectModulePath: (modulePath) =>
91
120
  this._redirectRequire(fromModule, modulePath),
92
121
  allowHaste,
93
122
  platform,
94
- resolveHasteModule: name =>
123
+ resolveHasteModule: (name) =>
95
124
  this._options.moduleMap.getModule(name, platform, true),
96
- resolveHastePackage: name =>
125
+ resolveHastePackage: (name) =>
97
126
  this._options.moduleMap.getPackage(name, platform, true),
98
- getPackageMainPath: this._getPackageMainPath
127
+ getPackageMainPath: this._getPackageMainPath,
99
128
  },
100
129
  moduleName,
101
130
  platform
@@ -114,7 +143,7 @@ class ModuleResolver {
114
143
  )}`,
115
144
  ` * ${Resolver.formatFileCandidates(
116
145
  this._removeRoot(candidates.dir)
117
- )}`
146
+ )}`,
118
147
  ].join("\n")
119
148
  );
120
149
  }
@@ -122,8 +151,8 @@ class ModuleResolver {
122
151
  if (error instanceof Resolver.FailedToResolveNameError) {
123
152
  const { dirPaths, extraPaths } = error;
124
153
  const displayDirPaths = dirPaths
125
- .filter(dirPath => this._options.dirExists(dirPath))
126
- .map(dirPath => path.relative(this._options.projectRoot, dirPath))
154
+ .filter((dirPath) => this._options.dirExists(dirPath))
155
+ .map((dirPath) => path.relative(this._options.projectRoot, dirPath))
127
156
  .concat(extraPaths);
128
157
  const hint = displayDirPaths.length ? " or in these directories:" : "";
129
158
  throw new UnableToResolveError(
@@ -131,12 +160,7 @@ class ModuleResolver {
131
160
  moduleName,
132
161
  [
133
162
  `${moduleName} could not be found within the project${hint || "."}`,
134
- ...displayDirPaths.map(dirPath => ` ${dirPath}`),
135
- "\nIf you are sure the module exists, try these steps:",
136
- " 1. Clear watchman watches: watchman watch-del-all",
137
- " 2. Delete node_modules and run yarn install",
138
- " 3. Reset Metro's cache: yarn start --reset-cache",
139
- " 4. Remove the cache: rm -rf /tmp/metro-*"
163
+ ...displayDirPaths.map((dirPath) => ` ${dirPath}`),
140
164
  ].join("\n")
141
165
  );
142
166
  }
@@ -145,7 +169,7 @@ class ModuleResolver {
145
169
  }
146
170
  }
147
171
 
148
- _getPackageMainPath = packageJsonPath => {
172
+ _getPackageMainPath = (packageJsonPath) => {
149
173
  const package_ = this._options.moduleCache.getPackage(packageJsonPath);
150
174
 
151
175
  return package_.getMain(this._options.mainFields);
@@ -168,10 +192,7 @@ class ModuleResolver {
168
192
  return this._options.moduleCache.getModule(arbitrary);
169
193
 
170
194
  case "empty":
171
- const { moduleCache } = this._options;
172
- const module_ = moduleCache.getModule(ModuleResolver.EMPTY_MODULE);
173
- invariant(module_ != null, "empty module is not available");
174
- return module_;
195
+ return this._getEmptyModule();
175
196
 
176
197
  default:
177
198
  resolution.type;
@@ -266,11 +287,11 @@ class UnableToResolveError extends Error {
266
287
  {
267
288
  start: {
268
289
  column: column + 1,
269
- line: lineNumber + 1
270
- }
290
+ line: lineNumber + 1,
291
+ },
271
292
  },
272
293
  {
273
- forceColor: process.env.NODE_ENV !== "test"
294
+ forceColor: process.env.NODE_ENV !== "test",
274
295
  }
275
296
  );
276
297
  }
@@ -278,5 +299,5 @@ class UnableToResolveError extends Error {
278
299
 
279
300
  module.exports = {
280
301
  ModuleResolver,
281
- UnableToResolveError
302
+ UnableToResolveError,
282
303
  };
@@ -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,15 +10,7 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const Resolver = require('metro-resolver');
14
-
15
- const fs = require('fs');
16
- const invariant = require('invariant');
17
- const path = require('path');
18
- const util = require('util');
19
-
20
- const {codeFrameColumns} = require('@babel/code-frame');
21
-
13
+ import type {ModuleMap} from './types';
22
14
  import type {
23
15
  CustomResolver,
24
16
  DoesFileExist,
@@ -28,54 +20,44 @@ import type {
28
20
  ResolveAsset,
29
21
  } from 'metro-resolver';
30
22
 
23
+ const {codeFrameColumns} = require('@babel/code-frame');
24
+ const fs = require('fs');
25
+ const invariant = require('invariant');
26
+ const Resolver = require('metro-resolver');
27
+ const path = require('path');
28
+ const util = require('util');
29
+
31
30
  export type DirExistsFn = (filePath: string) => boolean;
32
31
 
33
- export type Packageish = {
32
+ export type Packageish = interface {
34
33
  path: string,
35
34
  redirectRequire(
36
35
  toModuleName: string,
37
36
  mainFields: $ReadOnlyArray<string>,
38
37
  ): string | false,
39
38
  getMain(mainFields: $ReadOnlyArray<string>): string,
40
- ...
41
39
  };
42
40
 
43
- export type Moduleish = {
41
+ export type Moduleish = interface {
44
42
  +path: string,
45
43
  getPackage(): ?Packageish,
46
- ...
47
44
  };
48
45
 
49
- /**
50
- * `jest-haste-map`'s interface for ModuleMap.
51
- */
52
- export type ModuleMap = {
53
- getModule(
54
- name: string,
55
- platform: string | null,
56
- supportsNativePlatform: ?boolean,
57
- ): ?string,
58
- getPackage(
59
- name: string,
60
- platform: string | null,
61
- supportsNativePlatform: ?boolean,
62
- ): ?string,
63
- ...
64
- };
65
-
66
- export type ModuleishCache<TModule, TPackage> = {
46
+ export type ModuleishCache<TModule, TPackage> = interface {
67
47
  getPackage(
68
48
  name: string,
69
49
  platform?: string,
70
50
  supportsNativePlatform?: boolean,
71
51
  ): TPackage,
72
52
  getModule(path: string): TModule,
73
- ...
53
+ getPackageOf(modulePath: string): ?TPackage,
74
54
  };
75
55
 
76
56
  type Options<TModule, TPackage> = {|
77
57
  +dirExists: DirExistsFn,
58
+ +disableHierarchicalLookup: boolean,
78
59
  +doesFileExist: DoesFileExist,
60
+ +emptyModulePath: string,
79
61
  +extraNodeModules: ?Object,
80
62
  +isAssetFile: IsAssetFile,
81
63
  +mainFields: $ReadOnlyArray<string>,
@@ -91,14 +73,42 @@ type Options<TModule, TPackage> = {|
91
73
 
92
74
  class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
93
75
  _options: Options<TModule, TPackage>;
94
-
95
- static EMPTY_MODULE: string = require.resolve('./assets/empty-module.js');
76
+ // A module representing the project root, used as the origin when resolving `emptyModulePath`.
77
+ _projectRootFakeModule: Moduleish;
78
+ // An empty module, the result of resolving `emptyModulePath` from the project root.
79
+ _cachedEmptyModule: ?TModule;
96
80
 
97
81
  constructor(options: Options<TModule, TPackage>) {
98
82
  this._options = options;
83
+ const {projectRoot, moduleCache} = this._options;
84
+ this._projectRootFakeModule = {
85
+ path: path.join(projectRoot, '_'),
86
+ getPackage: () =>
87
+ moduleCache.getPackageOf(this._projectRootFakeModule.path),
88
+ isHaste() {
89
+ throw new Error('not implemented');
90
+ },
91
+ getName() {
92
+ throw new Error('not implemented');
93
+ },
94
+ };
95
+ }
96
+
97
+ _getEmptyModule() {
98
+ let emptyModule = this._cachedEmptyModule;
99
+ if (!emptyModule) {
100
+ emptyModule = this.resolveDependency(
101
+ this._projectRootFakeModule,
102
+ this._options.emptyModulePath,
103
+ false,
104
+ null,
105
+ );
106
+ this._cachedEmptyModule = emptyModule;
107
+ }
108
+ return emptyModule;
99
109
  }
100
110
 
101
- _redirectRequire(fromModule: TModule, modulePath: string): string | false {
111
+ _redirectRequire(fromModule: Moduleish, modulePath: string): string | false {
102
112
  const moduleCache = this._options.moduleCache;
103
113
  try {
104
114
  if (modulePath.startsWith('.')) {
@@ -123,7 +133,6 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
123
133
  // Since the redirected path is still relative to the package root,
124
134
  // we have to transform it back to be module-relative (as it
125
135
  // originally was)
126
- // $FlowFixMe[incompatible-type]
127
136
  if (redirectedPath !== false) {
128
137
  redirectedPath =
129
138
  './' +
@@ -137,7 +146,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
137
146
  }
138
147
  } else {
139
148
  const pck = path.isAbsolute(modulePath)
140
- ? moduleCache.getModule(modulePath).getPackage()
149
+ ? moduleCache.getPackageOf(modulePath)
141
150
  : fromModule.getPackage();
142
151
 
143
152
  if (pck) {
@@ -153,7 +162,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
153
162
  }
154
163
 
155
164
  resolveDependency(
156
- fromModule: TModule,
165
+ fromModule: Moduleish,
157
166
  moduleName: string,
158
167
  allowHaste: boolean,
159
168
  platform: string | null,
@@ -217,11 +226,6 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
217
226
  [
218
227
  `${moduleName} could not be found within the project${hint || '.'}`,
219
228
  ...displayDirPaths.map((dirPath: string) => ` ${dirPath}`),
220
- '\nIf you are sure the module exists, try these steps:',
221
- ' 1. Clear watchman watches: watchman watch-del-all',
222
- ' 2. Delete node_modules and run yarn install',
223
- " 3. Reset Metro's cache: yarn start --reset-cache",
224
- ' 4. Remove the cache: rm -rf /tmp/metro-*',
225
229
  ].join('\n'),
226
230
  );
227
231
  }
@@ -249,10 +253,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
249
253
  invariant(arbitrary != null, 'invalid asset resolution');
250
254
  return this._options.moduleCache.getModule(arbitrary);
251
255
  case 'empty':
252
- const {moduleCache} = this._options;
253
- const module_ = moduleCache.getModule(ModuleResolver.EMPTY_MODULE);
254
- invariant(module_ != null, 'empty module is not available');
255
- return module_;
256
+ return this._getEmptyModule();
256
257
  default:
257
258
  (resolution.type: empty);
258
259
  throw new Error('invalid type');
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ var _jestHasteMap = _interopRequireDefault(require("jest-haste-map"));
4
+
5
+ function _interopRequireDefault(obj) {
6
+ return obj && obj.__esModule ? obj : { default: obj };
7
+ }
8
+
9
+ /**
10
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
11
+ *
12
+ * This source code is licensed under the MIT license found in the
13
+ * LICENSE file in the root directory of this source tree.
14
+ *
15
+ *
16
+ * @format
17
+ */
18
+ // $FlowFixMe: Types for `jest-haste-map`
19
+ const ci = require("ci-info");
20
+
21
+ const path = require("path");
22
+
23
+ const JEST_HASTE_MAP_CACHE_BREAKER = 5;
24
+
25
+ function getIgnorePattern(config) {
26
+ // For now we support both options
27
+ const { blockList, blacklistRE } = config.resolver;
28
+ const ignorePattern = blacklistRE || blockList; // If neither option has been set, use default pattern
29
+
30
+ if (!ignorePattern) {
31
+ return / ^/;
32
+ }
33
+
34
+ const combine = (regexes) =>
35
+ new RegExp(
36
+ regexes
37
+ .map((regex) => "(" + regex.source.replace(/\//g, path.sep) + ")")
38
+ .join("|")
39
+ ); // If ignorePattern is an array, merge it into one
40
+
41
+ if (Array.isArray(ignorePattern)) {
42
+ return combine(ignorePattern);
43
+ }
44
+
45
+ return ignorePattern;
46
+ }
47
+
48
+ function createHasteMap(config, options) {
49
+ var _options$name, _options$throwOnModul;
50
+
51
+ const dependencyExtractor =
52
+ (options === null || options === void 0
53
+ ? void 0
54
+ : options.extractDependencies) === false
55
+ ? null
56
+ : config.resolver.dependencyExtractor;
57
+ const computeDependencies = dependencyExtractor != null;
58
+ const hasteConfig = {
59
+ cacheDirectory: config.hasteMapCacheDirectory,
60
+ computeDependencies,
61
+ computeSha1: true,
62
+ dependencyExtractor: config.resolver.dependencyExtractor,
63
+ extensions: config.resolver.sourceExts.concat(config.resolver.assetExts),
64
+ forceNodeFilesystemAPI: !config.resolver.useWatchman,
65
+ hasteImplModulePath: config.resolver.hasteImplModulePath,
66
+ hasteMapModulePath: config.resolver.unstable_hasteMapModulePath,
67
+ ignorePattern: getIgnorePattern(config),
68
+ maxWorkers: config.maxWorkers,
69
+ mocksPattern: "",
70
+ name: `${
71
+ (_options$name =
72
+ options === null || options === void 0 ? void 0 : options.name) !==
73
+ null && _options$name !== void 0
74
+ ? _options$name
75
+ : "metro"
76
+ }-${JEST_HASTE_MAP_CACHE_BREAKER}`,
77
+ platforms: config.resolver.platforms,
78
+ retainAllFiles: true,
79
+ resetCache: config.resetCache,
80
+ rootDir: config.projectRoot,
81
+ roots: config.watchFolders,
82
+ throwOnModuleCollision:
83
+ (_options$throwOnModul =
84
+ options === null || options === void 0
85
+ ? void 0
86
+ : options.throwOnModuleCollision) !== null &&
87
+ _options$throwOnModul !== void 0
88
+ ? _options$throwOnModul
89
+ : true,
90
+ useWatchman: config.resolver.useWatchman,
91
+ watch:
92
+ (options === null || options === void 0 ? void 0 : options.watch) == null
93
+ ? !ci.isCI
94
+ : options.watch,
95
+ };
96
+ return _jestHasteMap.default.create(hasteConfig);
97
+ }
98
+
99
+ module.exports = createHasteMap;
@@ -0,0 +1,88 @@
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
+ */
10
+
11
+ import type {HasteConfig, HasteMap} from './types';
12
+ import type {ConfigT} from 'metro-config/src/configTypes.flow';
13
+
14
+ // $FlowFixMe: Types for `jest-haste-map`
15
+ import JestHasteMap from 'jest-haste-map';
16
+
17
+ const ci = require('ci-info');
18
+ const path = require('path');
19
+
20
+ const JEST_HASTE_MAP_CACHE_BREAKER = 5;
21
+
22
+ function getIgnorePattern(config: ConfigT): RegExp {
23
+ // For now we support both options
24
+ const {blockList, blacklistRE} = config.resolver;
25
+ const ignorePattern = blacklistRE || blockList;
26
+
27
+ // If neither option has been set, use default pattern
28
+ if (!ignorePattern) {
29
+ return / ^/;
30
+ }
31
+
32
+ const combine = regexes =>
33
+ new RegExp(
34
+ regexes
35
+ .map(regex => '(' + regex.source.replace(/\//g, path.sep) + ')')
36
+ .join('|'),
37
+ );
38
+
39
+ // If ignorePattern is an array, merge it into one
40
+ if (Array.isArray(ignorePattern)) {
41
+ return combine(ignorePattern);
42
+ }
43
+
44
+ return ignorePattern;
45
+ }
46
+
47
+ function createHasteMap(
48
+ config: ConfigT,
49
+ options?: $ReadOnly<{
50
+ extractDependencies?: boolean,
51
+ watch?: boolean,
52
+ throwOnModuleCollision?: boolean,
53
+ name?: string,
54
+ }>,
55
+ ): HasteMap {
56
+ const dependencyExtractor =
57
+ options?.extractDependencies === false
58
+ ? null
59
+ : config.resolver.dependencyExtractor;
60
+ const computeDependencies = dependencyExtractor != null;
61
+
62
+ const hasteConfig: HasteConfig = {
63
+ cacheDirectory: config.hasteMapCacheDirectory,
64
+ computeDependencies,
65
+ computeSha1: true,
66
+ dependencyExtractor: config.resolver.dependencyExtractor,
67
+ extensions: config.resolver.sourceExts.concat(config.resolver.assetExts),
68
+ forceNodeFilesystemAPI: !config.resolver.useWatchman,
69
+ hasteImplModulePath: config.resolver.hasteImplModulePath,
70
+ hasteMapModulePath: config.resolver.unstable_hasteMapModulePath,
71
+ ignorePattern: getIgnorePattern(config),
72
+ maxWorkers: config.maxWorkers,
73
+ mocksPattern: '',
74
+ name: `${options?.name ?? 'metro'}-${JEST_HASTE_MAP_CACHE_BREAKER}`,
75
+ platforms: config.resolver.platforms,
76
+ retainAllFiles: true,
77
+ resetCache: config.resetCache,
78
+ rootDir: config.projectRoot,
79
+ roots: config.watchFolders,
80
+ throwOnModuleCollision: options?.throwOnModuleCollision ?? true,
81
+ useWatchman: config.resolver.useWatchman,
82
+ watch: options?.watch == null ? !ci.isCI : options.watch,
83
+ };
84
+
85
+ return JestHasteMap.create(hasteConfig);
86
+ }
87
+
88
+ module.exports = createHasteMap;
@@ -1,10 +1,10 @@
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.
6
6
  *
7
- * @format
8
7
  *
8
+ * @format
9
9
  */
10
10
  "use strict";
@@ -0,0 +1,88 @@
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
+
13
+ import type EventEmitter from 'events';
14
+
15
+ // TODO(cpojer): Create a jest-types repo.
16
+ export type HasteFS = {
17
+ exists(filePath: string): boolean,
18
+ getAllFiles(): Array<string>,
19
+ getDependencies(filePath: string): Array<string>,
20
+ getFileIterator(): Iterator<string>,
21
+ getModuleName(filePath: string): ?string,
22
+ getSha1(string): ?string,
23
+ matchFiles(pattern: RegExp | string): Array<string>,
24
+ ...
25
+ };
26
+
27
+ export type HasteConfig = $ReadOnly<{
28
+ cacheDirectory?: ?string,
29
+ computeDependencies?: ?boolean,
30
+ computeSha1?: ?boolean,
31
+ dependencyExtractor?: ?string | null,
32
+ enableSymlinks?: ?boolean,
33
+ extensions: $ReadOnlyArray<string>,
34
+ forceNodeFilesystemAPI?: ?boolean,
35
+ hasteImplModulePath?: ?string,
36
+ hasteMapModulePath?: ?string,
37
+ ignorePattern?: ?RegExp | ((str: string) => boolean),
38
+ maxWorkers: number,
39
+ mocksPattern?: ?string,
40
+ name: string,
41
+ platforms: $ReadOnlyArray<string>,
42
+ resetCache?: ?boolean,
43
+ retainAllFiles: boolean,
44
+ rootDir: string,
45
+ roots: $ReadOnlyArray<string>,
46
+ skipPackageJson?: ?boolean,
47
+ throwOnModuleCollision?: ?boolean,
48
+ useWatchman?: ?boolean,
49
+ watch?: ?boolean,
50
+ ...
51
+ }>;
52
+
53
+ type ModuleMapItem = {[platform: string]: ModuleMetaData};
54
+ type ModuleMetaData = [string, number];
55
+ type MockData = Map<string, string>;
56
+ type ModuleMapData = Map<string, ModuleMapItem>;
57
+ type DuplicatesSet = Map<string, number>;
58
+ type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;
59
+
60
+ type RawModuleMap = {
61
+ rootDir: string,
62
+ duplicates: DuplicatesIndex,
63
+ map: ModuleMapData,
64
+ mocks: MockData,
65
+ };
66
+
67
+ // `jest-haste-map`'s interface for ModuleMap.
68
+ export type ModuleMap = {
69
+ getModule(
70
+ name: string,
71
+ platform: string | null,
72
+ supportsNativePlatform: ?boolean,
73
+ ): ?string,
74
+ getPackage(
75
+ name: string,
76
+ platform: string | null,
77
+ supportsNativePlatform: ?boolean,
78
+ ): ?string,
79
+ getRawModuleMap(): RawModuleMap,
80
+ ...
81
+ };
82
+
83
+ // Interface as used by node-haste / internal tools, satisfied by JestHasteMap
84
+ export interface HasteMap extends EventEmitter {
85
+ build: () => Promise<{hasteFS: HasteFS, moduleMap: ModuleMap}>;
86
+ end: () => void;
87
+ getCacheFilePath: () => string;
88
+ }