Skip to content

Commit 5666d07

Browse files
authoredOct 26, 2024··
fix: HMR on slide facades (#1913)
1 parent 9d15fc2 commit 5666d07

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed
 

Diff for: ‎packages/slidev/node/vite/loaders.ts

+37-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ResolvedSlidevOptions, SlideInfo, SlidePatch, SlidevServerOptions } from '@slidev/types'
2+
import type { SlidevData } from 'packages/types'
23
import type { LoadResult } from 'rollup'
34
import type { Plugin, ViteDevServer } from 'vite'
45
import { notNullish, range } from '@antfu/utils'
@@ -12,7 +13,7 @@ import { templates } from '../virtual'
1213
import { templateConfigs } from '../virtual/configs'
1314
import { templateMonacoRunDeps } from '../virtual/monaco-deps'
1415
import { templateMonacoTypes } from '../virtual/monaco-types'
15-
import { templateSlides } from '../virtual/slides'
16+
import { templateSlides, VIRTUAL_SLIDE_PREFIX } from '../virtual/slides'
1617
import { templateTitleRendererMd } from '../virtual/titles'
1718
import { regexSlideFacadeId, regexSlideReqPath, regexSlideSourceId } from './common'
1819

@@ -40,15 +41,29 @@ export function createSlidesLoader(
4041
options: ResolvedSlidevOptions,
4142
serverOptions: SlidevServerOptions,
4243
): Plugin {
44+
const { data, mode, utils } = options
45+
4346
const hmrSlidesIndexes = new Set<number>()
4447
let server: ViteDevServer | undefined
45-
4648
let skipHmr: { filePath: string, fileContent: string } | null = null
4749

48-
const { data, mode, utils } = options
50+
interface ResolvedSourceIds {
51+
md: string[]
52+
frontmatter: string[]
53+
}
54+
let sourceIds = resolveSourceIds(data)
4955

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
5267
}
5368

5469
function updateServerWatcher() {
@@ -117,11 +132,11 @@ export function createSlidesLoader(
117132
fileContent,
118133
}
119134
server?.moduleGraph.invalidateModule(
120-
server.moduleGraph.getModuleById(getSourceId(idx, 'md'))!,
135+
server.moduleGraph.getModuleById(sourceIds.md[idx])!,
121136
)
122137
if (body.frontmatter) {
123138
server?.moduleGraph.invalidateModule(
124-
server.moduleGraph.getModuleById(getSourceId(idx, 'frontmatter'))!,
139+
server.moduleGraph.getModuleById(sourceIds.frontmatter[idx])!,
125140
)
126141
}
127142
}
@@ -158,9 +173,20 @@ export function createSlidesLoader(
158173

159174
const moduleIds = new Set<string>()
160175

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+
161188
if (data.slides.length !== newData.slides.length) {
162189
moduleIds.add(templateSlides.id)
163-
range(newData.slides.length).map(i => hmrSlidesIndexes.add(i))
164190
}
165191

166192
if (!equal(data.headmatter.defaults, newData.headmatter.defaults)) {
@@ -220,8 +246,8 @@ export function createSlidesLoader(
220246

221247
const vueModules = Array.from(hmrSlidesIndexes)
222248
.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])
225251
const styles = main ? [...main.clientImportedModules].find(m => m.id?.includes(`&type=style`)) : undefined
226252
return [
227253
frontmatter,
@@ -267,7 +293,7 @@ export function createSlidesLoader(
267293
if (matchFacade) {
268294
const [, no, type] = matchFacade
269295
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])
271297
return [
272298
`export * from ${sourceId}`,
273299
`export { default } from ${sourceId}`,

0 commit comments

Comments
 (0)
Please sign in to comment.