metro 0.75.0 → 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 (63) 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/DeltaCalculator.js +18 -11
  5. package/src/DeltaBundler/DeltaCalculator.js.flow +15 -11
  6. package/src/DeltaBundler/Graph.js +11 -28
  7. package/src/DeltaBundler/Graph.js.flow +7 -0
  8. package/src/DeltaBundler/Serializers/helpers/js.js +2 -7
  9. package/src/DeltaBundler/types.flow.js.flow +2 -7
  10. package/src/HmrServer.js +9 -32
  11. package/src/IncrementalBundler.js +9 -9
  12. package/src/IncrementalBundler.js.flow +4 -0
  13. package/src/ModuleGraph/worker/collectDependencies.js +53 -89
  14. package/src/ModuleGraph/worker/collectDependencies.js.flow +113 -141
  15. package/src/Server/symbolicate.js +1 -5
  16. package/src/Server.js +15 -53
  17. package/src/Server.js.flow +5 -0
  18. package/src/commands/dependencies.js +1 -5
  19. package/src/index.flow.js +1 -2
  20. package/src/integration_tests/basic_bundle/TestBigInt.js +0 -3
  21. package/src/integration_tests/basic_bundle/TestBigInt.js.flow +0 -3
  22. package/src/integration_tests/basic_bundle/require-context/mode-sync.js +1 -0
  23. package/src/integration_tests/basic_bundle/require-context/mode-sync.js.flow +1 -0
  24. package/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js +28 -0
  25. package/src/integration_tests/basic_bundle/require-resolveWeak/import-and-resolveWeak.js.flow +33 -0
  26. package/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js +20 -0
  27. package/src/integration_tests/basic_bundle/require-resolveWeak/multiple.js.flow +23 -0
  28. package/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js +18 -0
  29. package/src/integration_tests/basic_bundle/require-resolveWeak/never-required.js.flow +21 -0
  30. package/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js +28 -0
  31. package/src/integration_tests/basic_bundle/require-resolveWeak/require-and-resolveWeak.js.flow +33 -0
  32. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/counter-module.js +19 -0
  33. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/counter-module.js.flow +18 -0
  34. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/throwing-module.js +13 -0
  35. package/src/integration_tests/basic_bundle/require-resolveWeak/subdir/throwing-module.js.flow +11 -0
  36. package/src/integration_tests/basic_bundle/require-resolveWeak/utils.js +1 -0
  37. package/src/integration_tests/basic_bundle/require-resolveWeak/utils.js.flow +14 -0
  38. package/src/lib/CountingSet.js +1 -5
  39. package/src/lib/RamBundleParser.js +0 -1
  40. package/src/lib/RamBundleParser.js.flow +0 -1
  41. package/src/lib/TerminalReporter.js +9 -17
  42. package/src/lib/TerminalReporter.js.flow +7 -0
  43. package/src/lib/contextModuleTemplates.js +1 -1
  44. package/src/lib/contextModuleTemplates.js.flow +1 -1
  45. package/src/lib/formatBundlingError.js +0 -3
  46. package/src/lib/formatBundlingError.js.flow +0 -3
  47. package/src/lib/getAppendScripts.js +0 -3
  48. package/src/lib/getAppendScripts.js.flow +0 -3
  49. package/src/lib/getGraphId.js +2 -11
  50. package/src/lib/getPrependedScripts.js +2 -1
  51. package/src/lib/getPrependedScripts.js.flow +2 -1
  52. package/src/lib/reporting.js.flow +4 -0
  53. package/src/lib/transformHelpers.js +16 -22
  54. package/src/lib/transformHelpers.js.flow +7 -8
  55. package/src/node-haste/DependencyGraph/ModuleResolution.js +13 -11
  56. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +17 -5
  57. package/src/node-haste/DependencyGraph/createHasteMap.js +13 -38
  58. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +2 -1
  59. package/src/node-haste/DependencyGraph.js +28 -22
  60. package/src/node-haste/DependencyGraph.js.flow +25 -9
  61. package/src/node-haste/Module.js +5 -3
  62. package/src/node-haste/Module.js.flow +2 -3
  63. package/src/node-haste/ModuleCache.js +1 -5
@@ -15,7 +15,6 @@ const generate = require("@babel/generator").default;
15
15
  const template = require("@babel/template").default;
16
16
  const traverse = require("@babel/traverse").default;
17
17
  const types = require("@babel/types");
18
- const invariant = require("invariant");
19
18
  const nullthrows = require("nullthrows");
20
19
  const { isImport } = types;
21
20
  /**
@@ -28,21 +27,14 @@ const { isImport } = types;
28
27
  * The second argument is only provided for debugging purposes.
29
28
  */
30
29
  function collectDependencies(ast, options) {
31
- var _options$dependencyRe, _options$dependencyTr;
32
30
  const visited = new WeakSet();
33
31
  const state = {
34
32
  asyncRequireModulePathStringLiteral: null,
35
33
  dependencyCalls: new Set(),
36
34
  dependencyRegistry:
37
- (_options$dependencyRe = options.dependencyRegistry) !== null &&
38
- _options$dependencyRe !== void 0
39
- ? _options$dependencyRe
40
- : new DefaultModuleDependencyRegistry(),
35
+ options.dependencyRegistry ?? new DefaultModuleDependencyRegistry(),
41
36
  dependencyTransformer:
42
- (_options$dependencyTr = options.dependencyTransformer) !== null &&
43
- _options$dependencyTr !== void 0
44
- ? _options$dependencyTr
45
- : DefaultDependencyTransformer,
37
+ options.dependencyTransformer ?? DefaultDependencyTransformer,
46
38
  dependencyMapIdentifier: null,
47
39
  dynamicRequires: options.dynamicRequires,
48
40
  keepRequireNames: options.keepRequireNames,
@@ -68,26 +60,6 @@ function collectDependencies(ast, options) {
68
60
  });
69
61
  return;
70
62
  }
71
- if (name === "__jsResource" && !path.scope.getBinding(name)) {
72
- processImportCall(path, state, {
73
- asyncType: "async",
74
- jsResource: true,
75
- });
76
- return;
77
- }
78
- if (
79
- name === "__conditionallySplitJSResource" &&
80
- !path.scope.getBinding(name)
81
- ) {
82
- const args = path.get("arguments");
83
- invariant(Array.isArray(args), "Expected arguments to be an array");
84
- processImportCall(path, state, {
85
- asyncType: "async",
86
- jsResource: true,
87
- splitCondition: args[1],
88
- });
89
- return;
90
- }
91
63
 
92
64
  // Match `require.context`
93
65
  if (
@@ -108,6 +80,24 @@ function collectDependencies(ast, options) {
108
80
  visited.add(path.node);
109
81
  return;
110
82
  }
83
+
84
+ // Match `require.resolveWeak`
85
+ if (
86
+ callee.type === "MemberExpression" &&
87
+ // `require`
88
+ callee.object.type === "Identifier" &&
89
+ callee.object.name === "require" &&
90
+ // `resolveWeak`
91
+ callee.property.type === "Identifier" &&
92
+ callee.property.name === "resolveWeak" &&
93
+ !callee.computed &&
94
+ // Ensure `require` refers to the global and not something else.
95
+ !path.scope.getBinding("require")
96
+ ) {
97
+ processResolveWeakCall(path, state);
98
+ visited.add(path.node);
99
+ return;
100
+ }
111
101
  if (
112
102
  name != null &&
113
103
  state.dependencyCalls.has(name) &&
@@ -163,11 +153,8 @@ function getRequireContextArgs(path) {
163
153
  if (result.confident && typeof result.value === "string") {
164
154
  directory = result.value;
165
155
  } else {
166
- var _result$deopt;
167
156
  throw new InvalidRequireCallError(
168
- (_result$deopt = result.deopt) !== null && _result$deopt !== void 0
169
- ? _result$deopt
170
- : args[0],
157
+ result.deopt ?? args[0],
171
158
  "First argument of `require.context` should be a string denoting the directory to require."
172
159
  );
173
160
  }
@@ -180,11 +167,8 @@ function getRequireContextArgs(path) {
180
167
  if (result.confident && typeof result.value === "boolean") {
181
168
  recursive = result.value;
182
169
  } else if (!(result.confident && typeof result.value === "undefined")) {
183
- var _result$deopt2;
184
170
  throw new InvalidRequireCallError(
185
- (_result$deopt2 = result.deopt) !== null && _result$deopt2 !== void 0
186
- ? _result$deopt2
187
- : args[1],
171
+ result.deopt ?? args[1],
188
172
  "Second argument of `require.context` should be an optional boolean indicating if files should be imported recursively or not."
189
173
  );
190
174
  }
@@ -221,11 +205,8 @@ function getRequireContextArgs(path) {
221
205
  if (result.confident && typeof result.value === "string") {
222
206
  mode = getContextMode(args[3], result.value);
223
207
  } else if (!(result.confident && typeof result.value === "undefined")) {
224
- var _result$deopt3;
225
208
  throw new InvalidRequireCallError(
226
- (_result$deopt3 = result.deopt) !== null && _result$deopt3 !== void 0
227
- ? _result$deopt3
228
- : args[3],
209
+ result.deopt ?? args[3],
229
210
  'Fourth argument of `require.context` should be an optional string "mode" denoting how the modules will be resolved.'
230
211
  );
231
212
  }
@@ -279,6 +260,26 @@ function processRequireContextCall(path, state) {
279
260
  path.get("callee").replaceWith(types.identifier("require"));
280
261
  transformer.transformSyncRequire(path, dep, state);
281
262
  }
263
+ function processResolveWeakCall(path, state) {
264
+ const name = getModuleNameFromCallArgs(path);
265
+ if (name == null) {
266
+ throw new InvalidRequireCallError(path);
267
+ }
268
+ const dependency = registerDependency(
269
+ state,
270
+ {
271
+ name,
272
+ asyncType: "weak",
273
+ optional: isOptionalDependency(name, path, state),
274
+ },
275
+ path
276
+ );
277
+ path.replaceWith(
278
+ makeResolveWeakTemplate({
279
+ MODULE_ID: createModuleIDExpression(dependency, state),
280
+ })
281
+ );
282
+ }
282
283
  function collectImports(path, state) {
283
284
  if (path.node.source) {
284
285
  registerDependency(
@@ -302,15 +303,12 @@ function processImportCall(path, state, options) {
302
303
  {
303
304
  name,
304
305
  asyncType: options.asyncType,
305
- splitCondition: options.splitCondition,
306
306
  optional: isOptionalDependency(name, path, state),
307
307
  },
308
308
  path
309
309
  );
310
310
  const transformer = state.dependencyTransformer;
311
- if (options.jsResource) {
312
- transformer.transformJSResource(path, dep, state);
313
- } else if (options.asyncType === "async") {
311
+ if (options.asyncType === "async") {
314
312
  transformer.transformImportCall(path, dep, state);
315
313
  } else {
316
314
  transformer.transformPrefetch(path, dep, state);
@@ -338,14 +336,11 @@ function processRequireCall(path, state) {
338
336
  transformer.transformSyncRequire(path, dep, state);
339
337
  }
340
338
  function getNearestLocFromPath(path) {
341
- var _current;
342
339
  let current = path;
343
340
  while (current && !current.node.loc) {
344
341
  current = current.parentPath;
345
342
  }
346
- return (_current = current) === null || _current === void 0
347
- ? void 0
348
- : _current.node.loc;
343
+ return current?.node.loc;
349
344
  }
350
345
  function registerDependency(state, qualifier, path) {
351
346
  const dependency = state.dependencyRegistry.registerDependency(qualifier);
@@ -356,17 +351,10 @@ function registerDependency(state, qualifier, path) {
356
351
  return dependency;
357
352
  }
358
353
  function isOptionalDependency(name, path, state) {
359
- var _state$asyncRequireMo;
360
354
  const { allowOptionalDependencies } = state;
361
355
 
362
356
  // The async require module is a 'built-in'. Resolving should never fail -> treat it as non-optional.
363
- if (
364
- name ===
365
- ((_state$asyncRequireMo = state.asyncRequireModulePathStringLiteral) ===
366
- null || _state$asyncRequireMo === void 0
367
- ? void 0
368
- : _state$asyncRequireMo.value)
369
- ) {
357
+ if (name === state.asyncRequireModulePathStringLiteral?.value) {
370
358
  return false;
371
359
  }
372
360
  const isExcluded = () =>
@@ -397,10 +385,8 @@ function isOptionalDependency(name, path, state) {
397
385
  return false;
398
386
  }
399
387
  function getModuleNameFromCallArgs(path) {
400
- const expectedCount =
401
- path.node.callee.name === "__conditionallySplitJSResource" ? 2 : 1;
402
388
  const args = path.get("arguments");
403
- if (!Array.isArray(args) || args.length !== expectedCount) {
389
+ if (!Array.isArray(args) || args.length !== 1) {
404
390
  throw new InvalidRequireCallError(path);
405
391
  }
406
392
  const result = args[0].evaluate();
@@ -430,7 +416,7 @@ collectDependencies.InvalidRequireCallError = InvalidRequireCallError;
430
416
  * is reached. This makes dynamic require errors catchable by libraries that
431
417
  * want to use them.
432
418
  */
433
- const dynamicRequireErrorTemplate = template.statement(`
419
+ const dynamicRequireErrorTemplate = template.expression(`
434
420
  (function(line) {
435
421
  throw new Error(
436
422
  'Dynamic require defined at line ' + line + '; not supported by Metro',
@@ -442,14 +428,14 @@ const dynamicRequireErrorTemplate = template.statement(`
442
428
  * Produces a Babel template that transforms an "import(...)" call into a
443
429
  * "require(...)" call to the asyncRequire specified.
444
430
  */
445
- const makeAsyncRequireTemplate = template.statement(`
431
+ const makeAsyncRequireTemplate = template.expression(`
446
432
  require(ASYNC_REQUIRE_MODULE_PATH)(MODULE_ID, MODULE_NAME, DEPENDENCY_MAP.paths)
447
433
  `);
448
- const makeAsyncPrefetchTemplate = template.statement(`
434
+ const makeAsyncPrefetchTemplate = template.expression(`
449
435
  require(ASYNC_REQUIRE_MODULE_PATH).prefetch(MODULE_ID, MODULE_NAME, DEPENDENCY_MAP.paths)
450
436
  `);
451
- const makeJSResourceTemplate = template.statement(`
452
- require(ASYNC_REQUIRE_MODULE_PATH).resource(MODULE_ID, MODULE_NAME, DEPENDENCY_MAP.paths)
437
+ const makeResolveWeakTemplate = template.expression(`
438
+ MODULE_ID
453
439
  `);
454
440
  const DefaultDependencyTransformer = {
455
441
  transformSyncRequire(path, dependency, state) {
@@ -472,18 +458,6 @@ const DefaultDependencyTransformer = {
472
458
  })
473
459
  );
474
460
  },
475
- transformJSResource(path, dependency, state) {
476
- path.replaceWith(
477
- makeJSResourceTemplate({
478
- ASYNC_REQUIRE_MODULE_PATH: nullthrows(
479
- state.asyncRequireModulePathStringLiteral
480
- ),
481
- MODULE_ID: createModuleIDExpression(dependency, state),
482
- MODULE_NAME: createModuleNameLiteral(dependency),
483
- DEPENDENCY_MAP: nullthrows(state.dependencyMapIdentifier),
484
- })
485
- );
486
- },
487
461
  transformPrefetch(path, dependency, state) {
488
462
  path.replaceWith(
489
463
  makeAsyncPrefetchTemplate({
@@ -497,19 +471,9 @@ const DefaultDependencyTransformer = {
497
471
  );
498
472
  },
499
473
  transformIllegalDynamicRequire(path, state) {
500
- var _path$node$loc$start$, _path$node$loc;
501
474
  path.replaceWith(
502
475
  dynamicRequireErrorTemplate({
503
- LINE: types.numericLiteral(
504
- (_path$node$loc$start$ =
505
- (_path$node$loc = path.node.loc) === null ||
506
- _path$node$loc === void 0
507
- ? void 0
508
- : _path$node$loc.start.line) !== null &&
509
- _path$node$loc$start$ !== void 0
510
- ? _path$node$loc$start$
511
- : 0
512
- ),
476
+ LINE: types.numericLiteral(path.node.loc?.start.line ?? 0),
513
477
  })
514
478
  );
515
479
  },