diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 0aa7f27be3..c9cef54c7e 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -1048,24 +1048,26 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { : "custom", ssr: { - external: [ - // This is only necessary for development within this repo - // because these packages are symlinked and Vite treats them as - // internal source code. For consumers this is a no-op. - "react-router", - "react-router-dom", - "@react-router/architect", - "@react-router/cloudflare-pages", - "@react-router/cloudflare-workers", - "@react-router/cloudflare", - "@react-router/deno", - "@react-router/dev", - "@react-router/express", - "@react-router/netlify", - "@react-router/node", - "@react-router/serve", - "@react-router/server-runtime", - ], + external: isInReactRouterMonorepo() + ? [ + // This is only needed within this repo because these packages + // are linked to a directory outside of node_modules so Vite + // treats them as internal code by default. + "react-router", + "react-router-dom", + "@react-router/architect", + "@react-router/cloudflare-pages", + "@react-router/cloudflare-workers", + "@react-router/cloudflare", + "@react-router/deno", + "@react-router/dev", + "@react-router/express", + "@react-router/netlify", + "@react-router/node", + "@react-router/serve", + "@react-router/server-runtime", + ] + : undefined, }, optimizeDeps: { include: [ @@ -1779,6 +1781,18 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { ]; }; +function isInReactRouterMonorepo() { + // We use '@react-router/server-runtime' for this check since it's a + // dependency of this package and guaranteed to be in node_modules + let serverRuntimePath = path.dirname( + require.resolve("@react-router/server-runtime/package.json") + ); + let serverRuntimeParentDir = path.basename( + path.resolve(serverRuntimePath, "..") + ); + return serverRuntimeParentDir === "packages"; +} + function isEqualJson(v1: unknown, v2: unknown) { return JSON.stringify(v1) === JSON.stringify(v2); }