返回 slidev
config-vite.md
根目录 / docs / custom / config-vite.md
1 # Configure Vite and Plugins
2
3 <Environment type="node" />
4
5 Slidev is powered by [Vite](https://vitejs.dev/) under the hood. This means you can leverage Vite's great plugin system to customize your slides even further.
6
7 The `vite.config.ts` will be respected if you have one, and will be merged with the Vite config provided by Slidev, your theme and the addons.
8
9 ## Configure Internal Plugins
10
11 Slidev internally adds the following plugins to Vite:
12
13 - [@vitejs/plugin-vue](https://github.com/vitejs/vite-plugin-vue)
14 - [unplugin-vue-components](https://github.com/unplugin/unplugin-vue-components)
15 - [unplugin-icons](https://github.com/unplugin/unplugin-icons)
16 - [vite-plugin-vue-markdown](https://github.com/unplugin/unplugin-vue-markdown)
17 - [vite-plugin-remote-assets](https://github.com/antfu/vite-plugin-remote-assets)
18 - [unocss/vite](https://github.com/unocss/unocss/tree/main/packages-integrations/vite)
19
20 To configure the built-in plugins listed above, create a `vite.config.ts` with the following content. Please note that Slidev has some [default configurations](https://github.com/slidevjs/slidev/blob/main/packages/slidev/node/vite/index.ts) for those plugins, this usage will override some of them, which may potentially cause the app to break. Please treat this as **an advanced feature**, and make sure you know what you are doing before moving on.
21
22 ```ts twoslash [vite.config.ts]
23 /* eslint-disable import/first */
24 import type MarkdownExit from 'markdown-exit'
25
26 declare const MyPlugin: (md: any) => void
27 // ---cut---
28 import { defineViteConfig } from '@slidev/types'
29
30 export default defineViteConfig({
31 slidev: {
32 vue: {
33 /* vue options */
34 },
35 markdown: {
36 /* markdown-exit options */
37 markdownSetup(md) {
38 /* custom markdown-exit plugins */
39 md.use(MyPlugin)
40 },
41 },
42 /* options for other plugins */
43 },
44 })
45 ```
46
47 See the [type declarations](https://github.com/slidevjs/slidev/blob/main/packages/types/src/vite.ts#L11) for more options.
48
49 `defineViteConfig` is a drop-in replacement for Vite's `defineConfig`. The `slidev` field is declared by `@slidev/types`, and TypeScript only picks that declaration up when the package is part of your program, which is what importing the helper does. If you prefer Vite's own `defineConfig`, reference the types at the top of the file instead:
50
51 ```ts
52 /// <reference types="@slidev/types" />
53 ```
54
55 ::: warning
56 It is not allowed to re-add plugins that has been used internally be Slidev. For example, instead of
57
58 ```ts twoslash
59 import Vue from '@vitejs/plugin-vue'
60 import { defineConfig } from 'vite'
61
62 export default defineConfig({
63 plugins: [
64 Vue({
65 /* vue options */
66 })
67 ],
68 })
69 ```
70
71 Please pass the Vue options to the `slidev.vue` field as described above
72 :::
73
74 ## Add Custom Plugins based on Slide data
75
76 Usually you can add Vite plugins into your `vite.config.ts` (see above).
77 However, if you want to add plugins based on the slide data, you need to add a `./setup/vite-plugins.ts` with the following content:
78
79 ```ts twoslash
80 import { defineVitePluginsSetup } from '@slidev/types'
81
82 export default defineVitePluginsSetup((options) => {
83 return [
84 // Your plugins here
85 // Slide data is available as options.data.slides
86 ]
87 })
88 ```
89
89 lines MARKDOWN