| 1 | import type { ResolvedSlidevOptions } from '@slidev/types' |
| 2 | import type { Plugin } from 'vite' |
| 3 | import { describe, expect, it } from 'vitest' |
| 4 | import { createContextInjectionPlugin } from '../packages/slidev/node/vite/contextInjection' |
| 5 | import { createSlidesLoader } from '../packages/slidev/node/vite/loaders' |
| 6 | import { createMonacoTypesLoader } from '../packages/slidev/node/vite/monacoTypes' |
| 7 | |
| 8 | // Inspect the declarative filter rather than only the hook handler: existing |
| 9 | // in-handler checks already worked, but Rolldown still called every hook. |
| 10 | function hookMatches(plugin: Plugin, name: 'transform' | 'resolveId' | 'load', id: string): boolean { |
| 11 | const hook = plugin[name] |
| 12 | expect(hook).toBeDefined() |
| 13 | expect(typeof hook).not.toBe('function') |
| 14 | const { filter } = hook as unknown as { |
| 15 | filter: { id: RegExp | { include: RegExp, exclude?: RegExp } } |
| 16 | } |
| 17 | expect(filter).toBeDefined() |
| 18 | const pattern = filter.id |
| 19 | if (pattern instanceof RegExp) |
| 20 | return pattern.test(id) |
| 21 | return pattern.include.test(id) && !(pattern.exclude?.test(id) ?? false) |
| 22 | } |
| 23 | |
| 24 | describe('vite plugin hook filters', () => { |
| 25 | it('only injects context into user Vue component files', async () => { |
| 26 | const plugin = createContextInjectionPlugin() |
| 27 | expect(hookMatches(plugin, 'transform', '/project/App.vue')).toBe(true) |
| 28 | expect(hookMatches(plugin, 'transform', '/project/App.vue?vue&type=script')).toBe(false) |
| 29 | expect(hookMatches(plugin, 'transform', '/project/packages/client/App.vue')).toBe(false) |
| 30 | expect(hookMatches(plugin, 'transform', '/@slidev/client/App.vue')).toBe(false) |
| 31 | const hook = plugin.transform as { handler: (code: string, id: string) => Promise<unknown> } |
| 32 | expect(await hook.handler('<script setup>const n = 1</script>', '/project/App.vue')).toContain('/* @slidev-injection */') |
| 33 | expect(await hook.handler('<script setup>const n = 1</script>', '/project/packages/client/App.vue')).toBeUndefined() |
| 34 | }) |
| 35 | |
| 36 | it('filters slide resolver calls without narrowing the dynamic slide loader', () => { |
| 37 | const options = { |
| 38 | data: { |
| 39 | slides: [], |
| 40 | features: { katex: false }, |
| 41 | }, |
| 42 | userRoot: '/project', |
| 43 | mode: 'dev', |
| 44 | utils: {}, |
| 45 | } as unknown as ResolvedSlidevOptions |
| 46 | const plugin = createSlidesLoader(options, {}) |
| 47 | expect(hookMatches(plugin, 'resolveId', '/@slidev/routes')).toBe(true) |
| 48 | expect(hookMatches(plugin, 'resolveId', '/project/slides.md__slidev_1.md')).toBe(true) |
| 49 | expect(hookMatches(plugin, 'resolveId', '/project/main.ts')).toBe(false) |
| 50 | const resolve = plugin.resolveId as { handler: (id: string) => unknown } |
| 51 | expect(resolve.handler('/project/slides.md__slidev_1.md')).toBe('/project/slides.md__slidev_1.md') |
| 52 | expect(resolve.handler('/project/main.ts')).toBeNull() |
| 53 | }) |
| 54 | |
| 55 | it('limits both Monaco resolve and load hooks to the virtual prefix', async () => { |
| 56 | const plugin = createMonacoTypesLoader({ userRoot: '/project', utils: {} } as ResolvedSlidevOptions) |
| 57 | for (const name of ['resolveId', 'load'] as const) { |
| 58 | expect(hookMatches(plugin, name, '/@slidev-monaco-types/resolve?pkg=vue')).toBe(true) |
| 59 | expect(hookMatches(plugin, name, '/@slidev-monaco-types/load?name=vue')).toBe(true) |
| 60 | expect(hookMatches(plugin, name, '/project/App.vue')).toBe(false) |
| 61 | } |
| 62 | const resolve = plugin.resolveId as { handler: (id: string) => unknown } |
| 63 | const load = plugin.load as { handler: (id: string) => Promise<unknown> } |
| 64 | expect(resolve.handler('/@slidev-monaco-types/resolve?pkg=vue')).toBe('/@slidev-monaco-types/resolve?pkg=vue') |
| 65 | expect(resolve.handler('/project/App.vue')).toBeNull() |
| 66 | expect(await load.handler('/project/App.vue')).toBeNull() |
| 67 | }) |
| 68 | }) |
| 69 |