metro 0.70.3 → 0.71.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 (94) hide show
  1. package/package.json +23 -22
  2. package/src/Assets.js.flow +4 -4
  3. package/src/Bundler/util.js +1 -1
  4. package/src/Bundler/util.js.flow +2 -2
  5. package/src/Bundler.js +15 -10
  6. package/src/Bundler.js.flow +19 -14
  7. package/src/DeltaBundler/DeltaCalculator.js +13 -17
  8. package/src/DeltaBundler/DeltaCalculator.js.flow +15 -20
  9. package/src/DeltaBundler/Serializers/getAllFiles.js.flow +2 -2
  10. package/src/DeltaBundler/Serializers/getAssets.js.flow +2 -2
  11. package/src/DeltaBundler/Serializers/getExplodedSourceMap.js.flow +4 -4
  12. package/src/DeltaBundler/Serializers/getRamBundleInfo.js.flow +6 -6
  13. package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.js.flow +4 -4
  14. package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js.flow +2 -2
  15. package/src/DeltaBundler/Serializers/helpers/processModules.js.flow +2 -2
  16. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +2 -2
  17. package/src/DeltaBundler/Serializers/sourceMapGenerator.js.flow +6 -6
  18. package/src/DeltaBundler/Serializers/sourceMapObject.js.flow +4 -4
  19. package/src/DeltaBundler/Serializers/sourceMapString.js.flow +2 -2
  20. package/src/DeltaBundler/Worker.flow.js +78 -0
  21. package/src/DeltaBundler/Worker.flow.js.flow +121 -0
  22. package/src/DeltaBundler/Worker.js +8 -66
  23. package/src/DeltaBundler/Worker.js.flow +8 -107
  24. package/src/DeltaBundler/WorkerFarm.js.flow +4 -4
  25. package/src/DeltaBundler/__fixtures__/hasteImpl.js +4 -0
  26. package/src/DeltaBundler/getTransformCacheKey.js.flow +2 -2
  27. package/src/DeltaBundler/graphOperations.js +641 -0
  28. package/src/DeltaBundler/graphOperations.js.flow +752 -0
  29. package/src/DeltaBundler/types.flow.js +6 -0
  30. package/src/DeltaBundler/types.flow.js.flow +43 -31
  31. package/src/DeltaBundler.js +12 -6
  32. package/src/DeltaBundler.js.flow +14 -10
  33. package/src/HmrServer.js +0 -2
  34. package/src/HmrServer.js.flow +7 -8
  35. package/src/IncrementalBundler.js +1 -1
  36. package/src/IncrementalBundler.js.flow +8 -8
  37. package/src/ModuleGraph/node-haste/ModuleCache.js +1 -1
  38. package/src/ModuleGraph/node-haste/ModuleCache.js.flow +1 -1
  39. package/src/ModuleGraph/node-haste/node-haste.flow.js +0 -1
  40. package/src/ModuleGraph/node-haste/node-haste.flow.js.flow +3 -4
  41. package/src/ModuleGraph/node-haste/node-haste.js +4 -4
  42. package/src/ModuleGraph/node-haste/node-haste.js.flow +13 -7
  43. package/src/ModuleGraph/output/indexed-ram-bundle.js.flow +2 -2
  44. package/src/ModuleGraph/output/plain-bundle.js.flow +2 -2
  45. package/src/ModuleGraph/output/reverse-dependency-map-references.js.flow +8 -8
  46. package/src/ModuleGraph/output/util.js.flow +2 -2
  47. package/src/ModuleGraph/types.flow.js.flow +37 -37
  48. package/src/ModuleGraph/worker/collectDependencies.js +215 -8
  49. package/src/ModuleGraph/worker/collectDependencies.js.flow +230 -13
  50. package/src/Server/symbolicate.js.flow +1 -1
  51. package/src/Server.js.flow +18 -18
  52. package/src/cli.js +5 -0
  53. package/src/cli.js.flow +5 -0
  54. package/src/commands/build.js +4 -3
  55. package/src/commands/build.js.flow +5 -3
  56. package/src/commands/serve.js +3 -3
  57. package/src/commands/serve.js.flow +5 -3
  58. package/src/index.flow.js +392 -0
  59. package/src/index.flow.js.flow +480 -0
  60. package/src/index.js +8 -366
  61. package/src/index.js.flow +8 -456
  62. package/src/lib/CountingSet.js +116 -0
  63. package/src/lib/CountingSet.js.flow +126 -0
  64. package/src/lib/JsonReporter.js +0 -2
  65. package/src/lib/JsonReporter.js.flow +1 -1
  66. package/src/lib/bundleToBytecode.js.flow +2 -2
  67. package/src/lib/bundleToString.js.flow +2 -2
  68. package/src/lib/getAppendScripts.js +10 -4
  69. package/src/lib/getAppendScripts.js.flow +6 -4
  70. package/src/lib/getPreludeCode.js +19 -1
  71. package/src/lib/getPreludeCode.js.flow +17 -2
  72. package/src/lib/getPrependedScripts.js +10 -2
  73. package/src/lib/getPrependedScripts.js.flow +11 -2
  74. package/src/lib/reporting.js +0 -2
  75. package/src/lib/reporting.js.flow +2 -1
  76. package/src/lib/transformHelpers.js.flow +2 -2
  77. package/src/node-haste/DependencyGraph/ModuleResolution.js +17 -4
  78. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +20 -12
  79. package/src/node-haste/DependencyGraph/createHasteMap.js +80 -19
  80. package/src/node-haste/DependencyGraph/createHasteMap.js.flow +16 -14
  81. package/src/node-haste/DependencyGraph.js +31 -29
  82. package/src/node-haste/DependencyGraph.js.flow +44 -38
  83. package/src/node-haste/ModuleCache.js.flow +1 -1
  84. package/src/node-haste/lib/AssetPaths.js.flow +2 -2
  85. package/src/node-haste/lib/parsePlatformFilePath.js.flow +2 -2
  86. package/src/shared/output/RamBundle/as-indexed-file.js.flow +1 -1
  87. package/src/shared/output/RamBundle/buildSourcemapWithMetadata.js.flow +2 -2
  88. package/src/shared/types.flow.js.flow +14 -14
  89. package/src/DeltaBundler/computeDelta.js +0 -42
  90. package/src/DeltaBundler/computeDelta.js.flow +0 -47
  91. package/src/DeltaBundler/traverseDependencies.js +0 -470
  92. package/src/DeltaBundler/traverseDependencies.js.flow +0 -565
  93. package/src/node-haste/DependencyGraph/types.js +0 -10
  94. package/src/node-haste/DependencyGraph/types.js.flow +0 -88
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var _jestHasteMap = require("jest-haste-map");
3
+ var _metroFileMap = require("metro-file-map");
4
4
 
5
5
  /**
6
6
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -15,8 +15,6 @@ const createHasteMap = require("./DependencyGraph/createHasteMap");
15
15
 
16
16
  const { ModuleResolver } = require("./DependencyGraph/ModuleResolution");
17
17
 
18
- const Module = require("./Module");
19
-
20
18
  const ModuleCache = require("./ModuleCache");
21
19
 
22
20
  const { EventEmitter } = require("events");
@@ -35,7 +33,7 @@ const nullthrows = require("nullthrows");
35
33
 
36
34
  const path = require("path");
37
35
 
38
- const { DuplicateHasteCandidatesError } = _jestHasteMap.ModuleMap;
36
+ const { DuplicateHasteCandidatesError } = _metroFileMap.ModuleMap;
39
37
 
40
38
  function getOrCreate(map, field) {
41
39
  let subMap = map.get(field);
@@ -49,25 +47,12 @@ function getOrCreate(map, field) {
49
47
  }
50
48
 
51
49
  class DependencyGraph extends EventEmitter {
52
- constructor({ config, haste, initialHasteFS, initialModuleMap }) {
50
+ constructor(config, options) {
53
51
  super();
54
52
  this._config = config;
55
- this._haste = haste;
56
- this._hasteFS = initialHasteFS;
57
- this._moduleMap = initialModuleMap;
58
53
  this._assetExtensions = new Set(
59
54
  config.resolver.assetExts.map((asset) => "." + asset)
60
- ); // $FlowFixMe[method-unbinding] added when improving typing for this parameters
61
-
62
- this._haste.on("change", this._onHasteChange.bind(this));
63
-
64
- this._resolutionCache = new Map();
65
- this._moduleCache = this._createModuleCache();
66
-
67
- this._createModuleResolver();
68
- }
69
-
70
- static async load(config, options) {
55
+ );
71
56
  const { hasReducedPerformance, watch } =
72
57
  options !== null && options !== void 0 ? options : {};
73
58
  const initializingMetroLogEntry = log(
@@ -83,17 +68,34 @@ class DependencyGraph extends EventEmitter {
83
68
  // Bump this up to silence the max listeners EventEmitter warning.
84
69
 
85
70
  haste.setMaxListeners(1000);
86
- const { hasteFS, moduleMap } = await haste.build();
87
- log(createActionEndEntry(initializingMetroLogEntry));
88
- config.reporter.update({
89
- type: "dep_graph_loaded",
90
- });
91
- return new DependencyGraph({
92
- haste,
93
- initialHasteFS: hasteFS,
94
- initialModuleMap: moduleMap,
95
- config,
71
+ this._haste = haste;
72
+ this._readyPromise = haste.build().then(({ hasteFS, moduleMap }) => {
73
+ log(createActionEndEntry(initializingMetroLogEntry));
74
+ config.reporter.update({
75
+ type: "dep_graph_loaded",
76
+ });
77
+ this._hasteFS = hasteFS;
78
+ this._moduleMap = moduleMap; // $FlowFixMe[method-unbinding] added when improving typing for this parameters
79
+
80
+ this._haste.on("change", this._onHasteChange.bind(this));
81
+
82
+ this._resolutionCache = new Map();
83
+ this._moduleCache = this._createModuleCache();
84
+
85
+ this._createModuleResolver();
96
86
  });
87
+ } // Waits for the dependency graph to become ready after initialisation.
88
+ // Don't read anything from the graph until this resolves.
89
+
90
+ async ready() {
91
+ await this._readyPromise;
92
+ } // Creates the dependency graph and waits for it to become ready.
93
+ // @deprecated Use the constructor + ready() directly.
94
+
95
+ static async load(config, options) {
96
+ const self = new DependencyGraph(config, options);
97
+ await self.ready();
98
+ return self;
97
99
  }
98
100
 
99
101
  _getClosestPackage(filePath) {
@@ -8,15 +8,15 @@
8
8
  * @format
9
9
  */
10
10
 
11
- import type {HasteFS, HasteMap, ModuleMap} from './DependencyGraph/types';
12
11
  import type Package from './Package';
13
12
  import type {ConfigT} from 'metro-config/src/configTypes.flow';
13
+ import type MetroFileMap, {HasteFS} from 'metro-file-map';
14
+ import type Module from './Module';
14
15
 
15
- import {ModuleMap as JestHasteModuleMap} from 'jest-haste-map';
16
+ import {ModuleMap as MetroFileMapModuleMap} from 'metro-file-map';
16
17
 
17
18
  const createHasteMap = require('./DependencyGraph/createHasteMap');
18
19
  const {ModuleResolver} = require('./DependencyGraph/ModuleResolution');
19
- const Module = require('./Module');
20
20
  const ModuleCache = require('./ModuleCache');
21
21
  const {EventEmitter} = require('events');
22
22
  const fs = require('fs');
@@ -29,7 +29,7 @@ const {InvalidPackageError} = require('metro-resolver');
29
29
  const nullthrows = require('nullthrows');
30
30
  const path = require('path');
31
31
 
32
- const {DuplicateHasteCandidatesError} = JestHasteModuleMap;
32
+ const {DuplicateHasteCandidatesError} = MetroFileMapModuleMap;
33
33
 
34
34
  function getOrCreate<T>(
35
35
  map: Map<string, Map<string, T>>,
@@ -46,43 +46,28 @@ function getOrCreate<T>(
46
46
  class DependencyGraph extends EventEmitter {
47
47
  _assetExtensions: Set<string>;
48
48
  _config: ConfigT;
49
- _haste: HasteMap;
49
+ _haste: MetroFileMap;
50
50
  _hasteFS: HasteFS;
51
51
  _moduleCache: ModuleCache;
52
- _moduleMap: ModuleMap;
52
+ _moduleMap: MetroFileMapModuleMap;
53
53
  _moduleResolver: ModuleResolver<Module, Package>;
54
54
  _resolutionCache: Map<string, Map<string, Map<string, string>>>;
55
+ _readyPromise: Promise<void>;
55
56
 
56
- constructor({
57
- config,
58
- haste,
59
- initialHasteFS,
60
- initialModuleMap,
61
- }: {|
62
- +config: ConfigT,
63
- +haste: HasteMap,
64
- +initialHasteFS: HasteFS,
65
- +initialModuleMap: ModuleMap,
66
- |}) {
57
+ constructor(
58
+ config: ConfigT,
59
+ options?: {
60
+ +hasReducedPerformance?: boolean,
61
+ +watch?: boolean,
62
+ },
63
+ ) {
67
64
  super();
65
+
68
66
  this._config = config;
69
- this._haste = haste;
70
- this._hasteFS = initialHasteFS;
71
- this._moduleMap = initialModuleMap;
72
67
  this._assetExtensions = new Set(
73
68
  config.resolver.assetExts.map(asset => '.' + asset),
74
69
  );
75
- // $FlowFixMe[method-unbinding] added when improving typing for this parameters
76
- this._haste.on('change', this._onHasteChange.bind(this));
77
- this._resolutionCache = new Map();
78
- this._moduleCache = this._createModuleCache();
79
- this._createModuleResolver();
80
- }
81
70
 
82
- static async load(
83
- config: ConfigT,
84
- options?: {|+hasReducedPerformance?: boolean, +watch?: boolean|},
85
- ): Promise<DependencyGraph> {
86
71
  const {hasReducedPerformance, watch} = options ?? {};
87
72
  const initializingMetroLogEntry = log(
88
73
  createActionStartEntry('Initializing Metro'),
@@ -98,19 +83,40 @@ class DependencyGraph extends EventEmitter {
98
83
  // Bump this up to silence the max listeners EventEmitter warning.
99
84
  haste.setMaxListeners(1000);
100
85
 
101
- const {hasteFS, moduleMap} = await haste.build();
86
+ this._haste = haste;
87
+
88
+ this._readyPromise = haste.build().then(({hasteFS, moduleMap}) => {
89
+ log(createActionEndEntry(initializingMetroLogEntry));
90
+ config.reporter.update({type: 'dep_graph_loaded'});
102
91
 
103
- log(createActionEndEntry(initializingMetroLogEntry));
104
- config.reporter.update({type: 'dep_graph_loaded'});
92
+ this._hasteFS = hasteFS;
93
+ this._moduleMap = moduleMap;
105
94
 
106
- return new DependencyGraph({
107
- haste,
108
- initialHasteFS: hasteFS,
109
- initialModuleMap: moduleMap,
110
- config,
95
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
96
+ this._haste.on('change', this._onHasteChange.bind(this));
97
+ this._resolutionCache = new Map();
98
+ this._moduleCache = this._createModuleCache();
99
+ this._createModuleResolver();
111
100
  });
112
101
  }
113
102
 
103
+ // Waits for the dependency graph to become ready after initialisation.
104
+ // Don't read anything from the graph until this resolves.
105
+ async ready(): Promise<void> {
106
+ await this._readyPromise;
107
+ }
108
+
109
+ // Creates the dependency graph and waits for it to become ready.
110
+ // @deprecated Use the constructor + ready() directly.
111
+ static async load(
112
+ config: ConfigT,
113
+ options?: {+hasReducedPerformance?: boolean, +watch?: boolean},
114
+ ): Promise<DependencyGraph> {
115
+ const self = new DependencyGraph(config, options);
116
+ await self.ready();
117
+ return self;
118
+ }
119
+
114
120
  _getClosestPackage(filePath: string): ?string {
115
121
  const parsedPath = path.parse(filePath);
116
122
  const root = parsedPath.root;
@@ -69,7 +69,7 @@ class ModuleCache {
69
69
  }
70
70
 
71
71
  getPackageOf(modulePath: string): ?Package {
72
- let packagePath = this._packagePathByModulePath[modulePath];
72
+ let packagePath: ?string = this._packagePathByModulePath[modulePath];
73
73
  if (packagePath && this._packageCache[packagePath]) {
74
74
  return this._packageCache[packagePath];
75
75
  }
@@ -13,13 +13,13 @@
13
13
  const parsePlatformFilePath = require('./parsePlatformFilePath');
14
14
  const path = require('path');
15
15
 
16
- export type AssetPath = {|
16
+ export type AssetPath = {
17
17
  assetName: string,
18
18
  name: string,
19
19
  platform: ?string,
20
20
  resolution: number,
21
21
  type: string,
22
- |};
22
+ };
23
23
 
24
24
  const ASSET_BASE_NAME_RE = /(.+?)(@([\d.]+)x)?$/;
25
25
 
@@ -12,12 +12,12 @@
12
12
 
13
13
  const path = require('path');
14
14
 
15
- type PlatformFilePathParts = {|
15
+ type PlatformFilePathParts = {
16
16
  dirPath: string,
17
17
  baseName: string,
18
18
  platform: ?string,
19
19
  extension: ?string,
20
- |};
20
+ };
21
21
 
22
22
  const PATH_RE = /^(.+?)(\.([^.]+))?\.([^.]+)$/;
23
23
 
@@ -109,7 +109,7 @@ function moduleToBuffer(
109
109
  id: number,
110
110
  code: string,
111
111
  encoding: void | 'ascii' | 'utf16le' | 'utf8',
112
- ): {|buffer: Buffer, id: number|} {
112
+ ): {buffer: Buffer, id: number} {
113
113
  return {
114
114
  id,
115
115
  buffer: nullTerminatedBuffer(code, encoding),
@@ -19,12 +19,12 @@ const {
19
19
  joinModules,
20
20
  } = require('./util');
21
21
 
22
- type Params = {|
22
+ type Params = {
23
23
  fixWrapperOffset: boolean,
24
24
  lazyModules: $ReadOnlyArray<ModuleTransportLike>,
25
25
  moduleGroups: ?ModuleGroups,
26
26
  startupModules: $ReadOnlyArray<ModuleTransportLike>,
27
- |};
27
+ };
28
28
 
29
29
  module.exports = (({
30
30
  fixWrapperOffset,
@@ -61,33 +61,33 @@ export type BundleOptions = {
61
61
  ...
62
62
  };
63
63
 
64
- export type SerializerOptions = {|
64
+ export type SerializerOptions = {
65
65
  +sourceMapUrl: ?string,
66
66
  +sourceUrl: ?string,
67
67
  +runModule: boolean,
68
68
  +excludeSource: boolean,
69
69
  +inlineSourceMap: boolean,
70
70
  +modulesOnly: boolean,
71
- |};
71
+ };
72
72
 
73
- export type GraphOptions = {|
73
+ export type GraphOptions = {
74
74
  +shallow: boolean,
75
- |};
75
+ };
76
76
 
77
77
  // Stricter representation of BundleOptions.
78
- export type SplitBundleOptions = {|
78
+ export type SplitBundleOptions = {
79
79
  +entryFile: string,
80
80
  +transformOptions: TransformInputOptions,
81
81
  +serializerOptions: SerializerOptions,
82
82
  +graphOptions: GraphOptions,
83
83
  +onProgress: $PropertyType<DeltaBundlerOptions<>, 'onProgress'>,
84
- |};
84
+ };
85
85
 
86
- export type ModuleGroups = {|
86
+ export type ModuleGroups = {
87
87
  groups: Map<number, Set<number>>,
88
88
  modulesById: Map<number, ModuleTransportLike>,
89
89
  modulesInGroups: Set<number>,
90
- |};
90
+ };
91
91
 
92
92
  export type ModuleTransportLike = {
93
93
  +code: string,
@@ -97,18 +97,18 @@ export type ModuleTransportLike = {
97
97
  +sourcePath: string,
98
98
  ...
99
99
  };
100
- export type ModuleTransportLikeStrict = {|
100
+ export type ModuleTransportLikeStrict = {
101
101
  +code: string,
102
102
  +id: number,
103
103
  +map: ?MetroSourceMapOrMappings,
104
104
  +name?: string,
105
105
  +sourcePath: string,
106
- |};
107
- export type RamModuleTransport = {|
106
+ };
107
+ export type RamModuleTransport = {
108
108
  ...ModuleTransportLikeStrict,
109
109
  +source: string,
110
110
  +type: string,
111
- |};
111
+ };
112
112
 
113
113
  export type OutputOptions = {
114
114
  bundleOutput: string,
@@ -122,7 +122,7 @@ export type OutputOptions = {
122
122
  ...
123
123
  };
124
124
 
125
- export type RequestOptions = {|
125
+ export type RequestOptions = {
126
126
  entryFile: string,
127
127
  inlineSourceMap?: boolean,
128
128
  sourceMapUrl?: string,
@@ -131,6 +131,6 @@ export type RequestOptions = {|
131
131
  platform: string,
132
132
  createModuleIdFactory?: () => (path: string) => number,
133
133
  onProgress?: (transformedFileCount: number, totalFileCount: number) => void,
134
- |};
134
+ };
135
135
 
136
136
  export type {MinifierOptions};
@@ -1,42 +0,0 @@
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
- function computeDelta(entries1, entries2) {
13
- const modules1 = new Map(entries1);
14
- const modules2 = new Map(entries2);
15
- const added = new Map();
16
- const modified = new Map();
17
- const deleted = new Set();
18
-
19
- for (const [id, code] of modules1.entries()) {
20
- const newCode = modules2.get(id);
21
-
22
- if (newCode == null) {
23
- deleted.add(id);
24
- } else if (newCode !== code) {
25
- modified.set(id, newCode);
26
- }
27
- }
28
-
29
- for (const [id, code] of modules2.entries()) {
30
- if (!modules1.has(id)) {
31
- added.set(id, code);
32
- }
33
- }
34
-
35
- return {
36
- added: [...added.entries()],
37
- modified: [...modified.entries()],
38
- deleted: [...deleted],
39
- };
40
- }
41
-
42
- module.exports = computeDelta;
@@ -1,47 +0,0 @@
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
- 'use strict';
12
-
13
- import type {
14
- DeltaBundle,
15
- ModuleMap,
16
- } from 'metro-runtime/src/modules/types.flow';
17
-
18
- function computeDelta(entries1: ModuleMap, entries2: ModuleMap): DeltaBundle {
19
- const modules1 = new Map(entries1);
20
- const modules2 = new Map(entries2);
21
- const added = new Map();
22
- const modified = new Map();
23
- const deleted = new Set();
24
-
25
- for (const [id, code] of modules1.entries()) {
26
- const newCode = modules2.get(id);
27
- if (newCode == null) {
28
- deleted.add(id);
29
- } else if (newCode !== code) {
30
- modified.set(id, newCode);
31
- }
32
- }
33
-
34
- for (const [id, code] of modules2.entries()) {
35
- if (!modules1.has(id)) {
36
- added.set(id, code);
37
- }
38
- }
39
-
40
- return {
41
- added: [...added.entries()],
42
- modified: [...modified.entries()],
43
- deleted: [...deleted],
44
- };
45
- }
46
-
47
- module.exports = computeDelta;