Skip to content

Commit

Permalink
feat: support passing vite config in vitepress config file via vite
Browse files Browse the repository at this point in the history
… option

BREAKING CHANGE: Some config options have changed.

    - `vueOptions` renamed to `vue`
    - `alias` option has been removed. Use `vite.resovle.alias` instead.
  • Loading branch information
yyx990803 committed Jun 14, 2021
1 parent 5733fc6 commit 3737b10
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/node/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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[]
}

Expand All @@ -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) =>
Expand Down Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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

Expand All @@ -24,15 +25,23 @@ 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[] {
const markdownToVue = createMarkdownToVueRenderFn(root, markdown, pages)

const vuePlugin = createVuePlugin({
include: [/\.vue$/, /\.md$/],
...vueOptions
...userVuePluginOptions
})

let siteData = site
Expand All @@ -47,7 +56,7 @@ export function createVitePressPlugin(
},

config() {
return {
const baseConfig = {
resolve: {
alias
},
Expand All @@ -60,6 +69,9 @@ export function createVitePressPlugin(
exclude: ['@docsearch/js']
}
}
return userViteConfig
? mergeConfig(userViteConfig, baseConfig)
: baseConfig
},

resolveId(id) {
Expand Down

0 comments on commit 3737b10

Please sign in to comment.