|
| 1 | +# TypeScript Support for Config file |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### Support `.vuepress/config.ts` |
| 10 | + |
| 11 | +Previously, VuePress only supported these configurations |
| 12 | + |
| 13 | +- `.vuepress/config.js` |
| 14 | +- `.vuepress/config.yml` |
| 15 | +- `.vuepress/config.toml` |
| 16 | + |
| 17 | +From now on, `.vuepress/config.ts` get officially supported. |
| 18 | + |
| 19 | +### `defineConfig` helper for config intellisense |
| 20 | + |
| 21 | +A helper function exposed at `vuepress/config`, which helps you to have type prompt: |
| 22 | + |
| 23 | +```ts |
| 24 | +import { defineConfig } from "vuepress/config"; |
| 25 | + |
| 26 | +export default defineConfig({ |
| 27 | + title: "VuePress", |
| 28 | + description: "Vue-powered Static Site Generator" |
| 29 | + // ... |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +### `Typed` Theme Config |
| 34 | + |
| 35 | +By default, `defineConfig` helper leverages the theme config type from default theme: |
| 36 | + |
| 37 | +```js |
| 38 | +import { defineConfig } from "vuepress/config"; |
| 39 | + |
| 40 | +export default defineConfig({ |
| 41 | + themeConfig: { |
| 42 | + repo: "vuejs/vuepress", |
| 43 | + editLinks: true, |
| 44 | + docsDir: "packages/docs/docs" |
| 45 | + // Type is `DefaultThemeConfig` |
| 46 | + } |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +If you use a custom theme, you can use the `defineConfig4CustomTheme` helper with ability to pass generic type for your theme: |
| 51 | + |
| 52 | +```ts |
| 53 | +import { defineConfig4CustomTheme } from "vuepress/config"; |
| 54 | + |
| 55 | +interface MyThemeConfig { |
| 56 | + hello: string; |
| 57 | +} |
| 58 | + |
| 59 | +export default defineConfig4CustomTheme<MyThemeConfig>( |
| 60 | + { |
| 61 | + themeConfig: { |
| 62 | + // Type is `MyThemeConfig` |
| 63 | + hello: "vuepress" |
| 64 | + } |
| 65 | + }, |
| 66 | +``` |
| 67 | +
|
| 68 | +### Type Inferences for Official Plugins |
| 69 | +
|
| 70 | +From now, you’ll be able to enjoy the type prompt of the official plugins: |
| 71 | +
|
| 72 | + |
| 73 | +
|
| 74 | +Options of the official plugins certainly have type prompts, **Both [Tuple Style](https://vuepress.vuejs.org/plugin/using-a-plugin.html#plugin-options) and [Object Style](https://vuepress.vuejs.org/plugin/using-a-plugin.html#plugin-options), and [Plugin Shorthand](https://vuepress.vuejs.org/plugin/using-a-plugin.html#plugin-shorthand) support type inference**: |
| 75 | +
|
| 76 | +- Tuple Style: |
| 77 | +
|
| 78 | + |
| 79 | +
|
| 80 | +```ts |
| 81 | +import { defineConfig } from 'vuepress/config' |
| 82 | + |
| 83 | +export default defineConfig({ |
| 84 | + plugins: [ |
| 85 | + [ |
| 86 | + '@vuepress/pwa', |
| 87 | + { |
| 88 | + serviceWorker: true |
| 89 | + } |
| 90 | + ] |
| 91 | + ] |
| 92 | +}) |
| 93 | +``` |
| 94 | +
|
| 95 | +- Object Style: |
| 96 | +
|
| 97 | +```ts |
| 98 | +import { defineConfig } from 'vuepress/config' |
| 99 | + |
| 100 | +export default defineConfig({ |
| 101 | + plugins: { |
| 102 | + '@vuepress/pwa': { |
| 103 | + serviceWorker: true |
| 104 | + } |
| 105 | + } |
| 106 | +}) |
| 107 | +``` |
| 108 | +
|
| 109 | +The illustration snapshot is omitted here, you can try it yourself. |
| 110 | +
|
| 111 | +
|
| 112 | +### ISO Language Code |
| 113 | +
|
| 114 | +Type inference supports [ISO Language Code](http://www.lingoes.net/en/translator/langcode.htm) |
| 115 | +
|
| 116 | + |
| 117 | +
|
| 118 | +
|
| 119 | +### Context API |
| 120 | +
|
| 121 | +VuePress's configuration can also be a function, while its first parameter is the current [app context](https://vuepress.vuejs.org/plugin/context-api.html#context-api): |
| 122 | +
|
| 123 | +```ts |
| 124 | +import { defineConfig } from "vuepress/config"; |
| 125 | + |
| 126 | +export default defineConfig(ctx => ({ |
| 127 | + // do not execute babel compilation under development |
| 128 | + evergreen: ctx.isProd |
| 129 | +})); |
| 130 | +``` |
| 131 | +
|
| 132 | +## Limitations |
| 133 | +
|
| 134 | +It is worth noting that third-party plugins do not support [Plugin Shorthand](https://vuepress.vuejs.org/plugin/using-a-plugin.html#plugin-shorthand) if you're using [Tuple Style](https://vuepress.vuejs.org/plugin/using-a-plugin.html#plugin-options) to write your config, this is because from the perspective of the type system, the unknown shortcut is equivalent to `string`, which results in the failure of type inference. |
| 135 | +
|
| 136 | +By default, only officially maintained and plugins under [VuePress Community](https://vuepress-community.netlify.app/en/) support shortcut, feel free to submit pull request to add your plugin at this [file](https://github.com/vuejs/vuepress/blob/master/packages/vuepress/types/third-party-plugins.ts). |
| 137 | +
|
| 138 | +
|
| 139 | +
|
| 140 | +## Credits |
| 141 | +
|
| 142 | +- [bundle-require](https://github.com/egoist/bundle-require) (by [@egoist](https://github.com/egoist)): we leverage it under the hood to resolve user's config, which is powered by `esbuild`. |
| 143 | +- [vscode-ts-in-markdown](https://github.com/Amour1688/vscode-ts-in-markdown) (by [@Amour1688](https://github.com/Amour1688)): this documentation is powered by this plugin, which make type checking possible in markdown. |
0 commit comments