@@ -38,6 +38,25 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
3838 'theme' ,
3939]
4040
41+ /**
42+ * Files that triggers a restart when added or removed
43+ */
44+ const FILES_CREATE_RESTART_GLOBS = [
45+ 'global-bottom.vue' ,
46+ 'global-top.vue' ,
47+ 'uno.config.js' ,
48+ 'uno.config.ts' ,
49+ 'unocss.config.js' ,
50+ 'unocss.config.ts' ,
51+ ]
52+
53+ const FILES_CHANGE_RESTART_GLOBS = [
54+ 'vite.config.*' ,
55+ 'setup/shiki.ts' ,
56+ 'setup/katex.ts' ,
57+ 'setup/preparser.ts' ,
58+ ]
59+
4160injectPreparserExtensionLoader ( async ( headmatter ?: Record < string , unknown > , filepath ?: string , mode ?: string ) => {
4261 const addons = headmatter ?. addons as string [ ] ?? [ ]
4362 const { clientRoot, userRoot } = await getRoots ( )
@@ -113,6 +132,15 @@ cli.command(
113132
114133 let lastRemoteUrl : string | undefined
115134
135+ let restartTimer : ReturnType < typeof setTimeout > | undefined
136+ function restartServer ( ) {
137+ clearTimeout ( restartTimer ! )
138+ restartTimer = setTimeout ( ( ) => {
139+ console . log ( yellow ( '\n restarting...\n' ) )
140+ initServer ( )
141+ } , 500 )
142+ }
143+
116144 async function initServer ( ) {
117145 if ( server )
118146 await server . close ( )
@@ -149,7 +177,7 @@ cli.command(
149177 const themeRaw = theme || loaded . headmatter . theme as string || 'default'
150178 if ( options . themeRaw !== themeRaw ) {
151179 console . log ( yellow ( '\n restarting on theme change\n' ) )
152- initServer ( )
180+ restartServer ( )
153181 return false
154182 }
155183 // Because themeRaw is not changed, we don't resolve it again
@@ -162,7 +190,7 @@ cli.command(
162190
163191 if ( CONFIG_RESTART_FIELDS . some ( i => ! equal ( newData . config [ i ] , oldData . config [ i ] ) ) ) {
164192 console . log ( yellow ( '\n restarting on config change\n' ) )
165- initServer ( )
193+ restartServer ( )
166194 return false
167195 }
168196 return newData
@@ -276,6 +304,30 @@ cli.command(
276304
277305 initServer ( )
278306 bindShortcut ( )
307+
308+ // Start watcher to restart server on file changes
309+ const { watch } = await import ( 'chokidar' )
310+ const watcher = watch ( [
311+ ...FILES_CREATE_RESTART_GLOBS ,
312+ ...FILES_CHANGE_RESTART_GLOBS ,
313+ ] , {
314+ ignored : [ 'node_modules' , '.git' ] ,
315+ ignoreInitial : true ,
316+ } )
317+ watcher . on ( 'unlink' , ( file ) => {
318+ console . log ( yellow ( `\n file ${ file } removed, restarting...\n` ) )
319+ restartServer ( )
320+ } )
321+ watcher . on ( 'add' , ( file ) => {
322+ console . log ( yellow ( `\n file ${ file } added, restarting...\n` ) )
323+ restartServer ( )
324+ } )
325+ watcher . on ( 'change' , ( file ) => {
326+ if ( FILES_CREATE_RESTART_GLOBS . includes ( file ) )
327+ return
328+ console . log ( yellow ( `\n file ${ file } changed, restarting...\n` ) )
329+ restartServer ( )
330+ } )
279331 } ,
280332)
281333
0 commit comments