mirror of https://github.com/YunYouJun/valaxy
feat: can resolve themeConfig from theme.config.ts
This commit is contained in:
parent
d3c537696e
commit
33922602e9
|
@ -45,7 +45,7 @@ For a example, you can see [demo/yun](./demo/yun/) folder.
|
|||
|
||||
## Features
|
||||
|
||||
- ⚡️ [Vue 3](https://github.com/vuejs/vue-next), [Vite 3](https://github.com/vitejs/vite), [pnpm](https://pnpm.js.org/), [ESBuild](https://github.com/evanw/esbuild) - born with fastness
|
||||
- ⚡️ [Vue 3](https://github.com/vuejs/vue-next), [Vite 4](https://github.com/vitejs/vite), [pnpm](https://pnpm.js.org/), [ESBuild](https://github.com/evanw/esbuild) - born with fastness
|
||||
- 🔥 Hot Reload with Config & Markdown
|
||||
- 🔧 Type Tooltip for all config by `valaxy.config.ts`
|
||||
- 🗒 Extended Markdown Frontmatter
|
||||
|
|
|
@ -45,7 +45,7 @@ pnpm create valaxy
|
|||
|
||||
## 功能
|
||||
|
||||
- ⚡️ [Vue 3](https://github.com/vuejs/vue-next), [Vite 3](https://github.com/vitejs/vite), [pnpm](https://pnpm.js.org/), [ESBuild](https://github.com/evanw/esbuild) - 快速
|
||||
- ⚡️ [Vue 3](https://github.com/vuejs/vue-next), [Vite 4](https://github.com/vitejs/vite), [pnpm](https://pnpm.js.org/), [ESBuild](https://github.com/evanw/esbuild) - 快速
|
||||
- 🔥 配置 & Markdown 文件热更新
|
||||
- 🔧 `valaxy.config.ts` 的所有配置项皆有类型提示
|
||||
- 🗒 扩展 Markdown Frontmatter
|
||||
|
|
|
@ -15,7 +15,7 @@ export function useRandomData<T>(source: string | T[], random = false) {
|
|||
if (typeof source === 'string') {
|
||||
if (!isClient)
|
||||
return
|
||||
rawData = await fetch(source).then(res => res.json()) as T[]
|
||||
rawData = (await fetch(source).then(res => res.json()) as T[]) || []
|
||||
}
|
||||
else { rawData = source }
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import type { UserSiteConfig } from 'valaxy'
|
||||
import { cyan } from 'kolorist'
|
||||
import { cyan, dim } from 'kolorist'
|
||||
import { logger } from '../logger'
|
||||
import { loadConfigFromFile } from './utils'
|
||||
|
||||
|
@ -9,7 +9,6 @@ import { loadConfigFromFile } from './utils'
|
|||
* @returns
|
||||
*/
|
||||
export async function resolveSiteConfigFromRoot(root: string) {
|
||||
logger.info(`Resolve ${cyan('site.config.ts')}`)
|
||||
return loadConfigFromFile<UserSiteConfig>('site.config', {
|
||||
cwd: root,
|
||||
})
|
||||
|
@ -22,6 +21,8 @@ export async function resolveSiteConfigFromRoot(root: string) {
|
|||
*/
|
||||
export async function resolveSiteConfig(root: string) {
|
||||
const { config: userSiteConfig, configFile: siteConfigFile } = await resolveSiteConfigFromRoot(root)
|
||||
if (userSiteConfig)
|
||||
logger.info(`Resolve ${cyan('siteConfig')} from ${dim(siteConfigFile)}`)
|
||||
|
||||
return {
|
||||
siteConfig: userSiteConfig,
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
import type { UserSiteConfig } from 'valaxy'
|
||||
import { cyan, dim } from 'kolorist'
|
||||
import { logger } from '../logger'
|
||||
import { loadConfigFromFile } from './utils'
|
||||
|
||||
/**
|
||||
* resolve theme config from special root
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
export async function resolveThemeConfigFromRoot(root: string) {
|
||||
return loadConfigFromFile<UserSiteConfig>('theme.config', {
|
||||
cwd: root,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* resolve theme.config.ts and merge with default
|
||||
* @param root
|
||||
* @returns
|
||||
*/
|
||||
export async function resolveThemeConfig(root: string) {
|
||||
const { config: userThemeConfig, configFile: themeConfigFile } = await resolveThemeConfigFromRoot(root)
|
||||
|
||||
if (userThemeConfig)
|
||||
logger.info(`Resolve ${cyan('themeConfig')} from ${dim(themeConfigFile)}`)
|
||||
|
||||
return {
|
||||
themeConfig: userThemeConfig,
|
||||
themeConfigFile,
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ import { defaultValaxyConfig } from './config'
|
|||
import { parseAddons } from './utils/addons'
|
||||
import { getThemeRoot } from './utils/theme'
|
||||
import { resolveSiteConfig } from './config/site'
|
||||
import { resolveThemeConfig } from './config/theme'
|
||||
|
||||
// for cli entry
|
||||
export interface ValaxyEntryOptions {
|
||||
|
@ -67,6 +68,7 @@ export interface ResolvedValaxyOptions<ThemeConfig = DefaultThemeConfig> {
|
|||
*/
|
||||
configFile: string
|
||||
siteConfigFile: string
|
||||
themeConfigFile: string
|
||||
pages: string[]
|
||||
/**
|
||||
* all addons
|
||||
|
@ -151,9 +153,10 @@ export async function resolveOptions(
|
|||
|
||||
let { config: userValaxyConfig, configFile, theme } = await resolveValaxyConfig(options)
|
||||
const { siteConfig, siteConfigFile } = await resolveSiteConfig(options.userRoot)
|
||||
const { themeConfig, themeConfigFile } = await resolveThemeConfig(options.userRoot)
|
||||
|
||||
// merge with valaxy
|
||||
userValaxyConfig = defu<ValaxyNodeConfig, any>({ siteConfig }, userValaxyConfig)
|
||||
userValaxyConfig = defu<ValaxyNodeConfig, any>({ siteConfig }, { themeConfig }, userValaxyConfig)
|
||||
|
||||
const themeRoot = getThemeRoot(theme, options.userRoot)
|
||||
|
||||
|
@ -185,6 +188,7 @@ export async function resolveOptions(
|
|||
},
|
||||
configFile: configFile || '',
|
||||
siteConfigFile: siteConfigFile || '',
|
||||
themeConfigFile: themeConfigFile || '',
|
||||
pages,
|
||||
addons: [],
|
||||
}
|
||||
|
|
|
@ -255,12 +255,19 @@ export function createValaxyPlugin(options: ResolvedValaxyOptions, serverOptions
|
|||
return reloadConfigAndEntries(config)
|
||||
}
|
||||
|
||||
// siteConfig
|
||||
if (file === options.siteConfigFile) {
|
||||
const { siteConfig } = await resolveSiteConfig(options.userRoot)
|
||||
valaxyConfig.siteConfig = defu<SiteConfig, [SiteConfig]>(siteConfig, defaultSiteConfig)
|
||||
return reloadConfigAndEntries(valaxyConfig)
|
||||
}
|
||||
|
||||
// themeConfig
|
||||
if (file === options.themeConfigFile) {
|
||||
const { config } = await resolveOptions({ userRoot: options.userRoot })
|
||||
return reloadConfigAndEntries(config)
|
||||
}
|
||||
|
||||
if (file === resolve(options.themeRoot, 'valaxy.config.ts')) {
|
||||
const themeValaxyConfig = resolveThemeValaxyConfig(options)
|
||||
const valaxyConfig = mergeValaxyConfig(options.config, themeValaxyConfig)
|
||||
|
|
|
@ -2,7 +2,7 @@ import { mergeConfig as mergeViteConfig } from 'vite'
|
|||
import { createDefu } from 'defu'
|
||||
import { isFunction } from '@antfu/utils'
|
||||
|
||||
import { cyan } from 'kolorist'
|
||||
import { cyan, dim } from 'kolorist'
|
||||
import type { ResolvedValaxyOptions, ValaxyEntryOptions } from '../options'
|
||||
import type { ValaxyAddonFn, ValaxyAddonResolver, ValaxyConfigFn, ValaxyNodeConfig } from '../types'
|
||||
import { loadConfigFromFile } from '../config/utils'
|
||||
|
@ -48,11 +48,11 @@ export async function resolveValaxyConfigFromRoot(root: string, options?: Resolv
|
|||
* @returns
|
||||
*/
|
||||
export async function resolveValaxyConfig(options: ValaxyEntryOptions) {
|
||||
logger.info(`Resolve ${cyan('valaxy.config.ts')}`)
|
||||
|
||||
// const resolved = await mergeValaxyConfig(options)
|
||||
// valaxyConfig = mergeValaxyConfig(valaxyConfig, config)
|
||||
const { config: userValaxyConfig, configFile } = await resolveValaxyConfigFromRoot(options.userRoot || process.cwd())
|
||||
if (userValaxyConfig)
|
||||
logger.info(`Resolve ${cyan('valaxyConfig')} from ${dim(configFile)}`)
|
||||
|
||||
const theme = options.theme || userValaxyConfig.theme || 'yun'
|
||||
|
||||
|
|
Loading…
Reference in New Issue