From df25d992c48ec2e383769425b1016f97286d90f8 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Thu, 9 May 2024 13:16:22 +1000 Subject: [PATCH] Clean redundant `?client-route=1` usage in dev --- packages/remix-dev/vite/plugin.ts | 47 +++++++++++++------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 0576916476..6990a94e1a 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -121,9 +121,11 @@ const CLIENT_ROUTE_EXPORTS = [ "shouldRevalidate", ]; -// The "=1" suffix ensures client route requests can be processed before hitting -// the Vite plugin since "?client-route" can be serialized as "?client-route=" -const CLIENT_ROUTE_QUERY_STRING = "?client-route=1"; +/** This is used to manage a build optimization to remove unused route exports +from the client build output. This is important in cases where custom route +exports are only ever used on the server. Without this optimization we can't +tree-shake any unused custom exports because routes are entry points. */ +const BUILD_CLIENT_ROUTE_QUERY_STRING = "?__remix-build-client-route"; export type ServerBundleBuildConfig = { routes: RouteManifest; @@ -185,10 +187,6 @@ const getHash = (source: BinaryLike, maxLength?: number): string => { return typeof maxLength === "number" ? hash.slice(0, maxLength) : hash; }; -const isClientRoute = (id: string): boolean => { - return id.endsWith(CLIENT_ROUTE_QUERY_STRING); -}; - const resolveChunk = ( ctx: ReactRouterPluginContext, viteManifest: Vite.Manifest, @@ -199,7 +197,7 @@ const resolveChunk = ( path.relative(ctx.rootDirectory, absoluteFilePath) ); let entryChunk = - viteManifest[rootRelativeFilePath + CLIENT_ROUTE_QUERY_STRING] ?? + viteManifest[rootRelativeFilePath + BUILD_CLIENT_ROUTE_QUERY_STRING] ?? viteManifest[rootRelativeFilePath]; if (!entryChunk) { @@ -673,7 +671,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { `${resolveFileUrl( ctx, resolveRelativeRouteFilePath(route, ctx.reactRouterConfig) - )}${CLIENT_ROUTE_QUERY_STRING}` + )}` ), hasAction: sourceExports.includes("action"), hasLoader: sourceExports.includes("loader"), @@ -848,7 +846,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { `${path.resolve( ctx.reactRouterConfig.appDirectory, route.file - )}${CLIENT_ROUTE_QUERY_STRING}` + )}${BUILD_CLIENT_ROUTE_QUERY_STRING}` ), ], }, @@ -981,8 +979,8 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { cssModulesManifest[id] = code; } - if (isClientRoute(id)) { - let routeModuleId = id.replace(CLIENT_ROUTE_QUERY_STRING, ""); + if (id.endsWith(BUILD_CLIENT_ROUTE_QUERY_STRING)) { + let routeModuleId = id.replace(BUILD_CLIENT_ROUTE_QUERY_STRING, ""); let sourceExports = await getRouteModuleExports( viteChildCompiler, ctx, @@ -1414,10 +1412,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = (_config) => { let useFastRefresh = !ssr && (isJSX || code.includes(devRuntime)); if (!useFastRefresh) return; - if (isClientRoute(id)) { - return { code: addRefreshWrapper(ctx.reactRouterConfig, code, id) }; - } - let result = await babel.transformAsync(code, { babelrc: false, configFile: false, @@ -1514,17 +1508,16 @@ function addRefreshWrapper( id: string ): string { let route = getRoute(reactRouterConfig, id); - let acceptExports = - route || isClientRoute(id) - ? [ - "clientAction", - "clientLoader", - "handle", - "meta", - "links", - "shouldRevalidate", - ] - : []; + let acceptExports = route + ? [ + "clientAction", + "clientLoader", + "handle", + "meta", + "links", + "shouldRevalidate", + ] + : []; return ( REACT_REFRESH_HEADER.replaceAll("__SOURCE__", JSON.stringify(id)) + code +