@@ -39,7 +39,11 @@ import type { Cache } from "./cache";
39
39
import { generate , parse } from "./babel" ;
40
40
import type { NodeRequestHandler } from "./node-adapter" ;
41
41
import { fromNodeRequest , toNodeRequest } from "./node-adapter" ;
42
- import { getStylesForPathname , isCssModulesFile } from "./styles" ;
42
+ import {
43
+ getCssStringFromViteDevModuleCode ,
44
+ getStylesForPathname ,
45
+ isCssModulesFile ,
46
+ } from "./styles" ;
43
47
import * as VirtualModule from "./virtual-module" ;
44
48
import { resolveFileUrl } from "./resolve-file-url" ;
45
49
import { combineURLs } from "./combine-urls" ;
@@ -136,10 +140,7 @@ exports are only ever used on the server. Without this optimization we can't
136
140
tree-shake any unused custom exports because routes are entry points. */
137
141
const BUILD_CLIENT_ROUTE_QUERY_STRING = "?__react-router-build-client-route" ;
138
142
139
- export type EnvironmentName =
140
- | "client"
141
- | SsrEnvironmentName
142
- | CssDevHelperEnvironmentName ;
143
+ export type EnvironmentName = "client" | SsrEnvironmentName ;
143
144
144
145
const SSR_BUNDLE_PREFIX = "ssrBundle_" ;
145
146
type SsrBundleEnvironmentName = `${typeof SSR_BUNDLE_PREFIX } ${string } `;
@@ -151,16 +152,6 @@ function isSsrBundleEnvironmentName(
151
152
return name . startsWith ( SSR_BUNDLE_PREFIX ) ;
152
153
}
153
154
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
-
164
155
type EnvironmentOptions = Pick < Vite . EnvironmentOptions , "build" | "resolve" > ;
165
156
166
157
type EnvironmentOptionsResolver = ( options : {
@@ -1121,40 +1112,20 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
1121
1112
return cssModulesManifest [ dep . file ] ;
1122
1113
}
1123
1114
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 ;
1149
1117
invariant (
1150
- typeof cssMod === "object" &&
1151
- cssMod !== null &&
1152
- "default" in cssMod &&
1153
- typeof cssMod . default === "string" ,
1118
+ transformedCssCode ,
1154
1119
`Failed to load CSS for ${ dep . file ?? dep . url } `
1155
1120
) ;
1156
1121
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 ;
1158
1129
} ;
1159
1130
1160
1131
return [
@@ -3591,13 +3562,6 @@ export async function getEnvironmentOptionsResolvers(
3591
3562
} ) ;
3592
3563
}
3593
3564
3594
- if (
3595
- ctx . reactRouterConfig . future . unstable_viteEnvironmentApi &&
3596
- viteCommand === "serve"
3597
- ) {
3598
- environmentOptionsResolvers [ CSS_DEV_HELPER_ENVIRONMENT_NAME ] = ( ) => ( { } ) ;
3599
- }
3600
-
3601
3565
return environmentOptionsResolvers ;
3602
3566
}
3603
3567
0 commit comments