| 1 | import type { ResolvedSlidevOptions, SlidevPluginOptions } from '@slidev/types' |
| 2 | import type { Plugin } from 'vite' |
| 3 | import Vue from '@vitejs/plugin-vue' |
| 4 | import VueJsx from '@vitejs/plugin-vue-jsx' |
| 5 | |
| 6 | const RE_VUE_EXT = /\.vue$/ |
| 7 | const RE_VUE_QUERY_VUE = /\.vue\?vue/ |
| 8 | const RE_VUE_QUERY_V = /\.vue\?v=/ |
| 9 | const RE_MD_EXT = /\.md$/ |
| 10 | const RE_MD_QUERY_VUE = /\.md\?vue/ |
| 11 | |
| 12 | const customElements = new Set([ |
| 13 | // katex |
| 14 | 'annotation', |
| 15 | 'math', |
| 16 | 'menclose', |
| 17 | 'mfrac', |
| 18 | 'mglyph', |
| 19 | 'mi', |
| 20 | 'mlabeledtr', |
| 21 | 'mn', |
| 22 | 'mo', |
| 23 | 'mover', |
| 24 | 'mpadded', |
| 25 | 'mphantom', |
| 26 | 'mroot', |
| 27 | 'mrow', |
| 28 | 'mspace', |
| 29 | 'msqrt', |
| 30 | 'mstyle', |
| 31 | 'msub', |
| 32 | 'msubsup', |
| 33 | 'msup', |
| 34 | 'mtable', |
| 35 | 'mtd', |
| 36 | 'mtext', |
| 37 | 'mtr', |
| 38 | 'munder', |
| 39 | 'munderover', |
| 40 | 'semantics', |
| 41 | ]) |
| 42 | |
| 43 | export async function createVuePlugin( |
| 44 | _options: ResolvedSlidevOptions, |
| 45 | pluginOptions: SlidevPluginOptions, |
| 46 | ): Promise<Plugin[]> { |
| 47 | const { |
| 48 | vue: vueOptions = {}, |
| 49 | vuejsx: vuejsxOptions = {}, |
| 50 | } = pluginOptions |
| 51 | |
| 52 | const VuePlugin = Vue({ |
| 53 | include: [RE_VUE_EXT, RE_VUE_QUERY_VUE, RE_VUE_QUERY_V, RE_MD_EXT, RE_MD_QUERY_VUE], |
| 54 | exclude: [], |
| 55 | ...vueOptions, |
| 56 | template: { |
| 57 | ...vueOptions?.template, |
| 58 | compilerOptions: { |
| 59 | ...vueOptions?.template?.compilerOptions, |
| 60 | isCustomElement(tag) { |
| 61 | return customElements.has(tag) || vueOptions?.template?.compilerOptions?.isCustomElement?.(tag) |
| 62 | }, |
| 63 | }, |
| 64 | }, |
| 65 | }) |
| 66 | const VueJsxPlugin = VueJsx(vuejsxOptions) |
| 67 | |
| 68 | return [ |
| 69 | VueJsxPlugin, |
| 70 | VuePlugin, |
| 71 | ] |
| 72 | } |
| 73 |