metro 0.72.0 → 0.72.1

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 (71) hide show
  1. package/package.json +21 -21
  2. package/src/Bundler.js +11 -2
  3. package/src/Bundler.js.flow +7 -1
  4. package/src/DeltaBundler/DeltaCalculator.js +83 -21
  5. package/src/DeltaBundler/DeltaCalculator.js.flow +61 -8
  6. package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +1 -3
  7. package/src/DeltaBundler/Transformer.js +27 -4
  8. package/src/DeltaBundler/Transformer.js.flow +18 -2
  9. package/src/DeltaBundler/Worker.flow.js +45 -1
  10. package/src/DeltaBundler/Worker.flow.js.flow +42 -1
  11. package/src/DeltaBundler/WorkerFarm.js +3 -2
  12. package/src/DeltaBundler/WorkerFarm.js.flow +3 -1
  13. package/src/DeltaBundler/graphOperations.js +131 -20
  14. package/src/DeltaBundler/graphOperations.js.flow +106 -17
  15. package/src/DeltaBundler/types.flow.js.flow +6 -3
  16. package/src/HmrServer.js +2 -0
  17. package/src/HmrServer.js.flow +2 -0
  18. package/src/IncrementalBundler.js +6 -0
  19. package/src/IncrementalBundler.js.flow +6 -0
  20. package/src/ModuleGraph/node-haste/HasteFS.js.flow +1 -1
  21. package/src/ModuleGraph/node-haste/node-haste.js +2 -4
  22. package/src/ModuleGraph/node-haste/node-haste.js.flow +1 -3
  23. package/src/ModuleGraph/output/indexed-ram-bundle.js.flow +5 -13
  24. package/src/ModuleGraph/output/multiple-files-ram-bundle.js.flow +4 -14
  25. package/src/ModuleGraph/output/util.js.flow +1 -1
  26. package/src/ModuleGraph/worker/collectDependencies.js.flow +1 -1
  27. package/src/Server.js +4 -0
  28. package/src/Server.js.flow +37 -10
  29. package/src/integration_tests/basic_bundle/require-context/conflict.js +25 -0
  30. package/src/integration_tests/basic_bundle/require-context/conflict.js.flow +27 -0
  31. package/src/integration_tests/basic_bundle/require-context/empty.js +29 -0
  32. package/src/integration_tests/basic_bundle/require-context/empty.js.flow +26 -0
  33. package/src/integration_tests/basic_bundle/require-context/matching.js +26 -0
  34. package/src/integration_tests/basic_bundle/require-context/matching.js.flow +27 -0
  35. package/src/integration_tests/basic_bundle/require-context/mode-eager.js +22 -0
  36. package/src/integration_tests/basic_bundle/require-context/mode-eager.js.flow +24 -0
  37. package/src/integration_tests/basic_bundle/require-context/mode-lazy-once.js +22 -0
  38. package/src/integration_tests/basic_bundle/require-context/mode-lazy-once.js.flow +24 -0
  39. package/src/integration_tests/basic_bundle/require-context/mode-lazy.js +22 -0
  40. package/src/integration_tests/basic_bundle/require-context/mode-lazy.js.flow +24 -0
  41. package/src/integration_tests/basic_bundle/require-context/mode-sync.js +20 -0
  42. package/src/integration_tests/basic_bundle/require-context/mode-sync.js.flow +22 -0
  43. package/src/integration_tests/basic_bundle/require-context/subdir/a.js +12 -0
  44. package/src/integration_tests/basic_bundle/require-context/subdir/a.js.flow +11 -0
  45. package/src/integration_tests/basic_bundle/require-context/subdir/b.js +18 -0
  46. package/src/integration_tests/basic_bundle/require-context/subdir/b.js.flow +11 -0
  47. package/src/integration_tests/basic_bundle/require-context/subdir/c.js +12 -0
  48. package/src/integration_tests/basic_bundle/require-context/subdir/c.js.flow +11 -0
  49. package/src/integration_tests/basic_bundle/require-context/subdir/nested/d.js +12 -0
  50. package/src/integration_tests/basic_bundle/require-context/subdir/nested/d.js.flow +11 -0
  51. package/src/integration_tests/basic_bundle/require-context/subdir-conflict/index.js +12 -0
  52. package/src/integration_tests/basic_bundle/require-context/subdir-conflict/index.js.flow +11 -0
  53. package/src/integration_tests/basic_bundle/require-context/utils.js +29 -0
  54. package/src/integration_tests/basic_bundle/require-context/utils.js.flow +44 -0
  55. package/src/lib/CountingSet.js +1 -0
  56. package/src/lib/CountingSet.js.flow +1 -0
  57. package/src/lib/contextModule.js +80 -0
  58. package/src/lib/contextModule.js.flow +86 -0
  59. package/src/lib/contextModuleTemplates.js +186 -0
  60. package/src/lib/contextModuleTemplates.js.flow +148 -0
  61. package/src/lib/getGraphId.js +2 -1
  62. package/src/lib/getGraphId.js.flow +3 -0
  63. package/src/lib/getPrependedScripts.js +2 -0
  64. package/src/lib/getPrependedScripts.js.flow +2 -0
  65. package/src/lib/parseOptionsFromUrl.js.flow +7 -18
  66. package/src/lib/transformHelpers.js +41 -9
  67. package/src/lib/transformHelpers.js.flow +46 -9
  68. package/src/node-haste/DependencyGraph/ModuleResolution.js +1 -0
  69. package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +3 -2
  70. package/src/node-haste/DependencyGraph.js +5 -0
  71. package/src/node-haste/DependencyGraph.js.flow +14 -1
@@ -11,6 +11,7 @@
11
11
  'use strict';
12
12
 
13
13
  import type {BundleOptions} from '../shared/types.flow';
14
+ import type {TransformProfile} from 'metro-babel-transformer';
14
15
 
15
16
  const parsePlatformFilePath = require('../node-haste/lib/parsePlatformFilePath');
16
17
  const parseCustomTransformOptions = require('./parseCustomTransformOptions');
@@ -19,15 +20,8 @@ const path = require('path');
19
20
  const url = require('url');
20
21
 
21
22
  const getBoolean = (
22
- query: {[string]: string},
23
- opt:
24
- | $TEMPORARY$string<'dev'>
25
- | $TEMPORARY$string<'excludeSource'>
26
- | $TEMPORARY$string<'inlineSourceMap'>
27
- | $TEMPORARY$string<'minify'>
28
- | $TEMPORARY$string<'modulesOnly'>
29
- | $TEMPORARY$string<'runModule'>
30
- | $TEMPORARY$string<'shallow'>,
23
+ query: $ReadOnly<{[opt: string]: string}>,
24
+ opt: string,
31
25
  defaultValue: boolean,
32
26
  ) =>
33
27
  query[opt] == null
@@ -35,23 +29,18 @@ const getBoolean = (
35
29
  : query[opt] === 'true' || query[opt] === '1';
36
30
 
37
31
  const getNumber = (
38
- query: {[string]: string},
39
- opt: $TEMPORARY$string<'runtimeBytecodeVersion'>,
32
+ query: $ReadOnly<{[opt: string]: string}>,
33
+ opt: string,
40
34
  defaultValue: null,
41
35
  ) => {
42
36
  const number = parseInt(query[opt], 10);
43
37
  return Number.isNaN(number) ? defaultValue : number;
44
38
  };
45
39
 
46
- const getBundleType = (bundleType: string | $TEMPORARY$string<'map'>) =>
40
+ const getBundleType = (bundleType: string): 'map' | 'bundle' =>
47
41
  bundleType === 'map' ? bundleType : 'bundle';
48
42
 
49
- const getTransformProfile = (
50
- transformProfile:
51
- | string
52
- | $TEMPORARY$string<'hermes-canary'>
53
- | $TEMPORARY$string<'hermes-stable'>,
54
- ) =>
43
+ const getTransformProfile = (transformProfile: string): TransformProfile =>
55
44
  transformProfile === 'hermes-stable' || transformProfile === 'hermes-canary'
56
45
  ? transformProfile
57
46
  : 'default';
@@ -9,6 +9,8 @@
9
9
  */
10
10
  "use strict";
11
11
 
12
+ var _contextModuleTemplates = require("./contextModuleTemplates");
13
+
12
14
  const path = require("path");
13
15
 
14
16
  const baseIgnoredInlineRequires = ["React", "react", "react-native"];
@@ -49,6 +51,8 @@ async function calcTransformerOptions(
49
51
  onProgress: null,
50
52
  experimentalImportBundleSupport:
51
53
  config.transformer.experimentalImportBundleSupport,
54
+ unstable_allowRequireContext:
55
+ config.transformer.unstable_allowRequireContext,
52
56
  shallow: false,
53
57
  });
54
58
  return Array.from(dependencies.keys());
@@ -97,15 +101,43 @@ async function getTransformFn(
97
101
  config,
98
102
  options
99
103
  );
100
- return async (path) => {
101
- return await bundler.transformFile(path, {
102
- ...transformOptions,
103
- type: getType(transformOptions.type, path, config.resolver.assetExts),
104
- inlineRequires: removeInlineRequiresBlockListFromOptions(
105
- path,
106
- inlineRequires
107
- ),
108
- });
104
+ return async (modulePath, requireContext) => {
105
+ let templateBuffer;
106
+
107
+ if (requireContext) {
108
+ const graph = await bundler.getDependencyGraph(); // TODO: Check delta changes to avoid having to look over all files each time
109
+ // this is a massive performance boost.
110
+ // Search against all files, this is very expensive.
111
+ // TODO: Maybe we could let the user specify which root to check against.
112
+
113
+ const files = graph.matchFilesWithContext(requireContext.from, {
114
+ filter: requireContext.filter,
115
+ recursive: requireContext.recursive,
116
+ });
117
+ const template = (0, _contextModuleTemplates.getContextModuleTemplate)(
118
+ requireContext.mode,
119
+ requireContext.from,
120
+ files
121
+ );
122
+ templateBuffer = Buffer.from(template);
123
+ }
124
+
125
+ return await bundler.transformFile(
126
+ modulePath,
127
+ {
128
+ ...transformOptions,
129
+ type: getType(
130
+ transformOptions.type,
131
+ modulePath,
132
+ config.resolver.assetExts
133
+ ),
134
+ inlineRequires: removeInlineRequiresBlockListFromOptions(
135
+ modulePath,
136
+ inlineRequires
137
+ ),
138
+ },
139
+ templateBuffer
140
+ );
109
141
  };
110
142
  }
111
143
 
@@ -16,6 +16,9 @@ import type {TransformInputOptions} from '../DeltaBundler/types.flow';
16
16
  import type {TransformOptions} from '../DeltaBundler/Worker';
17
17
  import type {ConfigT} from 'metro-config/src/configTypes.flow';
18
18
  import type {Type} from 'metro-transform-worker';
19
+ import type {RequireContext} from './contextModule';
20
+
21
+ import {getContextModuleTemplate} from './contextModuleTemplates';
19
22
 
20
23
  const path = require('path');
21
24
 
@@ -68,6 +71,8 @@ async function calcTransformerOptions(
68
71
  onProgress: null,
69
72
  experimentalImportBundleSupport:
70
73
  config.transformer.experimentalImportBundleSupport,
74
+ unstable_allowRequireContext:
75
+ config.transformer.unstable_allowRequireContext,
71
76
  shallow: false,
72
77
  });
73
78
 
@@ -118,15 +123,47 @@ async function getTransformFn(
118
123
  options,
119
124
  );
120
125
 
121
- return async (path: string) => {
122
- return await bundler.transformFile(path, {
123
- ...transformOptions,
124
- type: getType(transformOptions.type, path, config.resolver.assetExts),
125
- inlineRequires: removeInlineRequiresBlockListFromOptions(
126
- path,
127
- inlineRequires,
128
- ),
129
- });
126
+ return async (modulePath: string, requireContext: ?RequireContext) => {
127
+ let templateBuffer: Buffer;
128
+
129
+ if (requireContext) {
130
+ const graph = await bundler.getDependencyGraph();
131
+
132
+ // TODO: Check delta changes to avoid having to look over all files each time
133
+ // this is a massive performance boost.
134
+
135
+ // Search against all files, this is very expensive.
136
+ // TODO: Maybe we could let the user specify which root to check against.
137
+ const files = graph.matchFilesWithContext(requireContext.from, {
138
+ filter: requireContext.filter,
139
+ recursive: requireContext.recursive,
140
+ });
141
+
142
+ const template = getContextModuleTemplate(
143
+ requireContext.mode,
144
+ requireContext.from,
145
+ files,
146
+ );
147
+
148
+ templateBuffer = Buffer.from(template);
149
+ }
150
+
151
+ return await bundler.transformFile(
152
+ modulePath,
153
+ {
154
+ ...transformOptions,
155
+ type: getType(
156
+ transformOptions.type,
157
+ modulePath,
158
+ config.resolver.assetExts,
159
+ ),
160
+ inlineRequires: removeInlineRequiresBlockListFromOptions(
161
+ modulePath,
162
+ inlineRequires,
163
+ ),
164
+ },
165
+ templateBuffer,
166
+ );
130
167
  };
131
168
  }
132
169
 
@@ -199,6 +199,7 @@ class ModuleResolver {
199
199
  return this._options.moduleCache.getModule(arbitrary);
200
200
 
201
201
  case "empty":
202
+ // $FlowFixMe[incompatible-return]
202
203
  return this._getEmptyModule();
203
204
 
204
205
  default:
@@ -94,7 +94,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
94
94
  };
95
95
  }
96
96
 
97
- _getEmptyModule() {
97
+ _getEmptyModule(): TModule | Moduleish {
98
98
  let emptyModule = this._cachedEmptyModule;
99
99
  if (!emptyModule) {
100
100
  emptyModule = this.resolveDependency(
@@ -252,6 +252,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
252
252
  invariant(arbitrary != null, 'invalid asset resolution');
253
253
  return this._options.moduleCache.getModule(arbitrary);
254
254
  case 'empty':
255
+ // $FlowFixMe[incompatible-return]
255
256
  return this._getEmptyModule();
256
257
  default:
257
258
  (resolution.type: empty);
@@ -259,7 +260,7 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
259
260
  }
260
261
  }
261
262
 
262
- _removeRoot(candidates: FileCandidates) {
263
+ _removeRoot(candidates: FileCandidates): FileCandidates {
263
264
  if (candidates.filePathPrefix) {
264
265
  candidates.filePathPrefix = path.relative(
265
266
  this._options.projectRoot,
@@ -208,6 +208,11 @@ class DependencyGraph extends EventEmitter {
208
208
  end() {
209
209
  this._haste.end();
210
210
  }
211
+ /** Given a search context, return a list of file paths matching the query. */
212
+
213
+ matchFilesWithContext(from, context) {
214
+ return this._hasteFS.matchFilesWithContext(from, context);
215
+ }
211
216
 
212
217
  resolveDependency(
213
218
  from,
@@ -179,7 +179,7 @@ class DependencyGraph extends EventEmitter {
179
179
  });
180
180
  }
181
181
 
182
- _createModuleCache() {
182
+ _createModuleCache(): ModuleCache {
183
183
  return new ModuleCache({
184
184
  // $FlowFixMe[method-unbinding] added when improving typing for this parameters
185
185
  getClosestPackage: this._getClosestPackage.bind(this),
@@ -225,6 +225,19 @@ class DependencyGraph extends EventEmitter {
225
225
  this._haste.end();
226
226
  }
227
227
 
228
+ /** Given a search context, return a list of file paths matching the query. */
229
+ matchFilesWithContext(
230
+ from: string,
231
+ context: $ReadOnly<{
232
+ /* Should search for files recursively. */
233
+ recursive: boolean,
234
+ /* Filter relative paths against a pattern. */
235
+ filter: RegExp,
236
+ }>,
237
+ ): string[] {
238
+ return this._hasteFS.matchFilesWithContext(from, context);
239
+ }
240
+
228
241
  resolveDependency(
229
242
  from: string,
230
243
  to: string,