11import type fsMod from 'node:fs' ;
22import { extname } from 'node:path' ;
3- import { fileURLToPath } from 'node:url' ;
3+ import { fileURLToPath , pathToFileURL } from 'node:url' ;
44import { bold } from 'kleur/colors' ;
5- import type { Plugin } from 'vite' ;
5+ import { normalizePath , type Plugin , type ViteDevServer } from 'vite' ;
66import { serializeRouteData } from '../core/app/index.js' ;
77import type { SerializedRouteInfo } from '../core/app/types.js' ;
88import { warnMissingAdapter } from '../core/dev/adapter-validation.js' ;
99import type { Logger } from '../core/logger/core.js' ;
1010import { createRoutesList } from '../core/routing/index.js' ;
1111import { getRoutePrerenderOption } from '../core/routing/manifest/prerender.js' ;
1212import { isEndpoint , isPage } from '../core/util.js' ;
13- import { normalizePath , rootRelativePath } from '../core/viteUtils.js' ;
13+ import { rootRelativePath } from '../core/viteUtils.js' ;
1414import type { AstroSettings } from '../types/astro.js' ;
1515import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js' ;
1616import type { PluginMetadata } from '../vite-plugin-astro/types.js' ;
@@ -56,8 +56,13 @@ export default async function astroPluginRoutes({
5656 } ,
5757 ) ;
5858
59- async function rebuildRoutes ( path : string | null = null ) {
60- if ( path != null ) {
59+ async function rebuildRoutes ( path : string | null = null , server : ViteDevServer ) {
60+ if ( path != null && path . startsWith ( settings . config . srcDir . pathname ) ) {
61+ logger . debug (
62+ 'update' ,
63+ `Re-calculating routes for ${ path . slice ( settings . config . srcDir . pathname . length ) } ` ,
64+ ) ;
65+ const file = pathToFileURL ( normalizePath ( path ) ) ;
6166 routeList = await createRoutesList (
6267 {
6368 settings,
@@ -70,21 +75,26 @@ export default async function astroPluginRoutes({
7075
7176 serializedRouteInfo = routeList . routes . map ( ( r ) : SerializedRouteInfo => {
7277 return {
73- file : '' ,
78+ file : fileURLToPath ( file ) ,
7479 links : [ ] ,
7580 scripts : [ ] ,
7681 styles : [ ] ,
7782 routeData : serializeRouteData ( r , settings . config . trailingSlash ) ,
7883 } ;
7984 } ) ;
85+ let environment = server . environments . ssr ;
86+ const virtualMod = environment . moduleGraph . getModuleById ( ASTRO_ROUTES_MODULE_ID_RESOLVED ) ;
87+ if ( ! virtualMod ) return ;
88+
89+ environment . moduleGraph . invalidateModule ( virtualMod ) ;
8090 }
8191 }
8292 return {
8393 name : 'astro:routes' ,
8494 configureServer ( server ) {
85- server . watcher . on ( 'add' , rebuildRoutes . bind ( null , null ) ) ;
86- server . watcher . on ( 'unlink' , rebuildRoutes . bind ( null , null ) ) ;
87- server . watcher . on ( 'change' , rebuildRoutes ) ;
95+ server . watcher . on ( 'add' , ( path ) => rebuildRoutes ( path , server ) ) ;
96+ server . watcher . on ( 'unlink' , ( path ) => rebuildRoutes ( path , server ) ) ;
97+ server . watcher . on ( 'change' , ( path ) => rebuildRoutes ( path , server ) ) ;
8898 } ,
8999
90100 applyToEnvironment ( environment ) {
0 commit comments