From 18c757bad9a8dc76fed817b1abb371f0515ae5ad Mon Sep 17 00:00:00 2001 From: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Date: Mon, 7 Aug 2023 14:35:28 -0700 Subject: [PATCH] Resolve npmPackageName from project root not local paths (#2588) --- .changeset/bright-otters-type.md | 6 ++++++ packages/metro-config/src/defaultConfig.js | 16 ++++++++++++---- packages/metro-service/src/config.ts | 3 ++- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 .changeset/bright-otters-type.md diff --git a/.changeset/bright-otters-type.md b/.changeset/bright-otters-type.md new file mode 100644 index 000000000..98ea956c0 --- /dev/null +++ b/.changeset/bright-otters-type.md @@ -0,0 +1,6 @@ +--- +"@rnx-kit/metro-service": patch +"@rnx-kit/metro-config": patch +--- + +Resolve npmPackageName from project root not local paths diff --git a/packages/metro-config/src/defaultConfig.js b/packages/metro-config/src/defaultConfig.js index 8eb6a9fec..8cb042ee6 100644 --- a/packages/metro-config/src/defaultConfig.js +++ b/packages/metro-config/src/defaultConfig.js @@ -13,17 +13,25 @@ const path = require("path"); /** * @param {PlatformImplementations} availablePlatforms + * @param {string} projectRoot */ -function getPreludeModules(availablePlatforms) { +function getPreludeModules(availablePlatforms, projectRoot) { // Include all instances of `InitializeCore` here and let Metro exclude // the unused ones. + const requireOptions = { paths: [projectRoot] }; const mainModules = new Set([ - require.resolve("react-native/Libraries/Core/InitializeCore"), + require.resolve( + "react-native/Libraries/Core/InitializeCore", + requireOptions + ), ]); for (const moduleName of Object.values(availablePlatforms)) { if (moduleName) { mainModules.add( - require.resolve(`${moduleName}/Libraries/Core/InitializeCore`) + require.resolve( + `${moduleName}/Libraries/Core/InitializeCore`, + requireOptions + ) ); } } @@ -106,7 +114,7 @@ function getDefaultConfig(projectRoot) { defaultConfig.resolver.resolveRequest = outOfTreePlatformResolver(availablePlatforms); - const preludeModules = getPreludeModules(availablePlatforms); + const preludeModules = getPreludeModules(availablePlatforms, projectRoot); defaultConfig.serializer.getModulesRunBeforeMainModule = () => { return preludeModules; }; diff --git a/packages/metro-service/src/config.ts b/packages/metro-service/src/config.ts index dcda1a9e9..c30011f07 100644 --- a/packages/metro-service/src/config.ts +++ b/packages/metro-service/src/config.ts @@ -124,7 +124,8 @@ function getDefaultConfigInternal(cliConfig: CLIConfig): InputConfigT { require.resolve( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion `${cliConfig.platforms[platform] - .npmPackageName!}/Libraries/Core/InitializeCore` + .npmPackageName!}/Libraries/Core/InitializeCore`, + { paths: [cliConfig.root] } ) ), ],