@@ -39,7 +39,11 @@ import type { Cache } from "./cache";
3939import { generate , parse } from "./babel" ;
4040import type { NodeRequestHandler } from "./node-adapter" ;
4141import { fromNodeRequest , toNodeRequest } from "./node-adapter" ;
42- import { getStylesForPathname , isCssModulesFile } from "./styles" ;
42+ import {
43+ getCssStringFromViteDevModuleCode ,
44+ getStylesForPathname ,
45+ isCssModulesFile ,
46+ } from "./styles" ;
4347import * as VirtualModule from "./virtual-module" ;
4448import { resolveFileUrl } from "./resolve-file-url" ;
4549import { combineURLs } from "./combine-urls" ;
@@ -136,10 +140,7 @@ exports are only ever used on the server. Without this optimization we can't
136140tree-shake any unused custom exports because routes are entry points. */
137141const BUILD_CLIENT_ROUTE_QUERY_STRING = "?__react-router-build-client-route" ;
138142
139- export type EnvironmentName =
140- | "client"
141- | SsrEnvironmentName
142- | CssDevHelperEnvironmentName ;
143+ export type EnvironmentName = "client" | SsrEnvironmentName ;
143144
144145const SSR_BUNDLE_PREFIX = "ssrBundle_" ;
145146type SsrBundleEnvironmentName = `${typeof SSR_BUNDLE_PREFIX } ${string } `;
@@ -151,16 +152,6 @@ function isSsrBundleEnvironmentName(
151152 return name . startsWith ( SSR_BUNDLE_PREFIX ) ;
152153}
153154
154- // We use a separate environment for loading the critical CSS during
155- // development. This is because "ssrLoadModule" isn't available if the "ssr"
156- // environment has been defined by another plugin (e.g.
157- // vite-plugin-cloudflare) as a custom Vite.DevEnvironment rather than a
158- // Vite.RunnableDevEnvironment:
159- // https://vite.dev/guide/api-environment-frameworks.html#runtime-agnostic-ssr
160- const CSS_DEV_HELPER_ENVIRONMENT_NAME =
161- "__react_router_css_dev_helper__" as const ;
162- type CssDevHelperEnvironmentName = typeof CSS_DEV_HELPER_ENVIRONMENT_NAME ;
163-
164155type EnvironmentOptions = Pick < Vite . EnvironmentOptions , "build" | "resolve" > ;
165156
166157type EnvironmentOptionsResolver = ( options : {
@@ -1121,40 +1112,20 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
11211112 return cssModulesManifest [ dep . file ] ;
11221113 }
11231114
1124- const vite = getVite ( ) ;
1125- const viteMajor = parseInt ( vite . version . split ( "." ) [ 0 ] , 10 ) ;
1126-
1127- const url =
1128- viteMajor >= 6
1129- ? // We need the ?inline query in Vite v6 when loading CSS in SSR
1130- // since it does not expose the default export for CSS in a
1131- // server environment. This is to align with non-SSR
1132- // environments. For backwards compatibility with v5 we keep
1133- // using the URL without ?inline query because the HMR code was
1134- // relying on the implicit SSR-client module graph relationship.
1135- injectQuery ( dep . url , "inline" )
1136- : dep . url ;
1137-
1138- let cssMod : unknown ;
1139- if ( ctx . reactRouterConfig . future . unstable_viteEnvironmentApi ) {
1140- const cssDevHelperEnvironment =
1141- viteDevServer . environments [ CSS_DEV_HELPER_ENVIRONMENT_NAME ] ;
1142- invariant ( cssDevHelperEnvironment , "Missing CSS dev helper environment" ) ;
1143- invariant ( vite . isRunnableDevEnvironment ( cssDevHelperEnvironment ) ) ;
1144- cssMod = await cssDevHelperEnvironment . runner . import ( url ) ;
1145- } else {
1146- cssMod = await viteDevServer . ssrLoadModule ( url ) ;
1147- }
1148-
1115+ let transformedCssCode = ( await viteDevServer . transformRequest ( dep . url ) )
1116+ ?. code ;
11491117 invariant (
1150- typeof cssMod === "object" &&
1151- cssMod !== null &&
1152- "default" in cssMod &&
1153- typeof cssMod . default === "string" ,
1118+ transformedCssCode ,
11541119 `Failed to load CSS for ${ dep . file ?? dep . url } `
11551120 ) ;
11561121
1157- return cssMod . default ;
1122+ let cssString = getCssStringFromViteDevModuleCode ( transformedCssCode ) ;
1123+ invariant (
1124+ typeof cssString === "string" ,
1125+ `Failed to extract CSS for ${ dep . file ?? dep . url } `
1126+ ) ;
1127+
1128+ return cssString ;
11581129 } ;
11591130
11601131 return [
@@ -3593,13 +3564,6 @@ export async function getEnvironmentOptionsResolvers(
35933564 } ) ;
35943565 }
35953566
3596- if (
3597- ctx . reactRouterConfig . future . unstable_viteEnvironmentApi &&
3598- viteCommand === "serve"
3599- ) {
3600- environmentOptionsResolvers [ CSS_DEV_HELPER_ENVIRONMENT_NAME ] = ( ) => ( { } ) ;
3601- }
3602-
36033567 return environmentOptionsResolvers ;
36043568}
36053569
0 commit comments