Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-bears-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rnef/plugin-metro': minor
---

Allow configuring `getModulesRunBeforeMainModule` through Metro config
47 changes: 32 additions & 15 deletions packages/plugin-metro/src/lib/start/loadMetroConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
},
},
};
}
Expand Down