@@ -14,6 +14,7 @@ import {
14
14
APP_PATH ,
15
15
DEFAULT_THEME_PATH ,
16
16
DIST_CLIENT_PATH ,
17
+ SITE_DATA_ID ,
17
18
SITE_DATA_REQUEST_PATH ,
18
19
resolveAliases
19
20
} from './alias'
@@ -43,7 +44,8 @@ declare module 'vite' {
43
44
}
44
45
}
45
46
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 (?: \/ | $ ) /
47
49
const 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 (?: $ | \? ) /
48
50
49
51
const hashRE = / \. ( [ - \w ] + ) \. j s $ /
@@ -131,7 +133,7 @@ export async function createVitePressPlugin(
131
133
config ( ) {
132
134
const baseConfig : UserConfig = {
133
135
resolve : {
134
- alias : resolveAliases ( siteConfig , ssr )
136
+ alias : resolveAliases ( siteConfig . root , ssr )
135
137
} ,
136
138
define : {
137
139
__VP_LOCAL_SEARCH__ : site . themeConfig ?. search ?. provider === 'local' ,
@@ -147,10 +149,7 @@ export async function createVitePressPlugin(
147
149
include : [
148
150
'vue' ,
149
151
'vitepress > @vue/devtools-api' ,
150
- 'vitepress > @vueuse/core' ,
151
- siteConfig . themeDir === DEFAULT_THEME_PATH
152
- ? '@theme/index'
153
- : undefined
152
+ 'vitepress > @vueuse/core'
154
153
] . filter ( ( d ) => d != null ) ,
155
154
exclude : [ '@docsearch/js' , 'vitepress' ]
156
155
} ,
@@ -170,10 +169,17 @@ export async function createVitePressPlugin(
170
169
: baseConfig
171
170
} ,
172
171
173
- resolveId ( id ) {
174
- if ( id === SITE_DATA_REQUEST_PATH ) {
172
+ resolveId ( id , importer , resolveOptions ) {
173
+ if ( id === SITE_DATA_ID ) {
175
174
return SITE_DATA_REQUEST_PATH
176
175
}
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
+ }
177
183
} ,
178
184
179
185
load ( id ) {
@@ -260,31 +266,6 @@ export async function createVitePressPlugin(
260
266
configDeps . forEach ( ( file ) => server . watcher . add ( file ) )
261
267
}
262
268
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
-
288
269
// serve our index.html after vite history fallback
289
270
return ( ) => {
290
271
server . middlewares . use ( async ( req , res , next ) => {
@@ -369,8 +350,34 @@ export async function createVitePressPlugin(
369
350
}
370
351
} ,
371
352
372
- async hotUpdate ( { file } ) {
353
+ async hotUpdate ( { file, type } ) {
373
354
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 ( {
366
+ type : 'full-reload' ,
367
+ path : '*' ,
368
+ triggeredBy : path . resolve ( config . root , file )
369
+ } )
370
+ return [ ]
371
+ }
372
+
373
+ // update pages, dynamicRoutes and rewrites on md file creation / deletion
374
+ if ( file . endsWith ( '.md' ) && type !== 'update' ) {
375
+ await resolvePages ( siteConfig )
376
+ }
377
+
378
+ if ( type === 'delete' ) {
379
+ delete importerMap [ relativePath ]
380
+ }
374
381
375
382
if (
376
383
file === configPath ||
0 commit comments