1
1
import type { ResolvedSlidevOptions , SlideInfo , SlidePatch , SlidevServerOptions } from '@slidev/types'
2
+ import type { SlidevData } from 'packages/types'
2
3
import type { LoadResult } from 'rollup'
3
4
import type { Plugin , ViteDevServer } from 'vite'
4
5
import { notNullish , range } from '@antfu/utils'
@@ -12,7 +13,7 @@ import { templates } from '../virtual'
12
13
import { templateConfigs } from '../virtual/configs'
13
14
import { templateMonacoRunDeps } from '../virtual/monaco-deps'
14
15
import { templateMonacoTypes } from '../virtual/monaco-types'
15
- import { templateSlides } from '../virtual/slides'
16
+ import { templateSlides , VIRTUAL_SLIDE_PREFIX } from '../virtual/slides'
16
17
import { templateTitleRendererMd } from '../virtual/titles'
17
18
import { regexSlideFacadeId , regexSlideReqPath , regexSlideSourceId } from './common'
18
19
@@ -40,15 +41,29 @@ export function createSlidesLoader(
40
41
options : ResolvedSlidevOptions ,
41
42
serverOptions : SlidevServerOptions ,
42
43
) : Plugin {
44
+ const { data, mode, utils } = options
45
+
43
46
const hmrSlidesIndexes = new Set < number > ( )
44
47
let server : ViteDevServer | undefined
45
-
46
48
let skipHmr : { filePath : string , fileContent : string } | null = null
47
49
48
- const { data, mode, utils } = options
50
+ interface ResolvedSourceIds {
51
+ md : string [ ]
52
+ frontmatter : string [ ]
53
+ }
54
+ let sourceIds = resolveSourceIds ( data )
49
55
50
- function getSourceId ( index : number , type : 'md' | 'frontmatter' ) {
51
- return `${ data . slides [ index ] . source . filepath } __slidev_${ index + 1 } .${ type } `
56
+ function resolveSourceIds ( data : SlidevData ) {
57
+ const ids : ResolvedSourceIds = {
58
+ md : [ ] ,
59
+ frontmatter : [ ] ,
60
+ }
61
+ for ( const type of [ 'md' , 'frontmatter' ] as const ) {
62
+ for ( let i = 0 ; i < data . slides . length ; i ++ ) {
63
+ ids [ type ] . push ( `${ data . slides [ i ] . source . filepath } __slidev_${ i + 1 } .${ type } ` )
64
+ }
65
+ }
66
+ return ids
52
67
}
53
68
54
69
function updateServerWatcher ( ) {
@@ -117,11 +132,11 @@ export function createSlidesLoader(
117
132
fileContent,
118
133
}
119
134
server ?. moduleGraph . invalidateModule (
120
- server . moduleGraph . getModuleById ( getSourceId ( idx , 'md' ) ) ! ,
135
+ server . moduleGraph . getModuleById ( sourceIds . md [ idx ] ) ! ,
121
136
)
122
137
if ( body . frontmatter ) {
123
138
server ?. moduleGraph . invalidateModule (
124
- server . moduleGraph . getModuleById ( getSourceId ( idx , ' frontmatter' ) ) ! ,
139
+ server . moduleGraph . getModuleById ( sourceIds . frontmatter [ idx ] ) ! ,
125
140
)
126
141
}
127
142
}
@@ -158,9 +173,20 @@ export function createSlidesLoader(
158
173
159
174
const moduleIds = new Set < string > ( )
160
175
176
+ const newSourceIds = resolveSourceIds ( newData )
177
+ for ( const type of [ 'md' , 'frontmatter' ] as const ) {
178
+ const old = sourceIds [ type ]
179
+ const newIds = newSourceIds [ type ]
180
+ for ( let i = 0 ; i < newIds . length ; i ++ ) {
181
+ if ( old [ i ] !== newIds [ i ] ) {
182
+ moduleIds . add ( `${ VIRTUAL_SLIDE_PREFIX } ${ i + 1 } /${ type } ` )
183
+ }
184
+ }
185
+ }
186
+ sourceIds = newSourceIds
187
+
161
188
if ( data . slides . length !== newData . slides . length ) {
162
189
moduleIds . add ( templateSlides . id )
163
- range ( newData . slides . length ) . map ( i => hmrSlidesIndexes . add ( i ) )
164
190
}
165
191
166
192
if ( ! equal ( data . headmatter . defaults , newData . headmatter . defaults ) ) {
@@ -220,8 +246,8 @@ export function createSlidesLoader(
220
246
221
247
const vueModules = Array . from ( hmrSlidesIndexes )
222
248
. flatMap ( ( idx ) => {
223
- const frontmatter = ctx . server . moduleGraph . getModuleById ( getSourceId ( idx , ' frontmatter' ) )
224
- const main = ctx . server . moduleGraph . getModuleById ( getSourceId ( idx , 'md' ) )
249
+ const frontmatter = ctx . server . moduleGraph . getModuleById ( sourceIds . frontmatter [ idx ] )
250
+ const main = ctx . server . moduleGraph . getModuleById ( sourceIds . md [ idx ] )
225
251
const styles = main ? [ ...main . clientImportedModules ] . find ( m => m . id ?. includes ( `&type=style` ) ) : undefined
226
252
return [
227
253
frontmatter ,
@@ -267,7 +293,7 @@ export function createSlidesLoader(
267
293
if ( matchFacade ) {
268
294
const [ , no , type ] = matchFacade
269
295
const idx = + no - 1
270
- const sourceId = JSON . stringify ( getSourceId ( idx , type as 'md' | 'frontmatter' ) )
296
+ const sourceId = JSON . stringify ( sourceIds [ type as 'md' | 'frontmatter' ] [ idx ] )
271
297
return [
272
298
`export * from ${ sourceId } ` ,
273
299
`export { default } from ${ sourceId } ` ,
0 commit comments