diff --git a/.changeset/twenty-bears-sleep.md b/.changeset/twenty-bears-sleep.md new file mode 100644 index 000000000..4299eec74 --- /dev/null +++ b/.changeset/twenty-bears-sleep.md @@ -0,0 +1,5 @@ +--- +'@rnef/plugin-metro': minor +--- + +Allow configuring `getModulesRunBeforeMainModule` through Metro config diff --git a/packages/plugin-metro/src/lib/start/loadMetroConfig.ts b/packages/plugin-metro/src/lib/start/loadMetroConfig.ts index 9ac2d0037..d435c3f84 100644 --- a/packages/plugin-metro/src/lib/start/loadMetroConfig.ts +++ b/packages/plugin-metro/src/lib/start/loadMetroConfig.ts @@ -49,24 +49,41 @@ function getOverrideConfig( } const require = createRequire(import.meta.url); + + // We can include multiple copies of InitializeCore here because metro will + // only add ones that are already part of the bundle + const initializeCorePaths = [ + require.resolve( + path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'), + { paths: [ctx.root] } + ), + ...outOfTreePlatforms.map((platform) => + require.resolve( + // @ts-expect-error - TBD + `${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`, + { paths: [ctx.root] } + ) + ), + ]; + return { resolver, serializer: { - // We can include multiple copies of InitializeCore here because metro will - // only add ones that are already part of the bundle - getModulesRunBeforeMainModule: () => [ - require.resolve( - path.join(ctx.reactNativePath, 'Libraries/Core/InitializeCore'), - { paths: [ctx.root] } - ), - ...outOfTreePlatforms.map((platform) => - require.resolve( - // @ts-expect-error - TBD - `${ctx.platforms[platform].npmPackageName}/Libraries/Core/InitializeCore`, - { paths: [ctx.root] } - ) - ), - ], + getModulesRunBeforeMainModule: (entrypoint) => { + const originalBeforeMainModules = + config.serializer.getModulesRunBeforeMainModule(entrypoint); + + // If the InitializeCore path is not specified in the original config, + // add it to the front of the list + const beforeMainModules = []; + for (const modulePath of initializeCorePaths) { + if (!originalBeforeMainModules.includes(modulePath)) { + beforeMainModules.push(modulePath); + } + } + + return [...beforeMainModules, ...originalBeforeMainModules]; + }, }, }; }