| 1 | import type { ResolvedSlidevOptions } from '@slidev/types' |
| 2 | import { describe, expect, it } from 'vitest' |
| 3 | import { createVueCompilerFlagsPlugin } from './compilerFlagsVue' |
| 4 | |
| 5 | function makeOptions(define: Record<string, string>, mode = 'dev'): ResolvedSlidevOptions { |
| 6 | return { |
| 7 | mode, |
| 8 | utils: { |
| 9 | define, |
| 10 | }, |
| 11 | } as unknown as ResolvedSlidevOptions |
| 12 | } |
| 13 | |
| 14 | function transformCode(define: Record<string, string>, code: string, id: string, mode = 'dev') { |
| 15 | const plugin = createVueCompilerFlagsPlugin(makeOptions(define, mode)) |
| 16 | const transform = plugin.transform as { |
| 17 | filter?: { id?: { include?: RegExp | RegExp[], exclude?: RegExp } } |
| 18 | handler: (code: string, id: string) => string | undefined |
| 19 | } |
| 20 | // Mirror Vite's hook filtering: the handler only runs when the id matches. |
| 21 | const include = transform.filter?.id?.include |
| 22 | const matches = Array.isArray(include) |
| 23 | ? include.some(re => re.test(id)) |
| 24 | : include?.test(id) |
| 25 | if (!matches) |
| 26 | return undefined |
| 27 | if (transform.filter?.id?.exclude?.test(id)) |
| 28 | return undefined |
| 29 | return transform.handler(code, id) |
| 30 | } |
| 31 | |
| 32 | const DEFINE = { |
| 33 | __DEV__: 'true', |
| 34 | __SLIDEV_HASH_ROUTE__: 'false', |
| 35 | __SLIDEV_MEMORY_ROUTE__: 'false', |
| 36 | __SLIDEV_FEATURE_PWA__: 'false', |
| 37 | } |
| 38 | |
| 39 | describe('createVueCompilerFlagsPlugin', () => { |
| 40 | it('replaces define flags in TypeScript modules', () => { |
| 41 | const code = ` |
| 42 | const router = createRouter({ |
| 43 | history: __SLIDEV_MEMORY_ROUTE__ ? createMemoryHistory(import.meta.env.BASE_URL) : __SLIDEV_HASH_ROUTE__ ? createWebHashHistory(import.meta.env.BASE_URL) : createWebHistory(import.meta.env.BASE_URL), |
| 44 | }) |
| 45 | if (__SLIDEV_FEATURE_PWA__) setupPWA() |
| 46 | ` |
| 47 | const result = transformCode(DEFINE, code, '/project/setup/main.ts') |
| 48 | expect(result).toContain('history: false ?') |
| 49 | expect(result).not.toContain('__SLIDEV_MEMORY_ROUTE__') |
| 50 | expect(result).not.toContain('__SLIDEV_HASH_ROUTE__') |
| 51 | expect(result).toContain('if (false) setupPWA()') |
| 52 | }) |
| 53 | |
| 54 | it('replaces define flags in JavaScript modules', () => { |
| 55 | const code = 'const isDev = __DEV__\n' |
| 56 | const result = transformCode(DEFINE, code, '/project/setup/main.js') |
| 57 | expect(result).toBe('const isDev = true\n') |
| 58 | }) |
| 59 | |
| 60 | it('replaces define flags in Vue SFC compiled output', () => { |
| 61 | const code = 'if (__DEV__ && __SLIDEV_FEATURE_PWA__) return 1\n' |
| 62 | const result = transformCode(DEFINE, code, '/project/App.vue') |
| 63 | expect(result).toBe('if (true && false) return 1\n') |
| 64 | }) |
| 65 | |
| 66 | it('replaces define flags in Vue SFC query ids', () => { |
| 67 | const code = 'const x = __DEV__\n' |
| 68 | const result = transformCode(DEFINE, code, '/project/App.vue?vue&type=script') |
| 69 | expect(result).toBe('const x = true\n') |
| 70 | }) |
| 71 | |
| 72 | it('leaves modules without matching extensions untouched', () => { |
| 73 | const code = 'const x = __DEV__\n' |
| 74 | const result = transformCode(DEFINE, code, '/project/styles.css') |
| 75 | expect(result).toBeUndefined() |
| 76 | }) |
| 77 | |
| 78 | it('leaves node_modules modules untouched', () => { |
| 79 | const code = 'const x = __DEV__\n' |
| 80 | const result = transformCode(DEFINE, code, '/project/node_modules/vue/dist/index.js') |
| 81 | expect(result).toBeUndefined() |
| 82 | }) |
| 83 | |
| 84 | it('replaces define flags in @slidev/client installed in node_modules', () => { |
| 85 | const code = 'if (__DEV__ && __SLIDEV_FEATURE_PWA__) return 1\n' |
| 86 | const expected = 'if (true && false) return 1\n' |
| 87 | for (const id of [ |
| 88 | '/project/node_modules/@slidev/client/internals/NavControls.vue', |
| 89 | '/project/node_modules/@slidev/client/internals/NavControls.vue?vue&type=script&setup=true&lang.ts', |
| 90 | '/project/node_modules/@slidev/client/setup/main.ts', |
| 91 | '/project/node_modules/@slidev/cli/node_modules/@slidev/client/App.vue', |
| 92 | '/project/node_modules/.pnpm/@slidev+client@53.2.0/node_modules/@slidev/client/App.vue', |
| 93 | ]) |
| 94 | expect(transformCode(DEFINE, code, id), id).toBe(expected) |
| 95 | }) |
| 96 | |
| 97 | it('replaces define flags in Vue SFCs from other node_modules packages', () => { |
| 98 | const code = 'const x = __DEV__\n' |
| 99 | const result = transformCode(DEFINE, code, '/project/node_modules/slidev-theme-foo/layouts/cover.vue') |
| 100 | expect(result).toBe('const x = true\n') |
| 101 | }) |
| 102 | |
| 103 | it('is a no-op for JS/TS modules in build mode', () => { |
| 104 | const code = 'const isDev = __DEV__\n' |
| 105 | const result = transformCode(DEFINE, code, '/project/setup/main.ts', 'build') |
| 106 | expect(result).toBeUndefined() |
| 107 | }) |
| 108 | |
| 109 | it('still replaces flags in Vue SFC compiled output in build mode', () => { |
| 110 | // Compiled Vue render functions reference flags as property accesses |
| 111 | // (ctx.__SLIDEV_FEATURE_PWA__) which Vite's identifier-only define cannot |
| 112 | // substitute in production builds either. |
| 113 | const code = 'if (__DEV__ && __SLIDEV_FEATURE_PWA__) return 1\n' |
| 114 | const result = transformCode(DEFINE, code, '/project/App.vue', 'build') |
| 115 | expect(result).toBe('if (true && false) return 1\n') |
| 116 | }) |
| 117 | |
| 118 | it('leaves code without define flags unchanged', () => { |
| 119 | const code = 'const x = 1\n' |
| 120 | const result = transformCode(DEFINE, code, '/project/main.ts') |
| 121 | expect(result).toBeUndefined() |
| 122 | }) |
| 123 | |
| 124 | it('does not replace compound identifiers that merely contain a flag name', () => { |
| 125 | const code = 'const x = __DEV__X\n' |
| 126 | const result = transformCode(DEFINE, code, '/project/main.ts') |
| 127 | // Identifier-boundary matching prevents replacing __DEV__ inside __DEV__X. |
| 128 | expect(result).toBeUndefined() |
| 129 | }) |
| 130 | |
| 131 | it('replaces standalone flag tokens even when adjacent to non-identifier characters', () => { |
| 132 | // `"__DEV__"` is a standalone flag token; only compound identifiers such |
| 133 | // as `__DEV__X` are preserved. Slidev's flag names never appear as |
| 134 | // standalone string content in practice. |
| 135 | const code = 'const s = "__DEV__"\n' |
| 136 | const result = transformCode(DEFINE, code, '/project/main.ts') |
| 137 | expect(result).toBe('const s = "true"\n') |
| 138 | }) |
| 139 | }) |
| 140 |