@expo/cli 58.1.20 → 58.1.22

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 (67) hide show
  1. package/build/metro-require/require.js +2 -0
  2. package/build/metro-require/require.js.map +1 -1
  3. package/build/src/api/rest/wrapFetchWithUserAgent.js +1 -1
  4. package/build/src/export/compileHermesArtifacts.d.ts +1 -2
  5. package/build/src/export/compileHermesArtifacts.js +7 -13
  6. package/build/src/export/compileHermesArtifacts.js.map +1 -1
  7. package/build/src/export/embed/exportEmbedAsync.js +9 -2
  8. package/build/src/export/embed/exportEmbedAsync.js.map +1 -1
  9. package/build/src/export/exportApp.js +1 -1
  10. package/build/src/export/exportApp.js.map +1 -1
  11. package/build/src/export/exportDomComponents.js +10 -23
  12. package/build/src/export/exportDomComponents.js.map +1 -1
  13. package/build/src/export/exportStaticAsync.js +6 -6
  14. package/build/src/export/exportStaticAsync.js.map +1 -1
  15. package/build/src/index.js +5 -2
  16. package/build/src/index.js.map +1 -1
  17. package/build/src/serve/adapter.d.ts +3 -0
  18. package/build/src/serve/adapter.js +90 -0
  19. package/build/src/serve/adapter.js.map +1 -0
  20. package/build/src/serve/serveAsync.js +10 -16
  21. package/build/src/serve/serveAsync.js.map +1 -1
  22. package/build/src/start/doctor/dependencies/getMissingPackages.js +6 -4
  23. package/build/src/start/doctor/dependencies/getMissingPackages.js.map +1 -1
  24. package/build/src/start/resolveOptions.d.ts +1 -1
  25. package/build/src/start/resolveOptions.js +1 -0
  26. package/build/src/start/resolveOptions.js.map +1 -1
  27. package/build/src/start/server/DevServerManager.js +2 -0
  28. package/build/src/start/server/DevServerManager.js.map +1 -1
  29. package/build/src/start/server/UrlCreator.d.ts +2 -2
  30. package/build/src/start/server/UrlCreator.js +6 -7
  31. package/build/src/start/server/UrlCreator.js.map +1 -1
  32. package/build/src/start/server/getStaticRenderFunctions.js +1 -3
  33. package/build/src/start/server/getStaticRenderFunctions.js.map +1 -1
  34. package/build/src/start/server/metro/MetroBundlerDevServer.d.ts +9 -10
  35. package/build/src/start/server/metro/MetroBundlerDevServer.js +119 -102
  36. package/build/src/start/server/metro/MetroBundlerDevServer.js.map +1 -1
  37. package/build/src/start/server/metro/createFileMap-fork.js +1 -1
  38. package/build/src/start/server/metro/createFileMap-fork.js.map +1 -1
  39. package/build/src/start/server/metro/createServerComponentsMiddleware.d.ts +6 -3
  40. package/build/src/start/server/metro/createServerComponentsMiddleware.js +21 -29
  41. package/build/src/start/server/metro/createServerComponentsMiddleware.js.map +1 -1
  42. package/build/src/start/server/metro/instantiateMetro.js +5 -0
  43. package/build/src/start/server/metro/instantiateMetro.js.map +1 -1
  44. package/build/src/start/server/metro/serverArtifacts.d.ts +4 -0
  45. package/build/src/start/server/metro/serverArtifacts.js +55 -0
  46. package/build/src/start/server/metro/serverArtifacts.js.map +1 -0
  47. package/build/src/start/server/middleware/inspector/createJsInspectorMiddleware.js +20 -2
  48. package/build/src/start/server/middleware/inspector/createJsInspectorMiddleware.js.map +1 -1
  49. package/build/src/start/server/middleware/metroOptions.d.ts +13 -3
  50. package/build/src/start/server/middleware/metroOptions.js +13 -10
  51. package/build/src/start/server/middleware/metroOptions.js.map +1 -1
  52. package/build/src/utils/env.js +2 -1
  53. package/build/src/utils/env.js.map +1 -1
  54. package/build/src/utils/ip.d.ts +1 -0
  55. package/build/src/utils/ip.js +48 -18
  56. package/build/src/utils/ip.js.map +1 -1
  57. package/build/src/utils/parentCommand.d.ts +4 -0
  58. package/build/src/utils/parentCommand.js +34 -0
  59. package/build/src/utils/parentCommand.js.map +1 -0
  60. package/build/src/utils/port.d.ts +4 -2
  61. package/build/src/utils/port.js +27 -15
  62. package/build/src/utils/port.js.map +1 -1
  63. package/build/src/utils/telemetry/clients/FetchClient.js +1 -1
  64. package/build/src/utils/telemetry/utils/context.d.ts +1 -0
  65. package/build/src/utils/telemetry/utils/context.js +3 -1
  66. package/build/src/utils/telemetry/utils/context.js.map +1 -1
  67. package/package.json +11 -11
@@ -41,6 +41,20 @@ function _paths() {
41
41
  };
42
42
  return data;
43
43
  }
44
+ function _getServerRunModuleStatement() {
45
+ const data = require("@expo/metro-config/build/serializer/getServerRunModuleStatement");
46
+ _getServerRunModuleStatement = function() {
47
+ return data;
48
+ };
49
+ return data;
50
+ }
51
+ function _serializeServerBundle() {
52
+ const data = require("@expo/metro-config/build/serializer/serializeServerBundle");
53
+ _serializeServerBundle = function() {
54
+ return data;
55
+ };
56
+ return data;
57
+ }
44
58
  function _sourceMap() {
45
59
  const data = require("@expo/metro-config/build/serializer/sourceMap");
46
60
  _sourceMap = function() {
@@ -130,6 +144,7 @@ const _metroWatchTypeScriptFiles = require("./metroWatchTypeScriptFiles");
130
144
  const _resolveLoader = require("./resolveLoader");
131
145
  const _router = require("./router");
132
146
  const _serializeHtml = require("./serializeHtml");
147
+ const _serverArtifacts = require("./serverArtifacts");
133
148
  const _waitForMetroToObserveTypeScriptFile = require("./waitForMetroToObserveTypeScriptFile");
134
149
  function _interop_require_default(obj) {
135
150
  return obj && obj.__esModule ? obj : {
@@ -149,50 +164,21 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
149
164
  return 'metro';
150
165
  }
151
166
  async exportServerRouteAsync({ contents, artifactFilename, files, includeSourceMaps, descriptor }) {
152
- if (!contents) return;
153
- let src = contents.src;
154
- if (includeSourceMaps && contents.map) {
155
- // TODO(kitten): Merge the source map transformer in the future
156
- // https://github.com/expo/expo/blob/0dffdb15/packages/%40expo/metro-config/src/serializer/serializeChunks.ts#L422-L439
157
- // Alternatively, check whether `sourcesRoot` helps here
158
- const artifactBasename = encodeURIComponent(_path().default.basename(artifactFilename) + '.map');
159
- // Match only the trailing sourcemap directive
160
- src = src.replace(/(?<=^|\n)\/\/# sourceMappingURL=[^\n]*(?=\s*$)/, `//# sourceMappingURL=${artifactBasename}`);
161
- // Metro emits indexed source maps, which have no top-level `sources` to rewrite
162
- const parsedMap = (0, _sourceMap().flattenSourceMap)(typeof contents.map === 'string' ? JSON.parse(contents.map) : contents.map);
163
- const mapData = {
164
- ...descriptor,
165
- contents: JSON.stringify({
166
- version: parsedMap.version,
167
- sources: parsedMap.sources.map((source)=>{
168
- if (source == null) {
169
- return source;
170
- }
171
- return (0, _metroOptions.convertPathToModuleSpecifier)(source.startsWith(this.projectRoot) ? _path().default.relative(this.projectRoot, source) : source);
172
- }),
173
- sourcesContent: new Array(parsedMap.sources.length).fill(null),
174
- names: parsedMap.names,
175
- mappings: parsedMap.mappings
176
- }),
177
- targetDomain: 'server'
178
- };
179
- files.set(artifactFilename + '.map', mapData);
180
- }
181
- const fileData = {
182
- ...descriptor,
183
- contents: src,
184
- targetDomain: 'server'
185
- };
186
- files.set(artifactFilename, fileData);
167
+ if (!contents) return artifactFilename;
168
+ (0, _assert().default)(contents.artifacts, 'Expected serialized artifacts for a server export.');
169
+ return (0, _serverArtifacts.persistServerArtifacts)(contents.artifacts.filter((artifact)=>includeSourceMaps || artifact.type !== 'map'), files, descriptor);
187
170
  }
188
171
  async exportMiddlewareAsync({ manifest, appDir, outputDir, files, platform, includeSourceMaps }) {
189
172
  if (!manifest.middleware) return;
190
173
  const middlewareFilePath = _path().default.isAbsolute(manifest.middleware.file) ? manifest.middleware.file : _path().default.join(appDir, manifest.middleware.file);
174
+ const serverOutputExtension = this.serverOutputExtension;
175
+ const artifactFilename = (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, _path().default.relative(appDir, middlewareFilePath.replace(/\.[tj]sx?$/, serverOutputExtension))));
191
176
  const contents = await this.bundleApiRoute(middlewareFilePath, {
192
- platform
177
+ platform,
178
+ entryFilename: artifactFilename,
179
+ includeSourceMaps: !!includeSourceMaps
193
180
  });
194
- const artifactFilename = (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, _path().default.relative(appDir, middlewareFilePath.replace(/\.[tj]sx?$/, '.js'))));
195
- await this.exportServerRouteAsync({
181
+ const filename = await this.exportServerRouteAsync({
196
182
  contents,
197
183
  artifactFilename,
198
184
  files,
@@ -202,10 +188,11 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
202
188
  }
203
189
  });
204
190
  // Remap the middleware file to represent the output file.
205
- manifest.middleware.file = artifactFilename;
191
+ manifest.middleware.file = filename;
206
192
  }
207
193
  async exportExpoRouterApiRoutesAsync({ includeSourceMaps, outputDir, prerenderManifest, platform }) {
208
194
  const { routerRoot } = this.instanceMetroOptions;
195
+ const serverOutputExtension = this.serverOutputExtension;
209
196
  (0, _assert().default)(routerRoot != null, 'The server must be started before calling exportExpoRouterApiRoutesAsync.');
210
197
  const appDir = _path().default.join(this.projectRoot, routerRoot);
211
198
  const manifest = await this.getExpoRouterRoutesManifestAsync({
@@ -242,11 +229,13 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
242
229
  });
243
230
  for (const route of manifest.apiRoutes){
244
231
  const filepath = _path().default.isAbsolute(route.file) ? route.file : _path().default.join(appDir, route.file);
232
+ const artifactFilename = route.page === rscPath ? (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, '.' + rscPath + serverOutputExtension)) : (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, _path().default.relative(appDir, filepath.replace(/\.[tj]sx?$/, serverOutputExtension))));
245
233
  const contents = await this.bundleApiRoute(filepath, {
246
- platform
234
+ platform,
235
+ entryFilename: artifactFilename,
236
+ includeSourceMaps: !!includeSourceMaps
247
237
  });
248
- const artifactFilename = route.page === rscPath ? (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, '.' + rscPath + '.js')) : (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, _path().default.relative(appDir, filepath.replace(/\.[tj]sx?$/, '.js'))));
249
- await this.exportServerRouteAsync({
238
+ const filename = await this.exportServerRouteAsync({
250
239
  contents,
251
240
  artifactFilename,
252
241
  files,
@@ -256,7 +245,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
256
245
  }
257
246
  });
258
247
  // Remap the manifest files to represent the output files.
259
- route.file = artifactFilename;
248
+ route.file = filename;
260
249
  }
261
250
  return {
262
251
  manifest: {
@@ -272,13 +261,16 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
272
261
  /**
273
262
  * Bundle render module for use in SSR
274
263
  */ async exportExpoRouterRenderModuleAsync({ files, includeSourceMaps, platform = 'web' }) {
264
+ const artifactFilename = `_expo/server/render${this.serverOutputExtension}`;
275
265
  const renderModule = await this.ssrLoadModuleContents(require.resolve('@expo/router-server/node/render.js'), {
276
266
  environment: 'node',
277
- platform
267
+ platform,
268
+ entryFilename: artifactFilename,
269
+ includeSourceMaps: !!includeSourceMaps
278
270
  });
279
- await this.exportServerRouteAsync({
271
+ return this.exportServerRouteAsync({
280
272
  contents: renderModule,
281
- artifactFilename: '_expo/server/render.js',
273
+ artifactFilename,
282
274
  files,
283
275
  includeSourceMaps,
284
276
  descriptor: {
@@ -286,7 +278,6 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
286
278
  targetDomain: 'server'
287
279
  }
288
280
  });
289
- return files;
290
281
  }
291
282
  /**
292
283
  * Export loader bundles as standalone JS files for SSR data loading.
@@ -295,14 +286,17 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
295
286
  * with all other exports (default, named) stripped out by the Babel plugin.
296
287
  * This keeps loader bundles small for efficient server-side execution.
297
288
  */ async exportExpoRouterLoadersAsync({ platform, entryPoints, files, outputDir, includeSourceMaps }) {
289
+ const filenames = new Map();
298
290
  // NOTE(@hassankhan): Should we parallelize loader bundling?
299
291
  for (const entry of entryPoints){
292
+ const pagePath = entry.page.startsWith('/') ? entry.page.slice(1) : entry.page;
293
+ const artifactFilename = (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, pagePath + this.serverOutputExtension));
300
294
  const contents = await this.bundleLoader(entry.file, {
301
- platform
295
+ platform,
296
+ entryFilename: artifactFilename,
297
+ includeSourceMaps: !!includeSourceMaps
302
298
  });
303
- const pagePath = entry.page.startsWith('/') ? entry.page.slice(1) : entry.page;
304
- const artifactFilename = (0, _metroOptions.convertPathToModuleSpecifier)(_path().default.join(outputDir, pagePath + '.js'));
305
- await this.exportServerRouteAsync({
299
+ const filename = await this.exportServerRouteAsync({
306
300
  contents,
307
301
  artifactFilename,
308
302
  files,
@@ -311,7 +305,9 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
311
305
  loaderId: entry.page
312
306
  }
313
307
  });
308
+ filenames.set(entry.page, filename);
314
309
  }
310
+ return filenames;
315
311
  }
316
312
  async getExpoRouterRoutesManifestAsync({ appDir }) {
317
313
  // getBuiltTimeServerManifest
@@ -432,7 +428,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
432
428
  mode,
433
429
  minify,
434
430
  environment: 'client',
435
- serializerIncludeMaps: includeSourceMaps,
431
+ includeSourceMaps,
436
432
  mainModuleName: resolvedMainModuleName,
437
433
  lazy: !_env.env.EXPO_NO_METRO_LAZY,
438
434
  asyncRoutes,
@@ -600,6 +596,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
600
596
  }
601
597
  // Set when the server is started.
602
598
  instanceMetroOptions = {};
599
+ serverOutputExtension = '.js';
603
600
  ssrLoadModule = async (filePath, specificOptions = {}, extras = {})=>{
604
601
  // NOTE(@kitten): We don't properly initialize the server-side modules
605
602
  // Instead, we first load an entrypoint with an empty bundle to initialize the runtime instead
@@ -621,7 +618,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
621
618
  };
622
619
  async metroImportAsArtifactsAsync(filePath, specificOptions = {}) {
623
620
  const results = await this.ssrLoadModuleContents(filePath, {
624
- serializerOutput: 'static',
621
+ output: 'static',
625
622
  ...specificOptions
626
623
  });
627
624
  // NOTE: This could potentially need more validation in the future.
@@ -657,7 +654,13 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
657
654
  baseUrl,
658
655
  // routerRoot,
659
656
  // isExporting,
660
- ...specificOptions
657
+ ...specificOptions,
658
+ ...specificOptions.entryFilename && {
659
+ output: 'static',
660
+ includeSourceMaps: !!specificOptions.includeSourceMaps,
661
+ splitChunks: false,
662
+ excludeSource: true
663
+ }
661
664
  };
662
665
  const expoBundleOptions = (0, _metroOptions.getMetroDirectBundleOptions)(opts);
663
666
  const resolverOptions = {
@@ -686,6 +689,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
686
689
  // That's why the defaults were added to match types. It's unclear though if that was correct
687
690
  // Maybe the `Server.DEFAULT_BUNDLE_OPTIONS` logic should be hoisted into `getMetroDirectBundleOptions`?
688
691
  const results = await this._bundleDirectAsync(resolvedEntryFilePath, {
692
+ getSourceUrl: extraOptions.getSourceUrl,
689
693
  graphOptions: {
690
694
  lazy: expoBundleOptions.lazy ?? false,
691
695
  shallow: expoBundleOptions.shallow ?? false
@@ -698,7 +702,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
698
702
  runModule: expoBundleOptions.runModule ?? true,
699
703
  sourceUrl: expoBundleOptions.sourceUrl,
700
704
  sourceMapUrl: extraOptions.sourceMapUrl ?? expoBundleOptions.sourceMapUrl,
701
- excludeSource: expoBundleOptions.serializerOptions.excludeSource ?? false
705
+ excludeSource: !!expoBundleOptions.serializerOptions.excludeSource
702
706
  },
703
707
  transformOptions
704
708
  });
@@ -732,12 +736,14 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
732
736
  isExporting,
733
737
  ...specificOptions
734
738
  };
739
+ if ((0, _metroOptions.isServerEnvironment)(opts.environment)) {
740
+ opts.outputExtension ??= this.serverOutputExtension;
741
+ }
735
742
  // https://github.com/facebook/metro/blob/2405f2f6c37a1b641cc379b9c733b1eff0c1c2a1/packages/metro/src/lib/parseOptionsFromUrl.js#L55-L87
736
743
  const { filename, bundle, map, ...rest } = await this.metroLoadModuleContents(filePath, opts);
737
- const scriptContents = wrapBundle(bundle);
738
744
  return {
739
745
  ...rest,
740
- src: scriptContents,
746
+ src: bundle,
741
747
  filename,
742
748
  map
743
749
  };
@@ -757,7 +763,8 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
757
763
  // NOTE(EvanBacon): This will not support any code elimination since it's a static pass.
758
764
  let { reactClientReferences: clientBoundaries, reactServerReferences: serverActionReferencesInServer, cssModules } = await this.rscRenderer.getExpoRouterClientReferencesAsync({
759
765
  platform: options.platform,
760
- domRoot: options.domRoot
766
+ domRoot: options.domRoot,
767
+ includeSourceMaps: !!options.includeSourceMaps
761
768
  }, files);
762
769
  // TODO: The output keys should be in production format or use a lookup manifest.
763
770
  const processClientBoundaries = async (reactServerReferences)=>{
@@ -786,7 +793,8 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
786
793
  const { clientBoundaries: nestedClientBoundaries } = await this.rscRenderer.exportServerActionsAsync({
787
794
  platform: options.platform,
788
795
  domRoot: options.domRoot,
789
- entryPoints: allKnownReactServerReferences
796
+ entryPoints: allKnownReactServerReferences,
797
+ includeSourceMaps: !!options.includeSourceMaps
790
798
  }, files);
791
799
  // TODO: Check against all modules in the initial client bundles.
792
800
  const hasUniqueClientBoundaries = nestedClientBoundaries.some((boundary)=>!clientBoundaries.includes(boundary));
@@ -846,7 +854,8 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
846
854
  routerOptions
847
855
  }, files);
848
856
  // Save the SSR manifest so we can perform more replacements in the server renderer and with server actions.
849
- files.set(`_expo/rsc/${options.platform}/ssr-manifest.js`, {
857
+ const manifestFilename = `_expo/rsc/${options.platform}/ssr-manifest${this.serverOutputExtension}`;
858
+ files.set(manifestFilename, {
850
859
  targetDomain: 'server',
851
860
  contents: 'module.exports = ' + JSON.stringify(// TODO: Add a less leaky version of this across the framework with just [key, value] (module ID, chunk).
852
861
  Object.fromEntries(Array.from(ssrManifest.entries()).map(([key, value])=>[
@@ -874,7 +883,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
874
883
  isExporting,
875
884
  ...options,
876
885
  environment: 'client',
877
- serializerOutput: 'static'
886
+ output: 'static'
878
887
  };
879
888
  // https://github.com/facebook/metro/blob/2405f2f6c37a1b641cc379b9c733b1eff0c1c2a1/packages/metro/src/lib/parseOptionsFromUrl.js#L55-L87
880
889
  if (!opts.mainModuleName.startsWith('/') && !_path().default.isAbsolute(opts.mainModuleName)) {
@@ -939,6 +948,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
939
948
  const configFileName = _path().default.basename(configPath);
940
949
  throw new _errors.CommandError(`The Expo Router "origin" property in the Expo config (${configFileName}) cannot be "false" when React Server Components is enabled. Remove it from the ${configFileName} file and try again.`);
941
950
  }
951
+ this.serverOutputExtension = config.pkg.type === 'module' ? '.cjs' : '.js';
942
952
  const instanceMetroOptions = {
943
953
  chunkingStrategy: (0, _metroOptions.getChunkingStrategyFromExpoConfig)(exp),
944
954
  isExporting: !!options.isExporting,
@@ -1110,6 +1120,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1110
1120
  if (isReactServerComponentsEnabled) {
1111
1121
  this.bindRSCDevModuleInjectionHandler();
1112
1122
  const rscMiddleware = (0, _createServerComponentsMiddleware.createServerComponentsMiddleware)(this.projectRoot, {
1123
+ outputExtension: this.serverOutputExtension,
1113
1124
  instanceMetroOptions: this.instanceMetroOptions,
1114
1125
  rscPath: '/_flight',
1115
1126
  ssrLoadModule: this.ssrLoadModule.bind(this),
@@ -1174,6 +1185,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1174
1185
  if (isReactServerComponentsEnabled) {
1175
1186
  this.bindRSCDevModuleInjectionHandler();
1176
1187
  const rscMiddleware = (0, _createServerComponentsMiddleware.createServerComponentsMiddleware)(this.projectRoot, {
1188
+ outputExtension: this.serverOutputExtension,
1177
1189
  instanceMetroOptions: this.instanceMetroOptions,
1178
1190
  rscPath: '/_flight',
1179
1191
  ssrLoadModule: this.ssrLoadModule.bind(this),
@@ -1345,9 +1357,15 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1345
1357
  // API Routes
1346
1358
  pendingRouteOperations = new Map();
1347
1359
  // Bundle the API Route with Metro and return the string contents to be evaluated in the server.
1348
- async bundleApiRoute(filePath, { platform }) {
1349
- if (this.pendingRouteOperations.has(filePath)) {
1350
- return this.pendingRouteOperations.get(filePath);
1360
+ async bundleApiRoute(filePath, { platform, entryFilename, includeSourceMaps }) {
1361
+ const cacheKey = JSON.stringify([
1362
+ filePath,
1363
+ platform,
1364
+ entryFilename,
1365
+ includeSourceMaps
1366
+ ]);
1367
+ if (this.pendingRouteOperations.has(cacheKey)) {
1368
+ return this.pendingRouteOperations.get(cacheKey);
1351
1369
  }
1352
1370
  const bundleAsync = async ()=>{
1353
1371
  try {
@@ -1357,7 +1375,9 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1357
1375
  });
1358
1376
  return await this.ssrLoadModuleContents(filePath, {
1359
1377
  isExporting: this.instanceMetroOptions.isExporting,
1360
- platform
1378
+ platform,
1379
+ entryFilename,
1380
+ includeSourceMaps
1361
1381
  });
1362
1382
  } catch (error) {
1363
1383
  const appDir = this.instanceMetroOptions?.routerRoot ? _path().default.join(this.projectRoot, this.instanceMetroOptions.routerRoot) : undefined;
@@ -1368,12 +1388,10 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1368
1388
  // Preserve symbol flags as well as the bundler's error details.
1369
1389
  Object.assign(err, error);
1370
1390
  throw err;
1371
- } finally{
1372
- // pendingRouteOperations.delete(filepath);
1373
1391
  }
1374
1392
  };
1375
1393
  const route = bundleAsync();
1376
- this.pendingRouteOperations.set(filePath, route);
1394
+ this.pendingRouteOperations.set(cacheKey, route);
1377
1395
  return route;
1378
1396
  }
1379
1397
  async ssrImportApiRoute(filePath, { platform }) {
@@ -1474,13 +1492,15 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1474
1492
  }
1475
1493
  /**
1476
1494
  * Bundle a loader module with Metro and return the string contents.
1477
- */ async bundleLoader(filePath, { platform }) {
1495
+ */ async bundleLoader(filePath, { platform, entryFilename, includeSourceMaps }) {
1478
1496
  try {
1479
1497
  return await this.ssrLoadModuleContents(filePath, {
1480
1498
  isExporting: this.instanceMetroOptions.isExporting,
1481
1499
  platform,
1482
1500
  environment: 'node',
1483
- isLoaderBundle: true
1501
+ isLoaderBundle: true,
1502
+ entryFilename,
1503
+ includeSourceMaps
1484
1504
  });
1485
1505
  } catch (error) {
1486
1506
  (0, _metroDebugEvents.debugEvent)('loader_bundle_failed', {
@@ -1577,9 +1597,11 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1577
1597
  }
1578
1598
  // Direct Metro access
1579
1599
  // Emulates the Metro dev server .bundle endpoint without having to go through a server.
1580
- async _bundleDirectAsync(resolvedEntryFilePath, { transformOptions, resolverOptions, graphOptions, serializerOptions }) {
1600
+ async _bundleDirectAsync(resolvedEntryFilePath, { getSourceUrl, transformOptions, resolverOptions, graphOptions, serializerOptions }) {
1581
1601
  (0, _assert().default)(this.metro, 'Metro server must be running to bundle directly.');
1582
1602
  const config = this.metro._config;
1603
+ const environment = transformOptions.customTransformOptions?.environment;
1604
+ const isServer = environment === 'node' || environment === 'react-server';
1583
1605
  const buildNumber = this.metro.getNewBuildNumber();
1584
1606
  const bundlePerfLogger = config.unstable_perfLoggerFactory?.('BUNDLING_REQUEST', {
1585
1607
  key: buildNumber
@@ -1623,7 +1645,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1623
1645
  try {
1624
1646
  // TODO: Some bug in Metro/RSC causes this to break when changing imports in server components.
1625
1647
  // We should resolve the bug because it results in ~6x faster bundling to reuse the graph revision.
1626
- if (transformOptions.customTransformOptions?.environment === 'react-server') {
1648
+ if (environment === 'react-server') {
1627
1649
  const props = await this.metro.getBundler().initializeGraph(// NOTE: Using absolute path instead of relative input path is a breaking change.
1628
1650
  // entryFile,
1629
1651
  resolvedEntryFilePath, transformOptions, resolverOptions, {
@@ -1679,6 +1701,7 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1679
1701
  runBeforeMainModule: config.serializer.getModulesRunBeforeMainModule(resolvedEntryFilePath),
1680
1702
  runModule: serializerOptions.runModule,
1681
1703
  sourceMapUrl: serializerOptions.sourceMapUrl,
1704
+ getSourceUrl,
1682
1705
  sourceUrl: serializerOptions.sourceUrl,
1683
1706
  inlineSourceMap: serializerOptions.inlineSourceMap,
1684
1707
  serverRoot: config.server.unstable_serverRoot ?? config.projectRoot,
@@ -1687,6 +1710,9 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1687
1710
  // but we have no type overrides or a proper type chain for this
1688
1711
  serializerOptions
1689
1712
  };
1713
+ if (isServer) {
1714
+ options.getRunModuleStatement = (0, _getServerRunModuleStatement().getServerRunModuleStatement)(resolvedEntryFilePath, options);
1715
+ }
1690
1716
  // TODO(@kitten): Yearns for a refactor. The typings here were and are questionable
1691
1717
  const bundle = await serializer(// NOTE: Using absolute path instead of relative input path is a breaking change.
1692
1718
  // entryFile,
@@ -1694,35 +1720,29 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1694
1720
  bundlePerfLogger?.point('serializingBundle_end');
1695
1721
  let bundleCode = null;
1696
1722
  let bundleMap = null;
1723
+ let exportResources;
1697
1724
  if (serializerOptions.output === 'static') {
1725
+ const result = isServer ? await (0, _serializeServerBundle().serializeServerBundleAsync)(bundle, resolvedEntryFilePath, revision.prepend, revision.graph, options, config) : bundle;
1726
+ let parsed;
1698
1727
  try {
1699
- const parsed = typeof bundle === 'string' ? JSON.parse(bundle) : bundle;
1700
- (0, _assert().default)('artifacts' in parsed && Array.isArray(parsed.artifacts), 'Expected serializer to return an object with key artifacts to contain an array of serial assets.');
1701
- const artifacts = parsed.artifacts;
1702
- const assets = parsed.assets;
1703
- const bundleCode = artifacts.find((asset)=>asset.type === 'js');
1704
- const bundleMap = artifacts.find((asset)=>asset.type === 'map')?.source ?? '';
1705
- const result = {
1706
- numModifiedFiles: delta.reset ? delta.added.size + revision.prepend.length : delta.added.size + delta.modified.size + delta.deleted.size,
1707
- lastModifiedDate: revision.date,
1708
- nextRevId: revision.id,
1709
- bundle: bundleCode.source,
1710
- map: bundleMap,
1711
- artifacts,
1712
- assets
1713
- };
1714
- this.metro._reporter.update({
1715
- buildID: getBuildID(buildNumber),
1716
- type: 'bundle_build_done'
1717
- });
1718
- return result;
1728
+ parsed = typeof result === 'string' ? JSON.parse(result) : result;
1729
+ (0, _assert().default)(parsed != null && typeof parsed === 'object' && Array.isArray(parsed.artifacts), 'Expected serializer to return an object with key artifacts to contain an array of serial assets.');
1719
1730
  } catch (error) {
1720
1731
  throw new Error('Serializer did not return expected format. The project copy of `expo/metro-config` may be out of date. Error: ' + error.message);
1721
1732
  }
1722
- }
1723
- if (typeof bundle === 'string') {
1733
+ const artifacts = parsed.artifacts;
1734
+ const assets = parsed.assets;
1735
+ const entry = isServer ? (0, _serverArtifacts.getServerEntryArtifact)(artifacts) : artifacts.find((asset)=>asset.type === 'js');
1736
+ bundleCode = entry.source;
1737
+ bundleMap = artifacts.find((asset)=>asset.type === 'map' && asset.filename === entry.filename + '.map')?.source ?? '';
1738
+ exportResources = {
1739
+ artifacts,
1740
+ assets
1741
+ };
1742
+ } else if (typeof bundle === 'string') {
1724
1743
  bundleCode = bundle;
1725
- // Create the source map in a second pass...
1744
+ // Legacy string serializers omit the map, so recover it from the graph. Expo's serializer
1745
+ // returns its finished map with the bundle and does not need this compatibility path.
1726
1746
  let { prepend, graph } = revision;
1727
1747
  if (serializerOptions.modulesOnly) {
1728
1748
  prepend = [];
@@ -1733,6 +1753,8 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1733
1753
  ...this.metro._getSortedModules(graph)
1734
1754
  ], {
1735
1755
  excludeSource: serializerOptions.excludeSource,
1756
+ flatten: isServer,
1757
+ getSourceUrl,
1736
1758
  processModuleFilter: config.serializer.processModuleFilter,
1737
1759
  shouldAddToIgnoreList
1738
1760
  });
@@ -1745,7 +1767,8 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1745
1767
  lastModifiedDate: revision.date,
1746
1768
  nextRevId: revision.id,
1747
1769
  bundle: bundleCode,
1748
- map: bundleMap
1770
+ map: bundleMap,
1771
+ ...exportResources
1749
1772
  };
1750
1773
  this.metro._reporter.update({
1751
1774
  buildID: getBuildID(buildNumber),
@@ -1794,12 +1817,6 @@ class MetroBundlerDevServer extends _BundlerDevServer.BundlerDevServer {
1794
1817
  function getBuildID(buildNumber) {
1795
1818
  return buildNumber.toString(36);
1796
1819
  }
1797
- function wrapBundle(str) {
1798
- // Skip the metro runtime so debugging is a bit easier.
1799
- // Replace the __r() call with an export statement.
1800
- // Use gm to apply to the last require line. This is needed when the bundle has side-effects.
1801
- return str.replace(/^(__r\(.*\);)$/gm, 'module.exports = $1');
1802
- }
1803
1820
  function unique(array) {
1804
1821
  return Array.from(new Set(array));
1805
1822
  }