| 1 | import type { ResolvedSlidevOptions } from '@slidev/types' |
| 2 | import type { Plugin } from 'vite' |
| 3 | import { objectEntries } from '@antfu/utils' |
| 4 | |
| 5 | const RE_VUE_FILE = /\.vue(?:$|\?)/ |
| 6 | |
| 7 | /** |
| 8 | * Replace compiler flags like `__DEV__` in Vue SFC |
| 9 | */ |
| 10 | export function createVueCompilerFlagsPlugin( |
| 11 | options: ResolvedSlidevOptions, |
| 12 | ): Plugin { |
| 13 | const define = objectEntries(options.utils.define) |
| 14 | return { |
| 15 | name: 'slidev:flags', |
| 16 | enforce: 'pre', |
| 17 | transform: { |
| 18 | // TODO: static filter |
| 19 | handler(code, id) { |
| 20 | if (!RE_VUE_FILE.test(id) && !id.includes('?vue&')) |
| 21 | return |
| 22 | const original = code |
| 23 | define.forEach(([from, to]) => { |
| 24 | code = code.replaceAll(from, to) |
| 25 | }) |
| 26 | if (original !== code) |
| 27 | return code |
| 28 | }, |
| 29 | }, |
| 30 | } |
| 31 | } |
| 32 |