metro 0.71.3 → 0.72.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/package.json +23 -21
  2. package/src/Assets.js +3 -2
  3. package/src/Assets.js.flow +3 -2
  4. package/src/Bundler.js +11 -2
  5. package/src/Bundler.js.flow +7 -1
  6. package/src/DeltaBundler/DeltaCalculator.js +83 -21
  7. package/src/DeltaBundler/DeltaCalculator.js.flow +61 -8
  8. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js +3 -2
  9. package/src/DeltaBundler/Serializers/baseBytecodeBundle.js.flow +2 -1
  10. package/src/DeltaBundler/Serializers/helpers/bytecode.js +2 -2
  11. package/src/DeltaBundler/Serializers/helpers/bytecode.js.flow +2 -1
  12. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +1 -3
  13. package/src/DeltaBundler/Transformer.js +27 -4
  14. package/src/DeltaBundler/Transformer.js.flow +18 -2
  15. package/src/DeltaBundler/Worker.flow.js +45 -1
  16. package/src/DeltaBundler/Worker.flow.js.flow +42 -1
  17. package/src/DeltaBundler/WorkerFarm.js +3 -2
  18. package/src/DeltaBundler/WorkerFarm.js.flow +3 -1
  19. package/src/DeltaBundler/graphOperations.js +170 -63
  20. package/src/DeltaBundler/graphOperations.js.flow +144 -64
  21. package/src/DeltaBundler/types.flow.js.flow +11 -5
  22. package/src/DeltaBundler.js.flow +1 -1
  23. package/src/HmrServer.js +11 -5
  24. package/src/HmrServer.js.flow +11 -4
  25. package/src/IncrementalBundler.js +22 -4
  26. package/src/IncrementalBundler.js.flow +23 -4
  27. package/src/ModuleGraph/node-haste/HasteFS.js.flow +1 -1
  28. package/src/ModuleGraph/node-haste/Package.js.flow +5 -5
  29. package/src/ModuleGraph/node-haste/node-haste.js +19 -8
  30. package/src/ModuleGraph/node-haste/node-haste.js.flow +43 -16
  31. package/src/ModuleGraph/output/indexed-ram-bundle.js.flow +5 -13
  32. package/src/ModuleGraph/output/multiple-files-ram-bundle.js.flow +4 -14
  33. package/src/ModuleGraph/output/util.js +3 -4
  34. package/src/ModuleGraph/output/util.js.flow +3 -3
  35. package/src/ModuleGraph/types.flow.js.flow +28 -5
  36. package/src/ModuleGraph/worker/collectDependencies.js +19 -30
  37. package/src/ModuleGraph/worker/collectDependencies.js.flow +23 -41
  38. package/src/Server.js +90 -32
  39. package/src/Server.js.flow +143 -44
  40. package/src/cli-utils.js.flow +1 -1
  41. package/src/commands/build.js +1 -2
  42. package/src/commands/build.js.flow +6 -9
  43. package/src/commands/dependencies.js +1 -1
  44. package/src/commands/serve.js +2 -1
  45. package/src/commands/serve.js.flow +7 -8
  46. package/src/index.flow.js +27 -16
  47. package/src/index.flow.js.flow +24 -15
  48. package/src/integration_tests/basic_bundle/require-context/conflict.js +25 -0
  49. package/src/integration_tests/basic_bundle/require-context/conflict.js.flow +27 -0
  50. package/src/integration_tests/basic_bundle/require-context/empty.js +29 -0
  51. package/src/integration_tests/basic_bundle/require-context/empty.js.flow +26 -0
  52. package/src/integration_tests/basic_bundle/require-context/matching.js +26 -0
  53. package/src/integration_tests/basic_bundle/require-context/matching.js.flow +27 -0
  54. package/src/integration_tests/basic_bundle/require-context/mode-eager.js +22 -0
  55. package/src/integration_tests/basic_bundle/require-context/mode-eager.js.flow +24 -0
  56. package/src/integration_tests/basic_bundle/require-context/mode-lazy-once.js +22 -0
  57. package/src/integration_tests/basic_bundle/require-context/mode-lazy-once.js.flow +24 -0
  58. package/src/integration_tests/basic_bundle/require-context/mode-lazy.js +22 -0
  59. package/src/integration_tests/basic_bundle/require-context/mode-lazy.js.flow +24 -0
  60. package/src/integration_tests/basic_bundle/require-context/mode-sync.js +20 -0
  61. package/src/integration_tests/basic_bundle/require-context/mode-sync.js.flow +22 -0
  62. package/src/integration_tests/basic_bundle/require-context/subdir/a.js +12 -0
  63. package/src/integration_tests/basic_bundle/require-context/subdir/a.js.flow +11 -0
  64. package/src/integration_tests/basic_bundle/require-context/subdir/b.js +18 -0
  65. package/src/integration_tests/basic_bundle/require-context/subdir/b.js.flow +11 -0
  66. package/src/integration_tests/basic_bundle/require-context/subdir/c.js +12 -0
  67. package/src/integration_tests/basic_bundle/require-context/subdir/c.js.flow +11 -0
  68. package/src/integration_tests/basic_bundle/require-context/subdir/nested/d.js +12 -0
  69. package/src/integration_tests/basic_bundle/require-context/subdir/nested/d.js.flow +11 -0
  70. package/src/integration_tests/basic_bundle/require-context/subdir-conflict/index.js +12 -0
  71. package/src/integration_tests/basic_bundle/require-context/subdir-conflict/index.js.flow +11 -0
  72. package/src/integration_tests/basic_bundle/require-context/utils.js +29 -0
  73. package/src/integration_tests/basic_bundle/require-context/utils.js.flow +44 -0
  74. package/src/lib/CountingSet.js +1 -0
  75. package/src/lib/CountingSet.js.flow +1 -0
  76. package/src/lib/RamBundleParser.js +1 -0
  77. package/src/lib/RamBundleParser.js.flow +1 -0
  78. package/src/lib/bundleToBytecode.js +3 -2
  79. package/src/lib/bundleToBytecode.js.flow +2 -2
  80. package/src/lib/contextModule.js +80 -0
  81. package/src/lib/contextModule.js.flow +86 -0
  82. package/src/lib/contextModuleTemplates.js +186 -0
  83. package/src/lib/contextModuleTemplates.js.flow +148 -0
  84. package/src/lib/getGraphId.js +17 -3
  85. package/src/lib/getGraphId.js.flow +12 -9
  86. package/src/lib/getPrependedScripts.js +15 -5
  87. package/src/lib/getPrependedScripts.js.flow +8 -1
  88. package/src/lib/parseCustomResolverOptions.js +26 -0
  89. package/src/lib/parseCustomResolverOptions.js.flow +38 -0
  90. package/src/lib/parseOptionsFromUrl.js +3 -0
  91. package/src/lib/parseOptionsFromUrl.js.flow +9 -18
  92. package/src/lib/splitBundleOptions.js +3 -0
  93. package/src/lib/splitBundleOptions.js.flow +3 -0
  94. package/src/lib/transformHelpers.js +68 -22
  95. package/src/lib/transformHelpers.js.flow +73 -16
  96. package/src/node-haste/DependencyGraph/ModuleResolution.js +19 -2
  97. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +8 -2
  98. package/src/node-haste/DependencyGraph/createHasteMap.js +7 -1
  99. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +7 -1
  100. package/src/node-haste/DependencyGraph.js +39 -9
  101. package/src/node-haste/DependencyGraph.js.flow +63 -12
  102. package/src/node-haste/Module.js +1 -0
  103. package/src/node-haste/Module.js.flow +1 -0
  104. package/src/shared/output/bundle.flow.js +67 -0
  105. package/src/shared/output/bundle.flow.js.flow +89 -0
  106. package/src/shared/output/bundle.js +8 -55
  107. package/src/shared/output/bundle.js.flow +8 -75
  108. package/src/shared/types.flow.js.flow +7 -0
@@ -26,6 +26,7 @@ import type {Reporter} from './lib/reporting';
26
26
  import type {
27
27
  BundleOptions,
28
28
  GraphOptions,
29
+ ResolverInputOptions,
29
30
  SplitBundleOptions,
30
31
  } from './shared/types.flow';
31
32
  import type {IncomingMessage, ServerResponse} from 'http';
@@ -36,6 +37,8 @@ import type {
36
37
  ActionStartLogEntry,
37
38
  LogEntry,
38
39
  } from 'metro-core/src/Logger';
40
+ import type {CustomResolverOptions} from 'metro-resolver/src/types';
41
+ import type {CustomTransformOptions} from 'metro-transform-worker';
39
42
 
40
43
  const {getAsset} = require('./Assets');
41
44
  const baseBytecodeBundle = require('./DeltaBundler/Serializers/baseBytecodeBundle');
@@ -66,7 +69,7 @@ const {
66
69
  Logger,
67
70
  Logger: {createActionStartEntry, createActionEndEntry, log},
68
71
  } = require('metro-core');
69
- const {VERSION: BYTECODE_VERSION} = require('metro-hermes-compiler');
72
+
70
73
  const mime = require('mime-types');
71
74
  const nullthrows = require('nullthrows');
72
75
  const path = require('path');
@@ -115,6 +118,10 @@ export type ServerOptions = $ReadOnly<{
115
118
  const DELTA_ID_HEADER = 'X-Metro-Delta-ID';
116
119
  const FILES_CHANGED_COUNT_HEADER = 'X-Metro-Files-Changed-Count';
117
120
 
121
+ function getBytecodeVersion() {
122
+ return require('metro-hermes-compiler').VERSION;
123
+ }
124
+
118
125
  class Server {
119
126
  _bundler: IncrementalBundler;
120
127
  _config: ConfigT;
@@ -178,6 +185,7 @@ class Server {
178
185
  entryFile,
179
186
  graphOptions,
180
187
  onProgress,
188
+ resolverOptions,
181
189
  serializerOptions,
182
190
  transformOptions,
183
191
  } = splitBundleOptions(options);
@@ -185,6 +193,7 @@ class Server {
185
193
  const {prepend, graph} = await this._bundler.buildGraph(
186
194
  entryFile,
187
195
  transformOptions,
196
+ resolverOptions,
188
197
  {
189
198
  onProgress,
190
199
  shallow: graphOptions.shallow,
@@ -196,7 +205,11 @@ class Server {
196
205
  const bundleOptions = {
197
206
  asyncRequireModulePath: await this._resolveRelativePath(
198
207
  this._config.transformer.asyncRequireModulePath,
199
- {transformOptions, relativeTo: 'project'},
208
+ {
209
+ relativeTo: 'project',
210
+ resolverOptions,
211
+ transformOptions,
212
+ },
200
213
  ),
201
214
  processModuleFilter: this._config.serializer.processModuleFilter,
202
215
  createModuleId: this._createModuleId,
@@ -255,6 +268,7 @@ class Server {
255
268
  entryFile,
256
269
  graphOptions,
257
270
  onProgress,
271
+ resolverOptions,
258
272
  serializerOptions,
259
273
  transformOptions,
260
274
  } = splitBundleOptions(options);
@@ -262,6 +276,7 @@ class Server {
262
276
  const {prepend, graph} = await this._bundler.buildGraph(
263
277
  entryFile,
264
278
  transformOptions,
279
+ resolverOptions,
265
280
  {onProgress, shallow: graphOptions.shallow},
266
281
  );
267
282
 
@@ -270,7 +285,11 @@ class Server {
270
285
  return await getRamBundleInfo(entryPoint, prepend, graph, {
271
286
  asyncRequireModulePath: await this._resolveRelativePath(
272
287
  this._config.transformer.asyncRequireModulePath,
273
- {transformOptions, relativeTo: 'project'},
288
+ {
289
+ relativeTo: 'project',
290
+ resolverOptions,
291
+ transformOptions,
292
+ },
274
293
  ),
275
294
  processModuleFilter: this._config.serializer.processModuleFilter,
276
295
  createModuleId: this._createModuleId,
@@ -295,12 +314,13 @@ class Server {
295
314
  }
296
315
 
297
316
  async getAssets(options: BundleOptions): Promise<$ReadOnlyArray<AssetData>> {
298
- const {entryFile, transformOptions, onProgress} =
317
+ const {entryFile, onProgress, resolverOptions, transformOptions} =
299
318
  splitBundleOptions(options);
300
319
 
301
320
  const dependencies = await this._bundler.getDependencies(
302
321
  [entryFile],
303
322
  transformOptions,
323
+ resolverOptions,
304
324
  {onProgress, shallow: false},
305
325
  );
306
326
 
@@ -320,10 +340,15 @@ class Server {
320
340
  +platform: string,
321
341
  ...
322
342
  }): Promise<Array<string>> {
323
- /* $FlowFixMe(>=0.122.0 site=react_native_fb) This comment suppresses an
324
- * error found when Flow v0.122.0 was deployed. To see the error, delete
325
- * this comment and run Flow. */
326
- const {entryFile, transformOptions, onProgress} = splitBundleOptions({
343
+ const {
344
+ entryFile,
345
+ onProgress,
346
+ resolverOptions,
347
+ transformOptions,
348
+ /* $FlowFixMe(>=0.122.0 site=react_native_fb) This comment suppresses an
349
+ * error found when Flow v0.122.0 was deployed. To see the error, delete
350
+ * this comment and run Flow. */
351
+ } = splitBundleOptions({
327
352
  ...Server.DEFAULT_BUNDLE_OPTIONS,
328
353
  ...options,
329
354
  bundleType: 'bundle',
@@ -332,6 +357,7 @@ class Server {
332
357
  const {prepend, graph} = await this._bundler.buildGraph(
333
358
  entryFile,
334
359
  transformOptions,
360
+ resolverOptions,
335
361
  {onProgress, shallow: false},
336
362
  );
337
363
 
@@ -351,7 +377,7 @@ class Server {
351
377
  res: ServerResponse,
352
378
  data: string | Buffer,
353
379
  assetPath: string,
354
- ) {
380
+ ): Buffer | string {
355
381
  if (req.headers && req.headers.range) {
356
382
  const [rangeStart, rangeEnd] = req.headers.range
357
383
  .replace(/bytes=/, '')
@@ -373,7 +399,10 @@ class Server {
373
399
  return data;
374
400
  }
375
401
 
376
- async _processSingleAssetRequest(req: IncomingMessage, res: ServerResponse) {
402
+ async _processSingleAssetRequest(
403
+ req: IncomingMessage,
404
+ res: ServerResponse,
405
+ ): Promise<void> {
377
406
  const urlObj = url.parse(decodeURI(req.url), true);
378
407
  let [, assetPath] =
379
408
  (urlObj &&
@@ -442,7 +471,7 @@ class Server {
442
471
  return parseOptionsFromUrl(
443
472
  url,
444
473
  new Set(this._config.resolver.platforms),
445
- BYTECODE_VERSION,
474
+ getBytecodeVersion(),
446
475
  );
447
476
  }
448
477
 
@@ -514,27 +543,41 @@ class Server {
514
543
  +build: (context: ProcessStartContext) => Promise<T>,
515
544
  +delete?: (context: ProcessDeleteContext) => Promise<void>,
516
545
  +finish: (context: ProcessEndContext<T>) => void,
517
- }) {
546
+ }): (
547
+ req: IncomingMessage,
548
+ res: ServerResponse,
549
+ bundleOptions: BundleOptions,
550
+ ) => Promise<void> {
518
551
  return async function requestProcessor(
552
+ this: Server,
519
553
  req: IncomingMessage,
520
554
  res: ServerResponse,
521
555
  bundleOptions: BundleOptions,
522
556
  ): Promise<void> {
523
- const {entryFile, graphOptions, transformOptions, serializerOptions} =
524
- splitBundleOptions(bundleOptions);
557
+ const {
558
+ entryFile,
559
+ graphOptions,
560
+ resolverOptions,
561
+ serializerOptions,
562
+ transformOptions,
563
+ } = splitBundleOptions(bundleOptions);
525
564
 
526
565
  /**
527
566
  * `entryFile` is relative to projectRoot, we need to use resolution function
528
567
  * to find the appropriate file with supported extensions.
529
568
  */
530
569
  const resolvedEntryFilePath = await this._resolveRelativePath(entryFile, {
531
- transformOptions,
532
570
  relativeTo: 'server',
571
+ resolverOptions,
572
+ transformOptions,
533
573
  });
534
574
  const graphId = getGraphId(resolvedEntryFilePath, transformOptions, {
535
- shallow: graphOptions.shallow,
536
575
  experimentalImportBundleSupport:
537
576
  this._config.transformer.experimentalImportBundleSupport,
577
+ unstable_allowRequireContext:
578
+ this._config.transformer.unstable_allowRequireContext,
579
+ resolverOptions,
580
+ shallow: graphOptions.shallow,
538
581
  });
539
582
 
540
583
  // For resources that support deletion, handle the DELETE method.
@@ -628,6 +671,7 @@ class Server {
628
671
  mres,
629
672
  onProgress,
630
673
  req,
674
+ resolverOptions,
631
675
  serializerOptions,
632
676
  transformOptions,
633
677
  };
@@ -692,7 +736,11 @@ class Server {
692
736
  };
693
737
  }
694
738
 
695
- _processBundleRequest = this._createRequestProcessor({
739
+ _processBundleRequest: (
740
+ req: IncomingMessage,
741
+ res: ServerResponse,
742
+ bundleOptions: BundleOptions,
743
+ ) => Promise<void> = this._createRequestProcessor({
696
744
  createStartEntry(context: ProcessStartContext) {
697
745
  return {
698
746
  action_name: 'Requesting bundle',
@@ -721,6 +769,7 @@ class Server {
721
769
  graphId,
722
770
  graphOptions,
723
771
  onProgress,
772
+ resolverOptions,
724
773
  serializerOptions,
725
774
  transformOptions,
726
775
  }) => {
@@ -728,10 +777,15 @@ class Server {
728
777
 
729
778
  const {delta, revision} = await (revPromise != null
730
779
  ? this._bundler.updateGraph(await revPromise, false)
731
- : this._bundler.initializeGraph(entryFile, transformOptions, {
732
- onProgress,
733
- shallow: graphOptions.shallow,
734
- }));
780
+ : this._bundler.initializeGraph(
781
+ entryFile,
782
+ transformOptions,
783
+ resolverOptions,
784
+ {
785
+ onProgress,
786
+ shallow: graphOptions.shallow,
787
+ },
788
+ ));
735
789
 
736
790
  const serializer =
737
791
  this._config.serializer.customSerializer ||
@@ -746,7 +800,11 @@ class Server {
746
800
  {
747
801
  asyncRequireModulePath: await this._resolveRelativePath(
748
802
  this._config.transformer.asyncRequireModulePath,
749
- {transformOptions, relativeTo: 'project'},
803
+ {
804
+ relativeTo: 'project',
805
+ resolverOptions,
806
+ transformOptions,
807
+ },
750
808
  ),
751
809
  processModuleFilter: this._config.serializer.processModuleFilter,
752
810
  createModuleId: this._createModuleId,
@@ -811,7 +869,11 @@ class Server {
811
869
  },
812
870
  });
813
871
 
814
- _processBytecodeBundleRequest = this._createRequestProcessor({
872
+ _processBytecodeBundleRequest: (
873
+ req: IncomingMessage,
874
+ res: ServerResponse,
875
+ bundleOptions: BundleOptions,
876
+ ) => Promise<void> = this._createRequestProcessor({
815
877
  createStartEntry(context: ProcessStartContext) {
816
878
  return {
817
879
  action_name: 'Requesting bundle',
@@ -840,6 +902,7 @@ class Server {
840
902
  graphId,
841
903
  graphOptions,
842
904
  onProgress,
905
+ resolverOptions,
843
906
  serializerOptions,
844
907
  transformOptions,
845
908
  }) => {
@@ -847,16 +910,25 @@ class Server {
847
910
 
848
911
  const {delta, revision} = await (revPromise != null
849
912
  ? this._bundler.updateGraph(await revPromise, false)
850
- : this._bundler.initializeGraph(entryFile, transformOptions, {
851
- onProgress,
852
- shallow: graphOptions.shallow,
853
- }));
913
+ : this._bundler.initializeGraph(
914
+ entryFile,
915
+ transformOptions,
916
+ resolverOptions,
917
+ {
918
+ onProgress,
919
+ shallow: graphOptions.shallow,
920
+ },
921
+ ));
854
922
 
855
923
  const bundle = bundleToBytecode(
856
924
  baseBytecodeBundle(entryFile, revision.prepend, revision.graph, {
857
925
  asyncRequireModulePath: await this._resolveRelativePath(
858
926
  this._config.transformer.asyncRequireModulePath,
859
- {transformOptions, relativeTo: 'project'},
927
+ {
928
+ relativeTo: 'project',
929
+ resolverOptions,
930
+ transformOptions,
931
+ },
860
932
  ),
861
933
  processModuleFilter: this._config.serializer.processModuleFilter,
862
934
  createModuleId: this._createModuleId,
@@ -929,7 +1001,11 @@ class Server {
929
1001
  );
930
1002
  }
931
1003
 
932
- _processSourceMapRequest = this._createRequestProcessor({
1004
+ _processSourceMapRequest: (
1005
+ req: IncomingMessage,
1006
+ res: ServerResponse,
1007
+ bundleOptions: BundleOptions,
1008
+ ) => Promise<void> = this._createRequestProcessor({
933
1009
  createStartEntry(context: ProcessStartContext) {
934
1010
  return {
935
1011
  action_name: 'Requesting sourcemap',
@@ -948,6 +1024,7 @@ class Server {
948
1024
  graphId,
949
1025
  graphOptions,
950
1026
  onProgress,
1027
+ resolverOptions,
951
1028
  serializerOptions,
952
1029
  transformOptions,
953
1030
  }) => {
@@ -957,6 +1034,7 @@ class Server {
957
1034
  ({revision} = await this._bundler.initializeGraph(
958
1035
  entryFile,
959
1036
  transformOptions,
1037
+ resolverOptions,
960
1038
  {onProgress, shallow: graphOptions.shallow},
961
1039
  ));
962
1040
  } else {
@@ -979,7 +1057,11 @@ class Server {
979
1057
  },
980
1058
  });
981
1059
 
982
- _processAssetsRequest = this._createRequestProcessor({
1060
+ _processAssetsRequest: (
1061
+ req: IncomingMessage,
1062
+ res: ServerResponse,
1063
+ bundleOptions: BundleOptions,
1064
+ ) => Promise<void> = this._createRequestProcessor({
983
1065
  createStartEntry(context: ProcessStartContext) {
984
1066
  return {
985
1067
  action_name: 'Requesting assets',
@@ -993,10 +1075,16 @@ class Server {
993
1075
  bundler: 'delta',
994
1076
  };
995
1077
  },
996
- build: async ({entryFile, transformOptions, onProgress}) => {
1078
+ build: async ({
1079
+ entryFile,
1080
+ onProgress,
1081
+ resolverOptions,
1082
+ transformOptions,
1083
+ }) => {
997
1084
  const dependencies = await this._bundler.getDependencies(
998
1085
  [entryFile],
999
1086
  transformOptions,
1087
+ resolverOptions,
1000
1088
  {onProgress, shallow: false},
1001
1089
  );
1002
1090
 
@@ -1117,15 +1205,16 @@ class Server {
1117
1205
  const options = parseOptionsFromUrl(
1118
1206
  reqUrl,
1119
1207
  new Set(this._config.resolver.platforms),
1120
- BYTECODE_VERSION,
1208
+ getBytecodeVersion(),
1121
1209
  );
1122
1210
 
1123
1211
  const {
1124
1212
  entryFile,
1125
- transformOptions,
1126
- serializerOptions,
1127
1213
  graphOptions,
1128
1214
  onProgress,
1215
+ resolverOptions,
1216
+ serializerOptions,
1217
+ transformOptions,
1129
1218
  } = splitBundleOptions(options);
1130
1219
 
1131
1220
  /**
@@ -1133,14 +1222,18 @@ class Server {
1133
1222
  * to find the appropriate file with supported extensions.
1134
1223
  */
1135
1224
  const resolvedEntryFilePath = await this._resolveRelativePath(entryFile, {
1136
- transformOptions,
1137
1225
  relativeTo: 'server',
1226
+ resolverOptions,
1227
+ transformOptions,
1138
1228
  });
1139
1229
 
1140
1230
  const graphId = getGraphId(resolvedEntryFilePath, transformOptions, {
1141
- shallow: graphOptions.shallow,
1142
1231
  experimentalImportBundleSupport:
1143
1232
  this._config.transformer.experimentalImportBundleSupport,
1233
+ unstable_allowRequireContext:
1234
+ this._config.transformer.unstable_allowRequireContext,
1235
+ resolverOptions,
1236
+ shallow: graphOptions.shallow,
1144
1237
  });
1145
1238
  let revision;
1146
1239
  const revPromise = this._bundler.getRevisionByGraphId(graphId);
@@ -1148,6 +1241,7 @@ class Server {
1148
1241
  ({revision} = await this._bundler.initializeGraph(
1149
1242
  resolvedEntryFilePath,
1150
1243
  transformOptions,
1244
+ resolverOptions,
1151
1245
  {onProgress, shallow: graphOptions.shallow},
1152
1246
  ));
1153
1247
  } else {
@@ -1170,16 +1264,19 @@ class Server {
1170
1264
  async _resolveRelativePath(
1171
1265
  filePath: string,
1172
1266
  {
1173
- transformOptions,
1174
1267
  relativeTo,
1268
+ resolverOptions,
1269
+ transformOptions,
1175
1270
  }: $ReadOnly<{
1176
- transformOptions: TransformInputOptions,
1177
1271
  relativeTo: 'project' | 'server',
1272
+ resolverOptions: ResolverInputOptions,
1273
+ transformOptions: TransformInputOptions,
1178
1274
  }>,
1179
- ) {
1275
+ ): Promise<string> {
1180
1276
  const resolutionFn = await transformHelpers.getResolveDependencyFn(
1181
1277
  this._bundler.getBundler(),
1182
1278
  transformOptions.platform,
1279
+ resolverOptions,
1183
1280
  );
1184
1281
  const rootDir =
1185
1282
  relativeTo === 'server'
@@ -1200,14 +1297,16 @@ class Server {
1200
1297
  return this._config.watchFolders;
1201
1298
  }
1202
1299
 
1203
- static DEFAULT_GRAPH_OPTIONS: {
1204
- customTransformOptions: any,
1300
+ static DEFAULT_GRAPH_OPTIONS: $ReadOnly<{
1301
+ customResolverOptions: CustomResolverOptions,
1302
+ customTransformOptions: CustomTransformOptions,
1205
1303
  dev: boolean,
1206
1304
  hot: boolean,
1207
1305
  minify: boolean,
1208
1306
  runtimeBytecodeVersion: ?number,
1209
1307
  unstable_transformProfile: 'default',
1210
- } = {
1308
+ }> = {
1309
+ customResolverOptions: Object.create(null),
1211
1310
  customTransformOptions: Object.create(null),
1212
1311
  dev: true,
1213
1312
  hot: false,
@@ -1238,11 +1337,11 @@ class Server {
1238
1337
  sourceUrl: null,
1239
1338
  };
1240
1339
 
1241
- _getServerRootDir() {
1340
+ _getServerRootDir(): string {
1242
1341
  return this._config.server.unstable_serverRoot ?? this._config.projectRoot;
1243
1342
  }
1244
1343
 
1245
- _getEntryPointAbsolutePath(entryFile: string) {
1344
+ _getEntryPointAbsolutePath(entryFile: string): string {
1246
1345
  return path.resolve(this._getServerRootDir(), entryFile);
1247
1346
  }
1248
1347
 
@@ -27,7 +27,7 @@ exports.watchFile = async function (
27
27
 
28
28
  exports.makeAsyncCommand =
29
29
  (
30
- command: (argv: YargArguments) => Promise<mixed>,
30
+ command: (argv: YargArguments) => Promise<void>,
31
31
  ): ((argv: YargArguments) => void) =>
32
32
  (argv: YargArguments) => {
33
33
  Promise.resolve(command(argv)).catch(error => {
@@ -22,8 +22,7 @@ const updateReporter = new TerminalReporter(term);
22
22
 
23
23
  module.exports = () => ({
24
24
  command: "build <entry>",
25
- description:
26
- "Generates a JavaScript bundle containing the specified entrypoint and its descendants",
25
+ desc: "Generates a JavaScript bundle containing the specified entrypoint and its descendants",
27
26
  builder: (yargs) => {
28
27
  yargs.option("project-roots", {
29
28
  alias: "P",
@@ -13,6 +13,7 @@
13
13
  import type {RunBuildOptions} from '../index';
14
14
  import type {YargArguments} from 'metro-config/src/configTypes.flow';
15
15
  import typeof Yargs from 'yargs';
16
+ import type {ModuleObject} from 'yargs';
16
17
 
17
18
  const {makeAsyncCommand} = require('../cli-utils');
18
19
  const TerminalReporter = require('../lib/TerminalReporter');
@@ -22,16 +23,12 @@ const {Terminal} = require('metro-core');
22
23
  const term = new Terminal(process.stdout);
23
24
  const updateReporter = new TerminalReporter(term);
24
25
 
25
- module.exports = (): ({
26
- builder: (yargs: Yargs) => void,
27
- command: string,
28
- description: string,
29
- handler: (argv: YargArguments) => void,
30
- }) => ({
26
+ module.exports = (): {
27
+ ...ModuleObject,
28
+ handler: Function,
29
+ } => ({
31
30
  command: 'build <entry>',
32
-
33
- description:
34
- 'Generates a JavaScript bundle containing the specified entrypoint and its descendants',
31
+ desc: 'Generates a JavaScript bundle containing the specified entrypoint and its descendants',
35
32
 
36
33
  builder: (yargs: Yargs): void => {
37
34
  yargs.option('project-roots', {
@@ -73,7 +73,7 @@ async function dependencies(args, config) {
73
73
 
74
74
  module.exports = () => ({
75
75
  command: "get-dependencies",
76
- description: "List dependencies",
76
+ desc: "List dependencies",
77
77
  builder: (yargs) => {
78
78
  yargs.option("entry-file", {
79
79
  type: "string",
@@ -17,7 +17,8 @@ const { promisify } = require("util");
17
17
 
18
18
  module.exports = () => ({
19
19
  command: "serve",
20
- description: "Starts Metro on the given port, building bundles on the fly",
20
+ aliases: ["start"],
21
+ desc: "Starts Metro on the given port, building bundles on the fly",
21
22
  builder: (yargs) => {
22
23
  yargs.option("project-roots", {
23
24
  alias: "P",
@@ -13,20 +13,19 @@
13
13
  import type {RunServerOptions} from '../index';
14
14
  import type {YargArguments} from 'metro-config/src/configTypes.flow';
15
15
  import typeof Yargs from 'yargs';
16
+ import type {ModuleObject} from 'yargs';
16
17
 
17
18
  const {makeAsyncCommand, watchFile} = require('../cli-utils');
18
19
  const {loadConfig, resolveConfig} = require('metro-config');
19
20
  const {promisify} = require('util');
20
21
 
21
- module.exports = (): ({
22
- builder: (yargs: Yargs) => void,
23
- command: $TEMPORARY$string<'serve'>,
24
- description: string,
25
- handler: (argv: YargArguments) => void,
26
- }) => ({
22
+ module.exports = (): {
23
+ ...ModuleObject,
24
+ handler: Function,
25
+ } => ({
27
26
  command: 'serve',
28
-
29
- description: 'Starts Metro on the given port, building bundles on the fly',
27
+ aliases: ['start'],
28
+ desc: 'Starts Metro on the given port, building bundles on the fly',
30
29
 
31
30
  builder: (yargs: Yargs): void => {
32
31
  yargs.option('project-roots', {
package/src/index.flow.js CHANGED
@@ -340,14 +340,22 @@ exports.buildGraph = async function (
340
340
  const bundler = new IncrementalBundler(mergedConfig);
341
341
 
342
342
  try {
343
- return await bundler.buildGraphForEntries(entries, {
344
- ...MetroServer.DEFAULT_GRAPH_OPTIONS,
345
- customTransformOptions,
346
- dev,
347
- minify,
348
- platform,
349
- type,
350
- });
343
+ const { customResolverOptions, ...defaultTransformInputOptions } =
344
+ MetroServer.DEFAULT_GRAPH_OPTIONS;
345
+ return await bundler.buildGraphForEntries(
346
+ entries,
347
+ {
348
+ ...defaultTransformInputOptions,
349
+ customTransformOptions,
350
+ dev,
351
+ minify,
352
+ platform,
353
+ type,
354
+ },
355
+ {
356
+ customResolverOptions,
357
+ }
358
+ );
351
359
  } finally {
352
360
  bundler.end();
353
361
  }
@@ -355,22 +363,25 @@ exports.buildGraph = async function (
355
363
 
356
364
  exports.attachMetroCli = function (
357
365
  yargs,
358
- { build = {}, serve = {}, dependencies = {} } = {}
366
+ // $FlowFixMe[prop-missing]
367
+ {
368
+ build = {},
369
+ serve = {},
370
+ dependencies = {}
371
+ } = {} // prettier-ignore
359
372
  ) {
373
+ yargs.strict();
374
+
360
375
  if (build) {
361
- const { command, description, builder, handler } = makeBuildCommand();
362
- yargs.command(command, description, builder, handler);
376
+ yargs.command(makeBuildCommand());
363
377
  }
364
378
 
365
379
  if (serve) {
366
- const { command, description, builder, handler } = makeServeCommand();
367
- yargs.command(command, description, builder, handler);
380
+ yargs.command(makeServeCommand());
368
381
  }
369
382
 
370
383
  if (dependencies) {
371
- const { command, description, builder, handler } =
372
- makeDependenciesCommand();
373
- yargs.command(command, description, builder, handler);
384
+ yargs.command(makeDependenciesCommand());
374
385
  }
375
386
 
376
387
  return yargs;