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
@@ -11,6 +11,8 @@ var _metroFileMap = require("metro-file-map");
11
11
  *
12
12
  * @format
13
13
  */
14
+ const canonicalize = require("metro-core/src/canonicalize");
15
+
14
16
  const createHasteMap = require("./DependencyGraph/createHasteMap");
15
17
 
16
18
  const { ModuleResolver } = require("./DependencyGraph/ModuleResolution");
@@ -34,8 +36,9 @@ const nullthrows = require("nullthrows");
34
36
  const path = require("path");
35
37
 
36
38
  const { DuplicateHasteCandidatesError } = _metroFileMap.ModuleMap;
39
+ const NULL_PLATFORM = Symbol();
37
40
 
38
- function getOrCreate(map, field) {
41
+ function getOrCreateMap(map, field) {
39
42
  let subMap = map.get(field);
40
43
 
41
44
  if (!subMap) {
@@ -208,27 +211,53 @@ class DependencyGraph extends EventEmitter {
208
211
  end() {
209
212
  this._haste.end();
210
213
  }
214
+ /** Given a search context, return a list of file paths matching the query. */
215
+
216
+ matchFilesWithContext(from, context) {
217
+ return this._hasteFS.matchFilesWithContext(from, context);
218
+ }
211
219
 
212
220
  resolveDependency(
213
221
  from,
214
222
  to,
215
223
  platform,
224
+ resolverOptions, // TODO: Fold assumeFlatNodeModules into resolverOptions and add to graphId
216
225
  { assumeFlatNodeModules } = {
217
226
  assumeFlatNodeModules: false,
218
227
  }
219
228
  ) {
229
+ var _JSON$stringify, _resolverOptions$cust;
230
+
220
231
  const isSensitiveToOriginFolder = // Resolution is always relative to the origin folder unless we assume a flat node_modules
221
232
  !assumeFlatNodeModules || // Path requests are resolved relative to the origin folder
222
233
  to.includes("/") ||
223
234
  to === "." ||
224
235
  to === ".." || // Preserve standard assumptions under node_modules
225
- from.includes(path.sep + "node_modules" + path.sep);
226
- const mapByDirectory = getOrCreate(
227
- this._resolutionCache,
228
- isSensitiveToOriginFolder ? path.dirname(from) : ""
236
+ from.includes(path.sep + "node_modules" + path.sep); // Compound key for the resolver cache
237
+
238
+ const resolverOptionsKey =
239
+ (_JSON$stringify = JSON.stringify(
240
+ (_resolverOptions$cust = resolverOptions.customResolverOptions) !==
241
+ null && _resolverOptions$cust !== void 0
242
+ ? _resolverOptions$cust
243
+ : {},
244
+ canonicalize
245
+ )) !== null && _JSON$stringify !== void 0
246
+ ? _JSON$stringify
247
+ : "";
248
+ const originKey = isSensitiveToOriginFolder ? path.dirname(from) : "";
249
+ const targetKey = to;
250
+ const platformKey =
251
+ platform !== null && platform !== void 0 ? platform : NULL_PLATFORM; // Traverse the resolver cache, which is a tree of maps
252
+
253
+ const mapByResolverOptions = this._resolutionCache;
254
+ const mapByOrigin = getOrCreateMap(
255
+ mapByResolverOptions,
256
+ resolverOptionsKey
229
257
  );
230
- const mapByPlatform = getOrCreate(mapByDirectory, to);
231
- let modulePath = mapByPlatform.get(platform);
258
+ const mapByTarget = getOrCreateMap(mapByOrigin, originKey);
259
+ const mapByPlatform = getOrCreateMap(mapByTarget, targetKey);
260
+ let modulePath = mapByPlatform.get(platformKey);
232
261
 
233
262
  if (!modulePath) {
234
263
  try {
@@ -236,7 +265,8 @@ class DependencyGraph extends EventEmitter {
236
265
  this._moduleCache.getModule(from),
237
266
  to,
238
267
  true,
239
- platform
268
+ platform,
269
+ resolverOptions
240
270
  ).path;
241
271
  } catch (error) {
242
272
  if (error instanceof DuplicateHasteCandidatesError) {
@@ -255,7 +285,7 @@ class DependencyGraph extends EventEmitter {
255
285
  }
256
286
  }
257
287
 
258
- mapByPlatform.set(platform, modulePath);
288
+ mapByPlatform.set(platformKey, modulePath);
259
289
  return modulePath;
260
290
  }
261
291
 
@@ -15,6 +15,7 @@ import type Module from './Module';
15
15
 
16
16
  import {ModuleMap as MetroFileMapModuleMap} from 'metro-file-map';
17
17
 
18
+ const canonicalize = require('metro-core/src/canonicalize');
18
19
  const createHasteMap = require('./DependencyGraph/createHasteMap');
19
20
  const {ModuleResolver} = require('./DependencyGraph/ModuleResolution');
20
21
  const ModuleCache = require('./ModuleCache');
@@ -28,13 +29,16 @@ const {
28
29
  const {InvalidPackageError} = require('metro-resolver');
29
30
  const nullthrows = require('nullthrows');
30
31
  const path = require('path');
32
+ import type {ResolverInputOptions} from '../shared/types.flow';
31
33
 
32
34
  const {DuplicateHasteCandidatesError} = MetroFileMapModuleMap;
33
35
 
34
- function getOrCreate<T>(
35
- map: Map<string, Map<string, T>>,
36
+ const NULL_PLATFORM = Symbol();
37
+
38
+ function getOrCreateMap<T>(
39
+ map: Map<string | symbol, Map<string | symbol, T>>,
36
40
  field: string,
37
- ): Map<string, T> {
41
+ ): Map<string | symbol, T> {
38
42
  let subMap = map.get(field);
39
43
  if (!subMap) {
40
44
  subMap = new Map();
@@ -51,7 +55,23 @@ class DependencyGraph extends EventEmitter {
51
55
  _moduleCache: ModuleCache;
52
56
  _moduleMap: MetroFileMapModuleMap;
53
57
  _moduleResolver: ModuleResolver<Module, Package>;
54
- _resolutionCache: Map<string, Map<string, Map<string, string>>>;
58
+ _resolutionCache: Map<
59
+ // Custom resolver options
60
+ string | symbol,
61
+ Map<
62
+ // Origin folder
63
+ string | symbol,
64
+ Map<
65
+ // Dependency name
66
+ string | symbol,
67
+ Map<
68
+ // Platform
69
+ string | symbol,
70
+ string,
71
+ >,
72
+ >,
73
+ >,
74
+ >;
55
75
  _readyPromise: Promise<void>;
56
76
 
57
77
  constructor(
@@ -179,7 +199,7 @@ class DependencyGraph extends EventEmitter {
179
199
  });
180
200
  }
181
201
 
182
- _createModuleCache() {
202
+ _createModuleCache(): ModuleCache {
183
203
  return new ModuleCache({
184
204
  // $FlowFixMe[method-unbinding] added when improving typing for this parameters
185
205
  getClosestPackage: this._getClosestPackage.bind(this),
@@ -225,10 +245,26 @@ class DependencyGraph extends EventEmitter {
225
245
  this._haste.end();
226
246
  }
227
247
 
248
+ /** Given a search context, return a list of file paths matching the query. */
249
+ matchFilesWithContext(
250
+ from: string,
251
+ context: $ReadOnly<{
252
+ /* Should search for files recursively. */
253
+ recursive: boolean,
254
+ /* Filter relative paths against a pattern. */
255
+ filter: RegExp,
256
+ }>,
257
+ ): string[] {
258
+ return this._hasteFS.matchFilesWithContext(from, context);
259
+ }
260
+
228
261
  resolveDependency(
229
262
  from: string,
230
263
  to: string,
231
- platform: string,
264
+ platform: string | null,
265
+ resolverOptions: ResolverInputOptions,
266
+
267
+ // TODO: Fold assumeFlatNodeModules into resolverOptions and add to graphId
232
268
  {assumeFlatNodeModules}: {assumeFlatNodeModules: boolean} = {
233
269
  assumeFlatNodeModules: false,
234
270
  },
@@ -242,12 +278,26 @@ class DependencyGraph extends EventEmitter {
242
278
  to === '..' ||
243
279
  // Preserve standard assumptions under node_modules
244
280
  from.includes(path.sep + 'node_modules' + path.sep);
245
- const mapByDirectory = getOrCreate(
246
- this._resolutionCache,
247
- isSensitiveToOriginFolder ? path.dirname(from) : '',
281
+
282
+ // Compound key for the resolver cache
283
+ const resolverOptionsKey =
284
+ JSON.stringify(
285
+ resolverOptions.customResolverOptions ?? {},
286
+ canonicalize,
287
+ ) ?? '';
288
+ const originKey = isSensitiveToOriginFolder ? path.dirname(from) : '';
289
+ const targetKey = to;
290
+ const platformKey = platform ?? NULL_PLATFORM;
291
+
292
+ // Traverse the resolver cache, which is a tree of maps
293
+ const mapByResolverOptions = this._resolutionCache;
294
+ const mapByOrigin = getOrCreateMap(
295
+ mapByResolverOptions,
296
+ resolverOptionsKey,
248
297
  );
249
- const mapByPlatform = getOrCreate(mapByDirectory, to);
250
- let modulePath = mapByPlatform.get(platform);
298
+ const mapByTarget = getOrCreateMap(mapByOrigin, originKey);
299
+ const mapByPlatform = getOrCreateMap(mapByTarget, targetKey);
300
+ let modulePath = mapByPlatform.get(platformKey);
251
301
 
252
302
  if (!modulePath) {
253
303
  try {
@@ -256,6 +306,7 @@ class DependencyGraph extends EventEmitter {
256
306
  to,
257
307
  true,
258
308
  platform,
309
+ resolverOptions,
259
310
  ).path;
260
311
  } catch (error) {
261
312
  if (error instanceof DuplicateHasteCandidatesError) {
@@ -272,7 +323,7 @@ class DependencyGraph extends EventEmitter {
272
323
  }
273
324
  }
274
325
 
275
- mapByPlatform.set(platform, modulePath);
326
+ mapByPlatform.set(platformKey, modulePath);
276
327
  return modulePath;
277
328
  }
278
329
 
@@ -12,6 +12,7 @@
12
12
  const isAbsolutePath = require("absolute-path");
13
13
 
14
14
  class Module {
15
+ // $FlowFixMe[missing-local-annot]
15
16
  constructor(file, moduleCache) {
16
17
  if (!isAbsolutePath(file)) {
17
18
  throw new Error("Expected file to be absolute path but got " + file);
@@ -21,6 +21,7 @@ class Module {
21
21
  _moduleCache: ModuleCache;
22
22
  _sourceCode: ?string;
23
23
 
24
+ // $FlowFixMe[missing-local-annot]
24
25
  constructor(file: string, moduleCache: ModuleCache) {
25
26
  if (!isAbsolutePath(file)) {
26
27
  throw new Error('Expected file to be absolute path but got ' + file);
@@ -0,0 +1,67 @@
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
+ *
8
+ * @format
9
+ */
10
+ "use strict";
11
+
12
+ const relativizeSourceMapInline = require("../../lib/relativizeSourceMap");
13
+
14
+ const Server = require("../../Server");
15
+
16
+ const writeFile = require("./writeFile");
17
+
18
+ function buildBundle(packagerClient, requestOptions) {
19
+ return packagerClient.build({
20
+ ...Server.DEFAULT_BUNDLE_OPTIONS,
21
+ ...requestOptions,
22
+ bundleType: "bundle",
23
+ });
24
+ }
25
+
26
+ function relativateSerializedMap(map, sourceMapSourcesRoot) {
27
+ const sourceMap = JSON.parse(map);
28
+ relativizeSourceMapInline(sourceMap, sourceMapSourcesRoot);
29
+ return JSON.stringify(sourceMap);
30
+ }
31
+
32
+ async function saveBundleAndMap(bundle, options, log) {
33
+ const {
34
+ bundleOutput,
35
+ bundleEncoding: encoding,
36
+ sourcemapOutput,
37
+ sourcemapSourcesRoot,
38
+ } = options;
39
+ const writeFns = [];
40
+ writeFns.push(async () => {
41
+ log("Writing bundle output to:", bundleOutput);
42
+ await writeFile(bundleOutput, bundle.code, encoding);
43
+ log("Done writing bundle output");
44
+ });
45
+
46
+ if (sourcemapOutput) {
47
+ let { map } = bundle;
48
+
49
+ if (sourcemapSourcesRoot !== undefined) {
50
+ log("start relativating source map");
51
+ map = relativateSerializedMap(map, sourcemapSourcesRoot);
52
+ log("finished relativating");
53
+ }
54
+
55
+ writeFns.push(async () => {
56
+ log("Writing sourcemap output to:", sourcemapOutput);
57
+ await writeFile(sourcemapOutput, map, null);
58
+ log("Done writing sourcemap output");
59
+ });
60
+ } // Wait until everything is written to disk.
61
+
62
+ await Promise.all(writeFns.map((cb) => cb()));
63
+ }
64
+
65
+ exports.build = buildBundle;
66
+ exports.save = saveBundleAndMap;
67
+ exports.formatName = "bundle";
@@ -0,0 +1,89 @@
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
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {OutputOptions, RequestOptions} from '../types.flow';
14
+ import type {MixedSourceMap} from 'metro-source-map';
15
+
16
+ const relativizeSourceMapInline = require('../../lib/relativizeSourceMap');
17
+ const Server = require('../../Server');
18
+ const writeFile = require('./writeFile');
19
+
20
+ function buildBundle(
21
+ packagerClient: Server,
22
+ requestOptions: RequestOptions,
23
+ ): Promise<{
24
+ code: string,
25
+ map: string,
26
+ ...
27
+ }> {
28
+ return packagerClient.build({
29
+ ...Server.DEFAULT_BUNDLE_OPTIONS,
30
+ ...requestOptions,
31
+ bundleType: 'bundle',
32
+ });
33
+ }
34
+
35
+ function relativateSerializedMap(
36
+ map: string,
37
+ sourceMapSourcesRoot: string,
38
+ ): string {
39
+ const sourceMap = (JSON.parse(map): MixedSourceMap);
40
+ relativizeSourceMapInline(sourceMap, sourceMapSourcesRoot);
41
+ return JSON.stringify(sourceMap);
42
+ }
43
+
44
+ async function saveBundleAndMap(
45
+ bundle: {
46
+ code: string,
47
+ map: string,
48
+ ...
49
+ },
50
+ options: OutputOptions,
51
+ log: (...args: Array<string>) => void,
52
+ ): Promise<mixed> {
53
+ const {
54
+ bundleOutput,
55
+ bundleEncoding: encoding,
56
+ sourcemapOutput,
57
+ sourcemapSourcesRoot,
58
+ } = options;
59
+
60
+ const writeFns = [];
61
+
62
+ writeFns.push(async () => {
63
+ log('Writing bundle output to:', bundleOutput);
64
+ await writeFile(bundleOutput, bundle.code, encoding);
65
+ log('Done writing bundle output');
66
+ });
67
+
68
+ if (sourcemapOutput) {
69
+ let {map} = bundle;
70
+ if (sourcemapSourcesRoot !== undefined) {
71
+ log('start relativating source map');
72
+ map = relativateSerializedMap(map, sourcemapSourcesRoot);
73
+ log('finished relativating');
74
+ }
75
+
76
+ writeFns.push(async () => {
77
+ log('Writing sourcemap output to:', sourcemapOutput);
78
+ await writeFile(sourcemapOutput, map, null);
79
+ log('Done writing sourcemap output');
80
+ });
81
+ }
82
+
83
+ // Wait until everything is written to disk.
84
+ await Promise.all(writeFns.map((cb: void => mixed) => cb()));
85
+ }
86
+
87
+ exports.build = buildBundle;
88
+ exports.save = saveBundleAndMap;
89
+ exports.formatName = 'bundle';
@@ -8,60 +8,13 @@
8
8
  * @format
9
9
  */
10
10
  "use strict";
11
+ /*::
12
+ export type * from './bundle.flow';
13
+ */
11
14
 
12
- const relativizeSourceMapInline = require("../../lib/relativizeSourceMap");
15
+ try {
16
+ // $FlowFixMe[untyped-import]
17
+ require("metro-babel-register").unstable_registerForMetroMonorepo();
18
+ } catch {}
13
19
 
14
- const Server = require("../../Server");
15
-
16
- const writeFile = require("./writeFile");
17
-
18
- function buildBundle(packagerClient, requestOptions) {
19
- return packagerClient.build({
20
- ...Server.DEFAULT_BUNDLE_OPTIONS,
21
- ...requestOptions,
22
- bundleType: "bundle",
23
- });
24
- }
25
-
26
- function relativateSerializedMap(map, sourceMapSourcesRoot) {
27
- const sourceMap = JSON.parse(map);
28
- relativizeSourceMapInline(sourceMap, sourceMapSourcesRoot);
29
- return JSON.stringify(sourceMap);
30
- }
31
-
32
- async function saveBundleAndMap(bundle, options, log) {
33
- const {
34
- bundleOutput,
35
- bundleEncoding: encoding,
36
- sourcemapOutput,
37
- sourcemapSourcesRoot,
38
- } = options;
39
- const writeFns = [];
40
- writeFns.push(async () => {
41
- log("Writing bundle output to:", bundleOutput);
42
- await writeFile(bundleOutput, bundle.code, encoding);
43
- log("Done writing bundle output");
44
- });
45
-
46
- if (sourcemapOutput) {
47
- let { map } = bundle;
48
-
49
- if (sourcemapSourcesRoot !== undefined) {
50
- log("start relativating source map");
51
- map = relativateSerializedMap(map, sourcemapSourcesRoot);
52
- log("finished relativating");
53
- }
54
-
55
- writeFns.push(async () => {
56
- log("Writing sourcemap output to:", sourcemapOutput);
57
- await writeFile(sourcemapOutput, map, null);
58
- log("Done writing sourcemap output");
59
- });
60
- } // Wait until everything is written to disk.
61
-
62
- await Promise.all(writeFns.map((cb) => cb()));
63
- }
64
-
65
- exports.build = buildBundle;
66
- exports.save = saveBundleAndMap;
67
- exports.formatName = "bundle";
20
+ module.exports = require("./bundle.flow");
@@ -10,80 +10,13 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {OutputOptions, RequestOptions} from '../types.flow';
14
- import type {MixedSourceMap} from 'metro-source-map';
13
+ /*::
14
+ export type * from './bundle.flow';
15
+ */
15
16
 
16
- const relativizeSourceMapInline = require('../../lib/relativizeSourceMap');
17
- const Server = require('../../Server');
18
- const writeFile = require('./writeFile');
17
+ try {
18
+ // $FlowFixMe[untyped-import]
19
+ require('metro-babel-register').unstable_registerForMetroMonorepo();
20
+ } catch {}
19
21
 
20
- function buildBundle(
21
- packagerClient: Server,
22
- requestOptions: RequestOptions,
23
- ): Promise<{
24
- code: string,
25
- map: string,
26
- ...
27
- }> {
28
- return packagerClient.build({
29
- ...Server.DEFAULT_BUNDLE_OPTIONS,
30
- ...requestOptions,
31
- bundleType: 'bundle',
32
- });
33
- }
34
-
35
- function relativateSerializedMap(
36
- map: string,
37
- sourceMapSourcesRoot: string,
38
- ): string {
39
- const sourceMap = (JSON.parse(map): MixedSourceMap);
40
- relativizeSourceMapInline(sourceMap, sourceMapSourcesRoot);
41
- return JSON.stringify(sourceMap);
42
- }
43
-
44
- async function saveBundleAndMap(
45
- bundle: {
46
- code: string,
47
- map: string,
48
- ...
49
- },
50
- options: OutputOptions,
51
- log: (...args: Array<string>) => void,
52
- ): Promise<mixed> {
53
- const {
54
- bundleOutput,
55
- bundleEncoding: encoding,
56
- sourcemapOutput,
57
- sourcemapSourcesRoot,
58
- } = options;
59
-
60
- const writeFns = [];
61
-
62
- writeFns.push(async () => {
63
- log('Writing bundle output to:', bundleOutput);
64
- await writeFile(bundleOutput, bundle.code, encoding);
65
- log('Done writing bundle output');
66
- });
67
-
68
- if (sourcemapOutput) {
69
- let {map} = bundle;
70
- if (sourcemapSourcesRoot !== undefined) {
71
- log('start relativating source map');
72
- map = relativateSerializedMap(map, sourcemapSourcesRoot);
73
- log('finished relativating');
74
- }
75
-
76
- writeFns.push(async () => {
77
- log('Writing sourcemap output to:', sourcemapOutput);
78
- await writeFile(sourcemapOutput, map, null);
79
- log('Done writing sourcemap output');
80
- });
81
- }
82
-
83
- // Wait until everything is written to disk.
84
- await Promise.all(writeFns.map((cb: void => mixed) => cb()));
85
- }
86
-
87
- exports.build = buildBundle;
88
- exports.save = saveBundleAndMap;
89
- exports.formatName = 'bundle';
22
+ module.exports = require('./bundle.flow');
@@ -23,6 +23,7 @@ import type {
23
23
  CustomTransformOptions,
24
24
  MinifierOptions,
25
25
  } from 'metro-transform-worker';
26
+ import type {CustomResolverOptions} from 'metro-resolver';
26
27
 
27
28
  type BundleType =
28
29
  | 'bundle'
@@ -41,6 +42,7 @@ type MetroSourceMapOrMappings =
41
42
 
42
43
  export type BundleOptions = {
43
44
  bundleType: BundleType,
45
+ +customResolverOptions: CustomResolverOptions,
44
46
  customTransformOptions: CustomTransformOptions,
45
47
  dev: boolean,
46
48
  entryFile: string,
@@ -61,6 +63,10 @@ export type BundleOptions = {
61
63
  ...
62
64
  };
63
65
 
66
+ export type ResolverInputOptions = $ReadOnly<{
67
+ customResolverOptions?: CustomResolverOptions,
68
+ }>;
69
+
64
70
  export type SerializerOptions = {
65
71
  +sourceMapUrl: ?string,
66
72
  +sourceUrl: ?string,
@@ -77,6 +83,7 @@ export type GraphOptions = {
77
83
  // Stricter representation of BundleOptions.
78
84
  export type SplitBundleOptions = {
79
85
  +entryFile: string,
86
+ +resolverOptions: ResolverInputOptions,
80
87
  +transformOptions: TransformInputOptions,
81
88
  +serializerOptions: SerializerOptions,
82
89
  +graphOptions: GraphOptions,