|
| 1 | +import { fileURLToPath } from 'node:url' |
| 2 | +import { basename } from 'node:path' |
| 3 | +import { defineConfig, normalizePath } from 'vite' |
| 4 | +import UnoCSS from 'unocss/vite' |
| 5 | +import type { ResolvedSlidevOptions, SlidevPluginOptions } from '@slidev/types' |
| 6 | +import IconsResolver from 'unplugin-icons/resolver' |
| 7 | +import Components from 'unplugin-vue-components/vite' |
| 8 | +import fg from 'fast-glob' |
| 9 | +import { createInspectPlugin } from '../slidev/node/vite/inspect' |
| 10 | +import { createIconsPlugin } from '../slidev/node/vite/icons' |
| 11 | +import { createVuePlugin } from '../slidev/node/vite/vue' |
| 12 | + |
| 13 | +const absolute = (path: string) => normalizePath(fileURLToPath(new URL(path, import.meta.url))) |
| 14 | +const clientRoot = absolute('.') |
| 15 | + |
| 16 | +const options = { |
| 17 | + clientRoot, |
| 18 | + roots: [], |
| 19 | + mode: 'build', |
| 20 | + inspect: true, |
| 21 | +} as unknown as ResolvedSlidevOptions |
| 22 | + |
| 23 | +const pluginOptions = { |
| 24 | + components: { |
| 25 | + dts: false, |
| 26 | + }, |
| 27 | +} as SlidevPluginOptions |
| 28 | + |
| 29 | +const defines = [ |
| 30 | + '__DEV__', |
| 31 | + '__SLIDEV_CLIENT_ROOT__', |
| 32 | + '__SLIDEV_HASH_ROUTE__', |
| 33 | + '__SLIDEV_FEATURE_DRAWINGS__', |
| 34 | + '__SLIDEV_FEATURE_EDITOR__', |
| 35 | + '__SLIDEV_FEATURE_DRAWINGS_PERSIST__', |
| 36 | + '__SLIDEV_FEATURE_RECORD__', |
| 37 | + '__SLIDEV_FEATURE_PRESENTER__', |
| 38 | + '__SLIDEV_FEATURE_PRINT__', |
| 39 | + '__SLIDEV_FEATURE_WAKE_LOCK__', |
| 40 | + '__SLIDEV_HAS_SERVER__', |
| 41 | +] |
| 42 | + |
| 43 | +const builtinComponents = Object.fromEntries(fg.sync('*', { |
| 44 | + cwd: absolute('./builtin'), |
| 45 | + absolute: true, |
| 46 | +}).map(i => [`components/${basename(i).replace(/\..*$/, '')}`, i])) |
| 47 | + |
| 48 | +const layoutComponents = Object.fromEntries(fg.sync('*', { |
| 49 | + cwd: absolute('./layouts'), |
| 50 | + absolute: true, |
| 51 | +}).map(i => [`layouts/${basename(i).replace(/\..*$/, '')}`, i])) |
| 52 | + |
| 53 | +const externals = [ |
| 54 | + '#slidev/', |
| 55 | + '/@slidev/', |
| 56 | + '@slidev/', |
| 57 | + 'server-reactive:', |
| 58 | + 'vue', |
| 59 | + 'vue-router', |
| 60 | + 'monaco-editor', |
| 61 | + 'typescript', |
| 62 | + 'mermaid', |
| 63 | + '~icons/', |
| 64 | +] |
| 65 | + |
| 66 | +export default defineConfig({ |
| 67 | + plugins: [ |
| 68 | + createInspectPlugin(options, pluginOptions), |
| 69 | + UnoCSS(), |
| 70 | + createVuePlugin(options, pluginOptions), |
| 71 | + Components({ |
| 72 | + extensions: ['vue', 'ts'], |
| 73 | + dirs: [absolute('./builtin')], |
| 74 | + resolvers: [ |
| 75 | + IconsResolver({ |
| 76 | + prefix: '', |
| 77 | + customCollections: Object.keys(pluginOptions.icons?.customCollections || []), |
| 78 | + }), |
| 79 | + ], |
| 80 | + dts: false, |
| 81 | + }), |
| 82 | + createIconsPlugin(options, pluginOptions), |
| 83 | + { |
| 84 | + name: 'slidev:flags', |
| 85 | + enforce: 'pre', |
| 86 | + transform(code, id) { |
| 87 | + if (id.match(/\.vue($|\?)/)) { |
| 88 | + const original = code |
| 89 | + defines.forEach((name) => { |
| 90 | + code = code.replaceAll(`_ctx.${name}`, name) |
| 91 | + }) |
| 92 | + if (original !== code) |
| 93 | + return code |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + build: { |
| 99 | + lib: { |
| 100 | + entry: { |
| 101 | + 'main': absolute('./main.ts'), |
| 102 | + 'index': absolute('./index.ts'), |
| 103 | + 'uno.css': absolute('./uno.ts'), |
| 104 | + ...builtinComponents, |
| 105 | + ...layoutComponents, |
| 106 | + }, |
| 107 | + formats: ['es'], |
| 108 | + }, |
| 109 | + target: 'esnext', |
| 110 | + rollupOptions: { |
| 111 | + external: source => externals.some(i => source.startsWith(i)), |
| 112 | + }, |
| 113 | + }, |
| 114 | +}) |
0 commit comments