@@ -14,6 +14,7 @@ import {
1414 APP_PATH ,
1515 DEFAULT_THEME_PATH ,
1616 DIST_CLIENT_PATH ,
17+ SITE_DATA_ID ,
1718 SITE_DATA_REQUEST_PATH ,
1819 resolveAliases
1920} from './alias'
@@ -43,7 +44,8 @@ declare module 'vite' {
4344 }
4445}
4546
46- const themeRE = / \/ \. v i t e p r e s s \/ t h e m e \/ i n d e x \. ( m | c ) ? ( j | t ) s $ /
47+ const themeRE = / (?: ^ | \/ ) \. v i t e p r e s s \/ t h e m e \/ i n d e x \. ( m | c ) ? ( j | t ) s $ /
48+ const startsWithThemeRE = / ^ @ t h e m e (?: \/ | $ ) /
4749const docsearchRE = / \/ @ d o c s e a r c h \/ c s s \/ d i s t \/ s t y l e .c s s (?: $ | \? ) /
4850
4951const hashRE = / \. ( [ - \w ] + ) \. j s $ /
@@ -131,7 +133,7 @@ export async function createVitePressPlugin(
131133 config ( ) {
132134 const baseConfig : UserConfig = {
133135 resolve : {
134- alias : resolveAliases ( siteConfig , ssr )
136+ alias : resolveAliases ( siteConfig . root , ssr )
135137 } ,
136138 define : {
137139 __VP_LOCAL_SEARCH__ : site . themeConfig ?. search ?. provider === 'local' ,
@@ -147,10 +149,7 @@ export async function createVitePressPlugin(
147149 include : [
148150 'vue' ,
149151 'vitepress > @vue/devtools-api' ,
150- 'vitepress > @vueuse/core' ,
151- siteConfig . themeDir === DEFAULT_THEME_PATH
152- ? '@theme/index'
153- : undefined
152+ 'vitepress > @vueuse/core'
154153 ] . filter ( ( d ) => d != null ) ,
155154 exclude : [ '@docsearch/js' , 'vitepress' ]
156155 } ,
@@ -170,10 +169,17 @@ export async function createVitePressPlugin(
170169 : baseConfig
171170 } ,
172171
173- resolveId ( id ) {
174- if ( id === SITE_DATA_REQUEST_PATH ) {
172+ resolveId ( id , importer , resolveOptions ) {
173+ if ( id === SITE_DATA_ID ) {
175174 return SITE_DATA_REQUEST_PATH
176175 }
176+ if ( startsWithThemeRE . test ( id ) ) {
177+ return this . resolve (
178+ siteConfig . themeDir + id . slice ( 6 ) ,
179+ importer ,
180+ Object . assign ( { skipSelf : true } , resolveOptions )
181+ )
182+ }
177183 } ,
178184
179185 load ( id ) {
@@ -260,31 +266,6 @@ export async function createVitePressPlugin(
260266 configDeps . forEach ( ( file ) => server . watcher . add ( file ) )
261267 }
262268
263- const onFileAddDelete = async ( added : boolean , file : string ) => {
264- const relativePath = path . posix . relative ( srcDir , file )
265- // restart server on theme file creation / deletion
266- if ( themeRE . test ( relativePath ) ) {
267- siteConfig . logger . info (
268- c . green (
269- `${ relativePath } ${ added ? 'created' : 'deleted' } , restarting server...\n`
270- ) ,
271- { clear : true , timestamp : true }
272- )
273-
274- await recreateServer ?.( )
275- }
276-
277- // update pages, dynamicRoutes and rewrites on md file creation / deletion
278- if ( relativePath . endsWith ( '.md' ) ) await resolvePages ( siteConfig )
279-
280- if ( ! added && importerMap [ relativePath ] ) {
281- delete importerMap [ relativePath ]
282- }
283- }
284- server . watcher
285- . on ( 'add' , onFileAddDelete . bind ( null , true ) )
286- . on ( 'unlink' , onFileAddDelete . bind ( null , false ) )
287-
288269 // serve our index.html after vite history fallback
289270 return ( ) => {
290271 server . middlewares . use ( async ( req , res , next ) => {
@@ -369,8 +350,30 @@ export async function createVitePressPlugin(
369350 }
370351 } ,
371352
372- async hotUpdate ( { file } ) {
353+ async hotUpdate ( { file, type } ) {
373354 if ( this . environment . name !== 'client' ) return
355+ const relativePath = path . posix . relative ( srcDir , file )
356+
357+ if ( themeRE . test ( relativePath ) && type !== 'update' ) {
358+ siteConfig . themeDir =
359+ type === 'create' ? path . posix . dirname ( file ) : DEFAULT_THEME_PATH
360+ siteConfig . logger . info ( c . green ( 'page reload ' ) + c . dim ( relativePath ) , {
361+ clear : true ,
362+ timestamp : true
363+ } )
364+ this . environment . moduleGraph . invalidateAll ( )
365+ this . environment . hot . send ( { type : 'full-reload' } )
366+ return [ ]
367+ }
368+
369+ // update pages, dynamicRoutes and rewrites on md file creation / deletion
370+ if ( file . endsWith ( '.md' ) && type !== 'update' ) {
371+ await resolvePages ( siteConfig )
372+ }
373+
374+ if ( type === 'delete' ) {
375+ delete importerMap [ relativePath ]
376+ }
374377
375378 if (
376379 file === configPath ||
0 commit comments