| 1 | /** |
| 2 | * Regression for https://github.com/slidevjs/slidev/issues/2784 |
| 3 | * |
| 4 | * On Vite 8, invalidating `/@slidev/title-renderer.md` when it is present in the |
| 5 | * client module graph but not `isSelfAccepting` dead-ends HMR and forces a full |
| 6 | * page reload. Title-renderer only embeds slide titles, so body-only edits must |
| 7 | * not invalidate it. |
| 8 | */ |
| 9 | import type { HotPayload } from 'vite' |
| 10 | import { mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 11 | import { tmpdir } from 'node:os' |
| 12 | import { join } from 'node:path' |
| 13 | import { afterEach, describe, expect, it } from 'vitest' |
| 14 | import { createServer, parser, resolveOptions } from '../packages/slidev/node' |
| 15 | |
| 16 | const TITLE_RENDERER_MD = '/@slidev/title-renderer.md' |
| 17 | |
| 18 | const ENTRY_MD = `--- |
| 19 | theme: none |
| 20 | title: HMR Repro |
| 21 | --- |
| 22 | |
| 23 | # Hello |
| 24 | |
| 25 | Body text |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | # Second |
| 30 | |
| 31 | More |
| 32 | ` |
| 33 | |
| 34 | interface HmrCapture { |
| 35 | fullReloads: Array<Extract<HotPayload, { type: 'full-reload' }>> |
| 36 | slideUpdates: number |
| 37 | moduleSnapshots: Array<Array<{ id: string | null, isSelfAccepting: boolean | undefined }>> |
| 38 | } |
| 39 | |
| 40 | function titleRendererInvalidated(capture: HmrCapture) { |
| 41 | return capture.moduleSnapshots.some(snapshot => |
| 42 | snapshot.some(m => m.id === TITLE_RENDERER_MD), |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | async function createReproServer(entry: string) { |
| 47 | const options = await resolveOptions({ entry, theme: 'none' }, 'dev') |
| 48 | const capture: HmrCapture = { |
| 49 | fullReloads: [], |
| 50 | slideUpdates: 0, |
| 51 | moduleSnapshots: [], |
| 52 | } |
| 53 | |
| 54 | const server = await createServer( |
| 55 | options, |
| 56 | { |
| 57 | plugins: [ |
| 58 | { |
| 59 | name: 'slidev-title-renderer-hmr-capture', |
| 60 | hotUpdate: { |
| 61 | order: 'post', |
| 62 | handler(ctx) { |
| 63 | if (!ctx.modules.length) |
| 64 | return |
| 65 | capture.moduleSnapshots.push( |
| 66 | ctx.modules.map(m => ({ |
| 67 | id: m.id, |
| 68 | isSelfAccepting: m.isSelfAccepting, |
| 69 | })), |
| 70 | ) |
| 71 | }, |
| 72 | }, |
| 73 | configureServer(s) { |
| 74 | const hot = s.environments.client.hot |
| 75 | const send = hot.send.bind(hot) |
| 76 | hot.send = ((...args: [payload: HotPayload | string, data?: unknown]) => { |
| 77 | const payload = args[0] |
| 78 | if (payload && typeof payload === 'object' && payload.type === 'full-reload') |
| 79 | capture.fullReloads.push(payload) |
| 80 | if (typeof payload === 'string' && payload === 'slidev:update-slide') |
| 81 | capture.slideUpdates += 1 |
| 82 | return (send as (...a: typeof args) => void)(...args) |
| 83 | }) as typeof hot.send |
| 84 | }, |
| 85 | }, |
| 86 | ], |
| 87 | server: { middlewareMode: true }, |
| 88 | clearScreen: false, |
| 89 | logLevel: 'error', |
| 90 | }, |
| 91 | { |
| 92 | async loadData(loadedSource) { |
| 93 | const loaded = await parser.load(options, options.entry, loadedSource, 'dev') |
| 94 | return { |
| 95 | ...loaded, |
| 96 | themeMeta: options.data.themeMeta, |
| 97 | config: parser.resolveConfig(loaded.headmatter, options.data.themeMeta, options.entry), |
| 98 | } |
| 99 | }, |
| 100 | }, |
| 101 | ) |
| 102 | |
| 103 | return { server, options, capture } |
| 104 | } |
| 105 | |
| 106 | describe('title-renderer HMR (#2784)', () => { |
| 107 | const cleanups: Array<() => Promise<void>> = [] |
| 108 | |
| 109 | afterEach(async () => { |
| 110 | while (cleanups.length) |
| 111 | await cleanups.pop()!() |
| 112 | }) |
| 113 | |
| 114 | it('does not full-reload on a body-only slides.md edit when title-renderer is in the module graph', async () => { |
| 115 | const dir = await mkdtemp(join(tmpdir(), 'slidev-title-renderer-hmr-')) |
| 116 | const entry = join(dir, 'slides.md') |
| 117 | await writeFile(entry, ENTRY_MD) |
| 118 | |
| 119 | const { server, capture } = await createReproServer(entry) |
| 120 | cleanups.push(async () => { |
| 121 | await server.close() |
| 122 | await rm(dir, { recursive: true, force: true }) |
| 123 | }) |
| 124 | |
| 125 | // Mirror the Vite 8 client graph state from #2784: the virtual module exists |
| 126 | // (e.g. after overview/goto loaded it) but is not yet self-accepting. |
| 127 | const titleMod = await server.environments.client.moduleGraph.ensureEntryFromUrl(TITLE_RENDERER_MD) |
| 128 | expect(titleMod.isSelfAccepting).toBe(false) |
| 129 | |
| 130 | await writeFile(entry, ENTRY_MD.replace('Body text', 'Body text changed')) |
| 131 | await waitFor(() => capture.slideUpdates > 0 || capture.fullReloads.length > 0) |
| 132 | |
| 133 | expect(capture.slideUpdates, 'Slidev should still emit slidev:update-slide').toBeGreaterThan(0) |
| 134 | expect(titleRendererInvalidated(capture), 'body-only edits must not invalidate title-renderer.md').toBe(false) |
| 135 | expect( |
| 136 | capture.fullReloads, |
| 137 | [ |
| 138 | 'body-only edits must not trigger Vite full-reload via title-renderer.md.', |
| 139 | `HMR modules seen: ${JSON.stringify(capture.moduleSnapshots)}`, |
| 140 | ].join(' '), |
| 141 | ).toEqual([]) |
| 142 | }, 20_000) |
| 143 | |
| 144 | it('still invalidates title-renderer.md when a slide title changes', async () => { |
| 145 | const dir = await mkdtemp(join(tmpdir(), 'slidev-title-renderer-hmr-title-')) |
| 146 | const entry = join(dir, 'slides.md') |
| 147 | await writeFile(entry, ENTRY_MD) |
| 148 | |
| 149 | const { server, capture } = await createReproServer(entry) |
| 150 | cleanups.push(async () => { |
| 151 | await server.close() |
| 152 | await rm(dir, { recursive: true, force: true }) |
| 153 | }) |
| 154 | |
| 155 | await server.environments.client.moduleGraph.ensureEntryFromUrl(TITLE_RENDERER_MD) |
| 156 | |
| 157 | // Second slide title comes from its heading (no per-slide frontmatter title). |
| 158 | await writeFile(entry, ENTRY_MD.replace('# Second', '# Second Renamed')) |
| 159 | await waitFor(() => capture.slideUpdates > 0 || titleRendererInvalidated(capture)) |
| 160 | |
| 161 | expect(capture.slideUpdates).toBeGreaterThan(0) |
| 162 | expect(titleRendererInvalidated(capture)).toBe(true) |
| 163 | }, 20_000) |
| 164 | }) |
| 165 | |
| 166 | async function waitFor(predicate: () => boolean, timeout = 5_000, interval = 50) { |
| 167 | const start = Date.now() |
| 168 | while (Date.now() - start < timeout) { |
| 169 | if (predicate()) |
| 170 | return |
| 171 | await new Promise(r => setTimeout(r, interval)) |
| 172 | } |
| 173 | throw new Error(`Timed out after ${timeout}ms waiting for HMR activity`) |
| 174 | } |
| 175 |