feat: support passing vite config in vitepress config file via `vite` option

BREAKING CHANGE: Some config options have changed.

    - `vueOptions` renamed to `vue`
    - `alias` option has been removed. Use `vite.resovle.alias` instead.
pull/317/head
Evan You 3 years ago
parent 5733fc625e
commit 3737b1055d

@ -22,7 +22,6 @@ export function resolveAliases(
userConfig: UserConfig
): AliasOptions {
const paths: Record<string, string> = {
...userConfig.alias,
'/@theme': themeDir,
'/@shared': SHARED_PATH,
[SITE_DATA_ID]: SITE_DATA_REQUEST_PATH

@ -2,7 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import chalk from 'chalk'
import globby from 'globby'
import { AliasOptions } from 'vite'
import { AliasOptions, UserConfig as ViteConfig } from 'vite'
import { Options as VuePluginOptions } from '@vitejs/plugin-vue'
import { SiteData, HeadConfig, LocaleConfig } from './shared'
import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias'
@ -20,10 +20,16 @@ export interface UserConfig<ThemeConfig = any> {
head?: HeadConfig[]
themeConfig?: ThemeConfig
locales?: Record<string, LocaleConfig>
alias?: Record<string, string>
markdown?: MarkdownOptions
/**
* Opitons to pass on to @vitejs/plugin-vue
*/
vue?: VuePluginOptions
/**
* Vite config
*/
vite?: ViteConfig
customData?: any
vueOptions?: VuePluginOptions
exclude?: string[]
}
@ -36,8 +42,9 @@ export interface SiteConfig<ThemeConfig = any> {
tempDir: string
alias: AliasOptions
pages: string[]
markdown?: MarkdownOptions
vueOptions?: VuePluginOptions
markdown: MarkdownOptions | undefined
vue: VuePluginOptions | undefined
vite: ViteConfig | undefined
}
const resolve = (root: string, file: string) =>
@ -68,7 +75,8 @@ export async function resolveConfig(
tempDir: path.resolve(APP_PATH, 'temp'),
markdown: userConfig.markdown,
alias: resolveAliases(themeDir, userConfig),
vueOptions: userConfig.vueOptions
vue: userConfig.vue,
vite: userConfig.vite
}
return config

@ -1,5 +1,5 @@
import path from 'path'
import { Plugin, ResolvedConfig } from 'vite'
import { mergeConfig, Plugin, ResolvedConfig } from 'vite'
import { SiteConfig, resolveSiteData } from './config'
import { createMarkdownToVueRenderFn } from './markdownToVue'
import { APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
@ -8,7 +8,8 @@ import { slash } from './utils/slash'
import { OutputAsset, OutputChunk } from 'rollup'
const hashRE = /\.(\w+)\.js$/
const staticInjectMarkerRE = /\b(const _hoisted_\d+ = \/\*#__PURE__\*\/createStaticVNode)\("(.*)", (\d+)\)/g
const staticInjectMarkerRE =
/\b(const _hoisted_\d+ = \/\*#__PURE__\*\/createStaticVNode)\("(.*)", (\d+)\)/g
const staticStripRE = /__VP_STATIC_START__.*?__VP_STATIC_END__/g
const staticRestoreRE = /__VP_STATIC_(START|END)__/g
@ -24,7 +25,15 @@ const isPageChunk = (
export function createVitePressPlugin(
root: string,
{ configPath, alias, markdown, site, vueOptions, pages }: SiteConfig,
{
configPath,
alias,
markdown,
site,
vue: userVuePluginOptions,
vite: userViteConfig,
pages
}: SiteConfig,
ssr = false,
pageToHashMap?: Record<string, string>
): Plugin[] {
@ -32,7 +41,7 @@ export function createVitePressPlugin(
const vuePlugin = createVuePlugin({
include: [/\.vue$/, /\.md$/],
...vueOptions
...userVuePluginOptions
})
let siteData = site
@ -47,7 +56,7 @@ export function createVitePressPlugin(
},
config() {
return {
const baseConfig = {
resolve: {
alias
},
@ -60,6 +69,9 @@ export function createVitePressPlugin(
exclude: ['@docsearch/js']
}
}
return userViteConfig
? mergeConfig(userViteConfig, baseConfig)
: baseConfig
},
resolveId(id) {

Loading…
Cancel
Save