@@ -130,12 +130,11 @@ const CLIENT_ROUTE_EXPORTS = [
130130 "shouldRevalidate" ,
131131] ;
132132
133- // Each route gets its own virtual module marked with an entry query string
134- const ROUTE_ENTRY_QUERY_STRING = "?route-entry=1" ;
135-
136- const isRouteEntry = ( id : string ) : boolean => {
137- return id . endsWith ( ROUTE_ENTRY_QUERY_STRING ) ;
138- } ;
133+ /** This is used to manage a build optimization to remove unused route exports
134+ from the client build output. This is important in cases where custom route
135+ exports are only ever used on the server. Without this optimization we can't
136+ tree-shake any unused custom exports because routes are entry points. */
137+ const BUILD_CLIENT_ROUTE_QUERY_STRING = "?__react-router-build-client-route" ;
139138
140139export type ServerBundleBuildConfig = {
141140 routes : RouteManifest ;
@@ -207,7 +206,7 @@ const resolveChunk = (
207206 path . relative ( ctx . rootDirectory , absoluteFilePath )
208207 ) ;
209208 let entryChunk =
210- viteManifest [ rootRelativeFilePath + ROUTE_ENTRY_QUERY_STRING ] ??
209+ viteManifest [ rootRelativeFilePath + BUILD_CLIENT_ROUTE_QUERY_STRING ] ??
211210 viteManifest [ rootRelativeFilePath ] ;
212211
213212 if ( ! entryChunk ) {
@@ -508,7 +507,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
508507 resolveFileUrl (
509508 ctx ,
510509 resolveRelativeRouteFilePath ( route , ctx . reactRouterConfig )
511- ) + ROUTE_ENTRY_QUERY_STRING
510+ )
512511 ) } ;`;
513512 } )
514513 . join ( "\n" ) }
@@ -694,10 +693,10 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
694693 caseSensitive : route . caseSensitive ,
695694 module : combineURLs (
696695 ctx . publicPath ,
697- ` ${ resolveFileUrl (
696+ resolveFileUrl (
698697 ctx ,
699698 resolveRelativeRouteFilePath ( route , ctx . reactRouterConfig )
700- ) } ${ ROUTE_ENTRY_QUERY_STRING } `
699+ )
701700 ) ,
702701 hasAction : sourceExports . includes ( "action" ) ,
703702 hasLoader : sourceExports . includes ( "loader" ) ,
@@ -886,7 +885,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
886885 `${ path . resolve (
887886 ctx . reactRouterConfig . appDirectory ,
888887 route . file
889- ) } ${ ROUTE_ENTRY_QUERY_STRING } `
888+ ) } ${ BUILD_CLIENT_ROUTE_QUERY_STRING } `
890889 ) ,
891890 ] ,
892891 } ,
@@ -1251,11 +1250,11 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
12511250 } ,
12521251 } ,
12531252 {
1254- name : "react-router:route-entry " ,
1253+ name : "react-router:build-client-route " ,
12551254 enforce : "pre" ,
12561255 async transform ( _code , id , options ) {
1257- if ( ! isRouteEntry ( id ) ) return ;
1258- let routeModuleId = id . replace ( ROUTE_ENTRY_QUERY_STRING , "" ) ;
1256+ if ( ! id . endsWith ( BUILD_CLIENT_ROUTE_QUERY_STRING ) ) return ;
1257+ let routeModuleId = id . replace ( BUILD_CLIENT_ROUTE_QUERY_STRING , "" ) ;
12591258
12601259 let routeFileName = path . basename ( routeModuleId ) ;
12611260
@@ -1516,10 +1515,6 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
15161515 let useFastRefresh = ! ssr && ( isJSX || code . includes ( devRuntime ) ) ;
15171516 if ( ! useFastRefresh ) return ;
15181517
1519- if ( isRouteEntry ( id ) ) {
1520- return { code : addRefreshWrapper ( ctx . reactRouterConfig , code , id ) } ;
1521- }
1522-
15231518 let result = await babel . transformAsync ( code , {
15241519 babelrc : false ,
15251520 configFile : false ,
@@ -1615,17 +1610,16 @@ function addRefreshWrapper(
16151610 id : string
16161611) : string {
16171612 let route = getRoute ( reactRouterConfig , id ) ;
1618- let acceptExports =
1619- route || isRouteEntry ( id )
1620- ? [
1621- "clientAction" ,
1622- "clientLoader" ,
1623- "handle" ,
1624- "meta" ,
1625- "links" ,
1626- "shouldRevalidate" ,
1627- ]
1628- : [ ] ;
1613+ let acceptExports = route
1614+ ? [
1615+ "clientAction" ,
1616+ "clientLoader" ,
1617+ "handle" ,
1618+ "meta" ,
1619+ "links" ,
1620+ "shouldRevalidate" ,
1621+ ]
1622+ : [ ] ;
16291623 return (
16301624 "\n\n" +
16311625 withCommentBoundaries (
0 commit comments