| 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 | const RE_TS_JS_FILE = /\.[cm]?[jt]s(?:$|\?)/ |
| 7 | const RE_VUE_QUERY_SCRIPT = /\?vue&type=script/ |
| 8 | // Skip JS/TS dependencies, but keep `@slidev/client` (shipped as source, may |
| 9 | // be nested under another package's node_modules) and Vue SFCs, whose |
| 10 | // templates reference flags as `_ctx.__FLAG__` that no other plugin replaces. |
| 11 | const RE_SKIPPED_DEPENDENCY = /\/node_modules\/(?!(?:.*\/)?@slidev\/client\/)(?!.*\.vue(?:$|\?))/ |
| 12 | |
| 13 | /** |
| 14 | * Replace compiler flags like `__DEV__` in Vue SFCs and client JS/TS modules. |
| 15 | * |
| 16 | * Vite's own `vite:define` does not substitute bare identifiers for the |
| 17 | * client environment in dev (see vitejs/vite#22419 class of regression), so |
| 18 | * Slidev's `__DEV__`/`__SLIDEV_*` flags would pass through unsubstituted in |
| 19 | * `.ts`/`.js` modules such as `packages/client/setup/main.ts`, breaking cold |
| 20 | * starts of custom `setup/main.ts` directives. |
| 21 | * |
| 22 | * Vue SFC files are transformed in every mode: their compiled render |
| 23 | * functions reference the flags as property accesses (e.g. |
| 24 | * `ctx.__SLIDEV_FEATURE_PWA__`), which Vite's identifier-only define cannot |
| 25 | * substitute in production builds either. |
| 26 | * |
| 27 | * JS/TS modules are transformed in dev only: Vite's own define substitutes |
| 28 | * bare identifiers in production builds, so running both would double-replace. |
| 29 | */ |
| 30 | export function createVueCompilerFlagsPlugin( |
| 31 | options: ResolvedSlidevOptions, |
| 32 | ): Plugin { |
| 33 | const define = objectEntries(options.utils.define) |
| 34 | const flagMap = new Map(define) |
| 35 | const flagRe = new RegExp( |
| 36 | [ |
| 37 | ...flagMap.keys(), |
| 38 | ].map(k => `(?<![\\w$])${escapeRegExp(k)}(?![\\w$])`).join('|'), |
| 39 | 'g', |
| 40 | ) |
| 41 | return { |
| 42 | name: 'slidev:flags', |
| 43 | enforce: 'pre', |
| 44 | transform: { |
| 45 | filter: { |
| 46 | id: { |
| 47 | include: [RE_VUE_FILE, RE_VUE_QUERY_SCRIPT, RE_TS_JS_FILE], |
| 48 | exclude: RE_SKIPPED_DEPENDENCY, |
| 49 | }, |
| 50 | }, |
| 51 | handler(code, id) { |
| 52 | // JS/TS modules are dev-only: Vite's define handles production builds. |
| 53 | if (options.mode !== 'dev' && RE_TS_JS_FILE.test(id)) |
| 54 | return |
| 55 | // Static filter: skip modules that cannot contain any flag. |
| 56 | if (!flagRe.test(code)) { |
| 57 | flagRe.lastIndex = 0 |
| 58 | return |
| 59 | } |
| 60 | flagRe.lastIndex = 0 |
| 61 | const original = code |
| 62 | code = code.replace(flagRe, m => flagMap.get(m)!) |
| 63 | if (original !== code) |
| 64 | return code |
| 65 | }, |
| 66 | }, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function escapeRegExp(str: string) { |
| 71 | return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 72 | } |
| 73 |