metro 0.75.1 → 0.76.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 (55) hide show
  1. package/package.json +23 -24
  2. package/src/Assets.js +2 -2
  3. package/src/Assets.js.flow +1 -1
  4. package/src/DeltaBundler/Graph.js +11 -28
  5. package/src/DeltaBundler/Graph.js.flow +7 -0
  6. package/src/DeltaBundler/Serializers/helpers/js.js +2 -7
  7. package/src/DeltaBundler/types.flow.js.flow +1 -7
  8. package/src/HmrServer.js +9 -32
  9. package/src/IncrementalBundler.js +5 -9
  10. package/src/ModuleGraph/worker/collectDependencies.js +53 -89
  11. package/src/ModuleGraph/worker/collectDependencies.js.flow +113 -141
  12. package/src/Server/symbolicate.js +1 -5
  13. package/src/Server.js +10 -53
  14. package/src/commands/dependencies.js +1 -5
  15. package/src/index.flow.js +1 -2
  16. package/src/integration_tests/basic_bundle/TestBigInt.js +0 -3
  17. package/src/integration_tests/basic_bundle/TestBigInt.js.flow +0 -3
  18. package/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js +28 -0
  19. package/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js.flow +33 -0
  20. package/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js +20 -0
  21. package/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js.flow +23 -0
  22. package/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js +18 -0
  23. package/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js.flow +21 -0
  24. package/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js +28 -0
  25. package/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js.flow +33 -0
  26. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/counter-module.js +19 -0
  27. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/counter-module.js.flow +18 -0
  28. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/throwing-module.js +13 -0
  29. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/throwing-module.js.flow +11 -0
  30. package/src/integration_tests/basic_bundle/require-resolveWeak/utils.js +1 -0
  31. package/src/integration_tests/basic_bundle/require-resolveWeak/utils.js.flow +14 -0
  32. package/src/lib/CountingSet.js +1 -5
  33. package/src/lib/RamBundleParser.js +0 -1
  34. package/src/lib/RamBundleParser.js.flow +0 -1
  35. package/src/lib/TerminalReporter.js +3 -17
  36. package/src/lib/contextModuleTemplates.js +1 -1
  37. package/src/lib/contextModuleTemplates.js.flow +1 -1
  38. package/src/lib/formatBundlingError.js +0 -3
  39. package/src/lib/formatBundlingError.js.flow +0 -3
  40. package/src/lib/getAppendScripts.js +0 -3
  41. package/src/lib/getAppendScripts.js.flow +0 -3
  42. package/src/lib/getGraphId.js +2 -11
  43. package/src/lib/getPrependedScripts.js +0 -1
  44. package/src/lib/getPrependedScripts.js.flow +0 -1
  45. package/src/lib/transformHelpers.js +14 -22
  46. package/src/lib/transformHelpers.js.flow +5 -8
  47. package/src/node-haste/DependencyGraph/ModuleResolution.js +4 -11
  48. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +3 -5
  49. package/src/node-haste/DependencyGraph/createHasteMap.js +13 -38
  50. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +2 -1
  51. package/src/node-haste/DependencyGraph.js +6 -17
  52. package/src/node-haste/DependencyGraph.js.flow +1 -5
  53. package/src/node-haste/Module.js +5 -3
  54. package/src/node-haste/Module.js.flow +2 -3
  55. package/src/node-haste/ModuleCache.js +1 -5
@@ -23,8 +23,8 @@ import type {Type} from 'metro-transform-worker';
23
23
  import type {RequireContext} from './contextModule';
24
24
 
25
25
  import {getContextModuleTemplate} from './contextModuleTemplates';
26
+ import isAssetFile from 'metro-resolver/src/utils/isAssetFile';
26
27
 
27
- const path = require('path');
28
28
  import type {ResolverInputOptions} from '../shared/types.flow';
29
29
 
30
30
  type InlineRequiresRaw = {+blockList: {[string]: true, ...}, ...} | boolean;
@@ -143,6 +143,7 @@ async function getTransformFn(
143
143
  options,
144
144
  resolverOptions,
145
145
  );
146
+ const assetExts = new Set(config.resolver.assetExts);
146
147
 
147
148
  return async (modulePath: string, requireContext: ?RequireContext) => {
148
149
  let templateBuffer: Buffer;
@@ -173,11 +174,7 @@ async function getTransformFn(
173
174
  modulePath,
174
175
  {
175
176
  ...transformOptions,
176
- type: getType(
177
- transformOptions.type,
178
- modulePath,
179
- config.resolver.assetExts,
180
- ),
177
+ type: getType(transformOptions.type, modulePath, assetExts),
181
178
  inlineRequires: removeInlineRequiresBlockListFromOptions(
182
179
  modulePath,
183
180
  inlineRequires,
@@ -191,13 +188,13 @@ async function getTransformFn(
191
188
  function getType(
192
189
  type: string,
193
190
  filePath: string,
194
- assetExts: $ReadOnlyArray<string>,
191
+ assetExts: $ReadOnlySet<string>,
195
192
  ): Type {
196
193
  if (type === 'script') {
197
194
  return type;
198
195
  }
199
196
 
200
- if (assetExts.indexOf(path.extname(filePath).slice(1)) !== -1) {
197
+ if (isAssetFile(filePath, assetExts)) {
201
198
  return 'asset';
202
199
  }
203
200
 
@@ -23,7 +23,6 @@ class ModuleResolver {
23
23
 
24
24
  // An empty module, the result of resolving `emptyModulePath` from the project root.
25
25
 
26
- // $FlowFixMe[missing-local-annot]
27
26
  constructor(options) {
28
27
  this._options = options;
29
28
  const { projectRoot, moduleCache } = this._options;
@@ -61,10 +60,10 @@ class ModuleResolver {
61
60
  resolverOptions
62
61
  ) {
63
62
  const {
63
+ assetExts,
64
64
  disableHierarchicalLookup,
65
65
  doesFileExist,
66
66
  extraNodeModules,
67
- isAssetFile,
68
67
  mainFields,
69
68
  nodeModulesPaths,
70
69
  preferNativePlatform,
@@ -77,14 +76,13 @@ class ModuleResolver {
77
76
  unstable_getRealPath,
78
77
  } = this._options;
79
78
  try {
80
- var _resolverOptions$cust;
81
79
  const result = Resolver.resolve(
82
80
  createDefaultContext({
83
81
  allowHaste,
82
+ assetExts,
84
83
  disableHierarchicalLookup,
85
84
  doesFileExist,
86
85
  extraNodeModules,
87
- isAssetFile,
88
86
  mainFields,
89
87
  nodeModulesPaths,
90
88
  preferNativePlatform,
@@ -96,11 +94,7 @@ class ModuleResolver {
96
94
  unstable_enablePackageExports,
97
95
  unstable_getRealPath,
98
96
  unstable_logWarning: this._logWarning,
99
- customResolverOptions:
100
- (_resolverOptions$cust = resolverOptions.customResolverOptions) !==
101
- null && _resolverOptions$cust !== void 0
102
- ? _resolverOptions$cust
103
- : {},
97
+ customResolverOptions: resolverOptions.customResolverOptions ?? {},
104
98
  originModulePath: fromModule.path,
105
99
  resolveHasteModule: (name) =>
106
100
  this._options.getHasteModulePath(name, platform),
@@ -259,8 +253,7 @@ class UnableToResolveError extends Error {
259
253
  originModulePath,
260
254
  message
261
255
  ) + (codeFrameMessage ? "\n" + codeFrameMessage : "");
262
- this.cause =
263
- options === null || options === void 0 ? void 0 : options.cause;
256
+ this.cause = options?.cause;
264
257
  }
265
258
  buildCodeFrameMessage() {
266
259
  let file;
@@ -16,7 +16,6 @@ import type {
16
16
  DoesFileExist,
17
17
  FileCandidates,
18
18
  GetRealPath,
19
- IsAssetFile,
20
19
  Resolution,
21
20
  ResolveAsset,
22
21
  } from 'metro-resolver';
@@ -55,6 +54,7 @@ export type ModuleishCache<TPackage> = interface {
55
54
  };
56
55
 
57
56
  type Options<TPackage> = $ReadOnly<{
57
+ assetExts: $ReadOnlySet<string>,
58
58
  dirExists: DirExistsFn,
59
59
  disableHierarchicalLookup: boolean,
60
60
  doesFileExist: DoesFileExist,
@@ -62,7 +62,6 @@ type Options<TPackage> = $ReadOnly<{
62
62
  extraNodeModules: ?Object,
63
63
  getHasteModulePath: (name: string, platform: ?string) => ?string,
64
64
  getHastePackagePath: (name: string, platform: ?string) => ?string,
65
- isAssetFile: IsAssetFile,
66
65
  mainFields: $ReadOnlyArray<string>,
67
66
  moduleCache: ModuleishCache<TPackage>,
68
67
  nodeModulesPaths: $ReadOnlyArray<string>,
@@ -87,7 +86,6 @@ class ModuleResolver<TPackage: Packageish> {
87
86
  // An empty module, the result of resolving `emptyModulePath` from the project root.
88
87
  _cachedEmptyModule: ?BundlerResolution;
89
88
 
90
- // $FlowFixMe[missing-local-annot]
91
89
  constructor(options: Options<TPackage>) {
92
90
  this._options = options;
93
91
  const {projectRoot, moduleCache} = this._options;
@@ -127,10 +125,10 @@ class ModuleResolver<TPackage: Packageish> {
127
125
  resolverOptions: ResolverInputOptions,
128
126
  ): BundlerResolution {
129
127
  const {
128
+ assetExts,
130
129
  disableHierarchicalLookup,
131
130
  doesFileExist,
132
131
  extraNodeModules,
133
- isAssetFile,
134
132
  mainFields,
135
133
  nodeModulesPaths,
136
134
  preferNativePlatform,
@@ -147,10 +145,10 @@ class ModuleResolver<TPackage: Packageish> {
147
145
  const result = Resolver.resolve(
148
146
  createDefaultContext({
149
147
  allowHaste,
148
+ assetExts,
150
149
  disableHierarchicalLookup,
151
150
  doesFileExist,
152
151
  extraNodeModules,
153
- isAssetFile,
154
152
  mainFields,
155
153
  nodeModulesPaths,
156
154
  preferNativePlatform,
@@ -66,7 +66,7 @@ function getIgnorePattern(config) {
66
66
  const combine = (regexes) =>
67
67
  new RegExp(
68
68
  regexes
69
- .map((regex) => "(" + regex.source.replace(/\//g, path.sep) + ")")
69
+ .map((regex) => "(" + regex.source.replaceAll("/", path.sep) + ")")
70
70
  .join("|")
71
71
  );
72
72
 
@@ -77,37 +77,21 @@ function getIgnorePattern(config) {
77
77
  return ignorePattern;
78
78
  }
79
79
  function createHasteMap(config, options) {
80
- var _config$unstable_file, _options$throwOnModul;
81
80
  const dependencyExtractor =
82
- (options === null || options === void 0
83
- ? void 0
84
- : options.extractDependencies) === false
81
+ options?.extractDependencies === false
85
82
  ? null
86
83
  : config.resolver.dependencyExtractor;
87
84
  const computeDependencies = dependencyExtractor != null;
88
85
  return _metroFileMap.default.create({
89
86
  cacheManagerFactory:
90
- (_config$unstable_file =
91
- config === null || config === void 0
92
- ? void 0
93
- : config.unstable_fileMapCacheManagerFactory) !== null &&
94
- _config$unstable_file !== void 0
95
- ? _config$unstable_file
96
- : (buildParameters) => {
97
- var _config$fileMapCacheD;
98
- return new _metroFileMap.DiskCacheManager({
99
- buildParameters,
100
- cacheDirectory:
101
- (_config$fileMapCacheD = config.fileMapCacheDirectory) !==
102
- null && _config$fileMapCacheD !== void 0
103
- ? _config$fileMapCacheD
104
- : config.hasteMapCacheDirectory,
105
- cacheFilePrefix:
106
- options === null || options === void 0
107
- ? void 0
108
- : options.cacheFilePrefix,
109
- });
110
- },
87
+ config?.unstable_fileMapCacheManagerFactory ??
88
+ ((buildParameters) =>
89
+ new _metroFileMap.DiskCacheManager({
90
+ buildParameters,
91
+ cacheDirectory:
92
+ config.fileMapCacheDirectory ?? config.hasteMapCacheDirectory,
93
+ cacheFilePrefix: options?.cacheFilePrefix,
94
+ })),
111
95
  perfLoggerFactory: config.unstable_perfLoggerFactory,
112
96
  computeDependencies,
113
97
  computeSha1: true,
@@ -131,19 +115,10 @@ function createHasteMap(config, options) {
131
115
  resetCache: config.resetCache,
132
116
  rootDir: config.projectRoot,
133
117
  roots: config.watchFolders,
134
- throwOnModuleCollision:
135
- (_options$throwOnModul =
136
- options === null || options === void 0
137
- ? void 0
138
- : options.throwOnModuleCollision) !== null &&
139
- _options$throwOnModul !== void 0
140
- ? _options$throwOnModul
141
- : true,
118
+ throwOnModuleCollision: options?.throwOnModuleCollision ?? true,
119
+ unstable_preferTreeFS: config.watcher.unstable_preferTreeFS,
142
120
  useWatchman: config.resolver.useWatchman,
143
- watch:
144
- (options === null || options === void 0 ? void 0 : options.watch) == null
145
- ? !ci.isCI
146
- : options.watch,
121
+ watch: options?.watch == null ? !ci.isCI : options.watch,
147
122
  watchmanDeferStates: config.watcher.watchman.deferStates,
148
123
  });
149
124
  }
@@ -29,7 +29,7 @@ function getIgnorePattern(config: ConfigT): RegExp {
29
29
  const combine = (regexes: Array<RegExp>) =>
30
30
  new RegExp(
31
31
  regexes
32
- .map(regex => '(' + regex.source.replace(/\//g, path.sep) + ')')
32
+ .map(regex => '(' + regex.source.replaceAll('/', path.sep) + ')')
33
33
  .join('|'),
34
34
  );
35
35
 
@@ -90,6 +90,7 @@ function createHasteMap(
90
90
  rootDir: config.projectRoot,
91
91
  roots: config.watchFolders,
92
92
  throwOnModuleCollision: options?.throwOnModuleCollision ?? true,
93
+ unstable_preferTreeFS: config.watcher.unstable_preferTreeFS,
93
94
  useWatchman: config.resolver.useWatchman,
94
95
  watch: options?.watch == null ? !ci.isCI : options.watch,
95
96
  watchmanDeferStates: config.watcher.watchman.deferStates,
@@ -39,11 +39,7 @@ class DependencyGraph extends EventEmitter {
39
39
  constructor(config, options) {
40
40
  super();
41
41
  this._config = config;
42
- this._assetExtensions = new Set(
43
- config.resolver.assetExts.map((asset) => "." + asset)
44
- );
45
- const { hasReducedPerformance, watch } =
46
- options !== null && options !== void 0 ? options : {};
42
+ const { hasReducedPerformance, watch } = options ?? {};
47
43
  const initializingMetroLogEntry = log(
48
44
  createActionStartEntry("Initializing Metro")
49
45
  );
@@ -129,6 +125,7 @@ class DependencyGraph extends EventEmitter {
129
125
  }
130
126
  _createModuleResolver() {
131
127
  this._moduleResolver = new ModuleResolver({
128
+ assetExts: new Set(this._config.resolver.assetExts),
132
129
  dirExists: (filePath) => {
133
130
  try {
134
131
  return fs.lstatSync(filePath).isDirectory();
@@ -144,7 +141,6 @@ class DependencyGraph extends EventEmitter {
144
141
  this._hasteModuleMap.getModule(name, platform, true),
145
142
  getHastePackagePath: (name, platform) =>
146
143
  this._hasteModuleMap.getPackage(name, platform, true),
147
- isAssetFile: (file) => this._assetExtensions.has(path.extname(file)),
148
144
  mainFields: this._config.resolver.resolverMainFields,
149
145
  moduleCache: this._moduleCache,
150
146
  nodeModulesPaths: this._config.resolver.nodeModulesPaths,
@@ -241,7 +237,6 @@ class DependencyGraph extends EventEmitter {
241
237
  assumeFlatNodeModules: false,
242
238
  }
243
239
  ) {
244
- var _JSON$stringify, _resolverOptions$cust;
245
240
  const isSensitiveToOriginFolder =
246
241
  // Resolution is always relative to the origin folder unless we assume a flat node_modules
247
242
  !assumeFlatNodeModules ||
@@ -254,19 +249,13 @@ class DependencyGraph extends EventEmitter {
254
249
 
255
250
  // Compound key for the resolver cache
256
251
  const resolverOptionsKey =
257
- (_JSON$stringify = JSON.stringify(
258
- (_resolverOptions$cust = resolverOptions.customResolverOptions) !==
259
- null && _resolverOptions$cust !== void 0
260
- ? _resolverOptions$cust
261
- : {},
252
+ JSON.stringify(
253
+ resolverOptions.customResolverOptions ?? {},
262
254
  canonicalize
263
- )) !== null && _JSON$stringify !== void 0
264
- ? _JSON$stringify
265
- : "";
255
+ ) ?? "";
266
256
  const originKey = isSensitiveToOriginFolder ? path.dirname(from) : "";
267
257
  const targetKey = to;
268
- const platformKey =
269
- platform !== null && platform !== void 0 ? platform : NULL_PLATFORM;
258
+ const platformKey = platform ?? NULL_PLATFORM;
270
259
 
271
260
  // Traverse the resolver cache, which is a tree of maps
272
261
  const mapByResolverOptions = this._resolutionCache;
@@ -53,7 +53,6 @@ function getOrCreateMap<T>(
53
53
  }
54
54
 
55
55
  class DependencyGraph extends EventEmitter {
56
- _assetExtensions: Set<string>;
57
56
  _config: ConfigT;
58
57
  _haste: MetroFileMap;
59
58
  _fileSystem: FileSystem;
@@ -89,9 +88,6 @@ class DependencyGraph extends EventEmitter {
89
88
  super();
90
89
 
91
90
  this._config = config;
92
- this._assetExtensions = new Set(
93
- config.resolver.assetExts.map(asset => '.' + asset),
94
- );
95
91
 
96
92
  const {hasReducedPerformance, watch} = options ?? {};
97
93
  const initializingMetroLogEntry = log(
@@ -176,6 +172,7 @@ class DependencyGraph extends EventEmitter {
176
172
 
177
173
  _createModuleResolver() {
178
174
  this._moduleResolver = new ModuleResolver({
175
+ assetExts: new Set(this._config.resolver.assetExts),
179
176
  dirExists: (filePath: string) => {
180
177
  try {
181
178
  return fs.lstatSync(filePath).isDirectory();
@@ -191,7 +188,6 @@ class DependencyGraph extends EventEmitter {
191
188
  this._hasteModuleMap.getModule(name, platform, true),
192
189
  getHastePackagePath: (name, platform) =>
193
190
  this._hasteModuleMap.getPackage(name, platform, true),
194
- isAssetFile: file => this._assetExtensions.has(path.extname(file)),
195
191
  mainFields: this._config.resolver.resolverMainFields,
196
192
  moduleCache: this._moduleCache,
197
193
  nodeModulesPaths: this._config.resolver.nodeModulesPaths,
@@ -11,11 +11,13 @@
11
11
 
12
12
  "use strict";
13
13
 
14
- const isAbsolutePath = require("absolute-path");
14
+ var _path = _interopRequireDefault(require("path"));
15
+ function _interopRequireDefault(obj) {
16
+ return obj && obj.__esModule ? obj : { default: obj };
17
+ }
15
18
  class Module {
16
- // $FlowFixMe[missing-local-annot]
17
19
  constructor(file, moduleCache) {
18
- if (!isAbsolutePath(file)) {
20
+ if (!_path.default.isAbsolute(file)) {
19
21
  throw new Error("Expected file to be absolute path but got " + file);
20
22
  }
21
23
  this.path = file;
@@ -14,7 +14,7 @@
14
14
  import type ModuleCache from './ModuleCache';
15
15
  import type Package from './Package';
16
16
 
17
- const isAbsolutePath = require('absolute-path');
17
+ import path from 'path';
18
18
 
19
19
  class Module {
20
20
  path: string;
@@ -22,9 +22,8 @@ class Module {
22
22
  _moduleCache: ModuleCache;
23
23
  _sourceCode: ?string;
24
24
 
25
- // $FlowFixMe[missing-local-annot]
26
25
  constructor(file: string, moduleCache: ModuleCache) {
27
- if (!isAbsolutePath(file)) {
26
+ if (!path.isAbsolute(file)) {
28
27
  throw new Error('Expected file to be absolute path but got ' + file);
29
28
  }
30
29
 
@@ -43,7 +43,6 @@ class ModuleCache {
43
43
  return this.getPackageOf(module.path);
44
44
  }
45
45
  getPackageOf(modulePath) {
46
- var _this$_modulePathsByP;
47
46
  let packagePath = this._packagePathByModulePath[modulePath];
48
47
  if (packagePath && this._packageCache[packagePath]) {
49
48
  return this._packageCache[packagePath];
@@ -54,10 +53,7 @@ class ModuleCache {
54
53
  }
55
54
  this._packagePathByModulePath[modulePath] = packagePath;
56
55
  const modulePaths =
57
- (_this$_modulePathsByP = this._modulePathsByPackagePath[packagePath]) !==
58
- null && _this$_modulePathsByP !== void 0
59
- ? _this$_modulePathsByP
60
- : new Set();
56
+ this._modulePathsByPackagePath[packagePath] ?? new Set();
61
57
  modulePaths.add(modulePath);
62
58
  this._modulePathsByPackagePath[packagePath] = modulePaths;
63
59
  return this.getPackage(packagePath);