@@ -4,8 +4,10 @@ import { getReferenceRouteCompilerPlugins } from './code-splitter/plugins/framew
44import { createRouteHmrStatement } from './hmr'
55import { debug , normalizePath } from './utils'
66import { getConfig } from './config'
7+ import { createRouterPluginContext } from './router-plugin-context'
78import type { UnpluginFactory } from 'unplugin'
89import type { Config } from './config'
10+ import type { RouterPluginContext } from './router-plugin-context'
911
1012/**
1113 * This plugin adds HMR support for file routes.
@@ -18,11 +20,18 @@ const includeCode = [
1820 'createRootRoute(' ,
1921 'createRootRouteWithContext(' ,
2022]
21- export const unpluginRouterHmrFactory : UnpluginFactory <
22- Partial < Config > | undefined
23- > = ( options = { } ) => {
23+
24+ export function createRouterHmrPlugin (
25+ options : Partial < Config | ( ( ) => Config ) > | undefined = { } ,
26+ routerPluginContext : RouterPluginContext ,
27+ ) : ReturnType < UnpluginFactory < Partial < Config > | undefined > > {
2428 let ROOT : string = process . cwd ( )
25- let userConfig = options as Config
29+
30+ const resolveUserConfig = ( ) => {
31+ return getConfig ( typeof options === 'function' ? options ( ) : options , ROOT )
32+ }
33+
34+ let userConfig = resolveUserConfig ( )
2635
2736 return {
2837 name : 'tanstack-router:hmr' ,
@@ -37,7 +46,7 @@ export const unpluginRouterHmrFactory: UnpluginFactory<
3746 } ,
3847 handler ( code , id ) {
3948 const normalizedId = normalizePath ( id )
40- const routeEntry = globalThis . TSR_ROUTES_BY_ID_MAP ? .get ( normalizedId )
49+ const routeEntry = routerPluginContext . routesByFile . get ( normalizedId )
4150 if ( ! routeEntry ) {
4251 return null
4352 }
@@ -97,7 +106,7 @@ export const unpluginRouterHmrFactory: UnpluginFactory<
97106 vite : {
98107 configResolved ( config ) {
99108 ROOT = config . root
100- userConfig = getConfig ( options , ROOT )
109+ userConfig = resolveUserConfig ( )
101110 } ,
102111 applyToEnvironment ( environment ) {
103112 if ( userConfig . plugin ?. vite ?. environmentName ) {
@@ -108,3 +117,9 @@ export const unpluginRouterHmrFactory: UnpluginFactory<
108117 } ,
109118 }
110119}
120+
121+ export const unpluginRouterHmrFactory : UnpluginFactory <
122+ Partial < Config > | undefined
123+ > = ( options = { } ) => {
124+ return createRouterHmrPlugin ( options , createRouterPluginContext ( ) )
125+ }
0 commit comments