Skip to content

Commit

Permalink
feat: support ts/esm config file + defineConfig() helper
Browse files Browse the repository at this point in the history
close #339, close #376
  • Loading branch information
yyx990803 committed Oct 7, 2021
1 parent 4c042b6 commit d3b1521
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.js → docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
lang: 'en-US',
title: 'VitePress',
description: 'Vite & Vue powered static site generator.',
Expand Down
2 changes: 1 addition & 1 deletion src/node/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function build(
const start = Date.now()

process.env.NODE_ENV = 'production'
const siteConfig = await resolveConfig(root)
const siteConfig = await resolveConfig(root, 'build', 'production')

if (buildOptions.mpa) {
siteConfig.mpa = true
Expand Down
66 changes: 51 additions & 15 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
normalizePath,
AliasOptions,
UserConfig as ViteConfig,
mergeConfig as mergeViteConfig
mergeConfig as mergeViteConfig,
loadConfigFromFile
} from 'vite'
import { Options as VuePluginOptions } from '@vitejs/plugin-vue'
import {
Expand Down Expand Up @@ -63,7 +64,7 @@ export interface SiteConfig<ThemeConfig = any> {
root: string
srcDir: string
site: SiteData<ThemeConfig>
configPath: string
configPath: string | undefined
themeDir: string
outDir: string
tempDir: string
Expand All @@ -78,10 +79,19 @@ export interface SiteConfig<ThemeConfig = any> {
const resolve = (root: string, file: string) =>
normalizePath(path.resolve(root, `.vitepress`, file))

/**
* Type config helper
*/
export function defineConfig(config: RawConfigExports) {
return config
}

export async function resolveConfig(
root: string = process.cwd()
root: string = process.cwd(),
command: 'serve' | 'build' = 'serve',
mode = 'development'
): Promise<SiteConfig> {
const userConfig = await resolveUserConfig(root)
const [userConfig, configPath] = await resolveUserConfig(root, command, mode)
const site = await resolveSiteData(root, userConfig)
const srcDir = path.resolve(root, userConfig.srcDir || '.')

Expand Down Expand Up @@ -110,7 +120,7 @@ export async function resolveConfig(
site,
themeDir,
pages,
configPath: resolve(root, 'config.js'),
configPath,
outDir: resolve(root, 'dist'),
tempDir: path.resolve(APP_PATH, 'temp'),
markdown: userConfig.markdown,
Expand All @@ -123,19 +133,43 @@ export async function resolveConfig(
return config
}

export async function resolveUserConfig(root: string): Promise<UserConfig> {
const supportedConfigExtensions = ['js', 'ts', '.mjs', 'mts']

async function resolveUserConfig(
root: string,
command: 'serve' | 'build',
mode: string
): Promise<[UserConfig, string | undefined]> {
// load user config
const configPath = resolve(root, 'config.js')
const hasUserConfig = await fs.pathExists(configPath)
// always delete cache first before loading config
delete require.cache[require.resolve(configPath)]
const userConfig: RawConfigExports = hasUserConfig ? require(configPath) : {}
if (hasUserConfig) {
let configPath
for (const ext of supportedConfigExtensions) {
const p = resolve(root, `config.${ext}`)
if (await fs.pathExists(p)) {
configPath = p
break
}
}

const userConfig: RawConfigExports = configPath
? ((
await loadConfigFromFile(
{
command,
mode
},
configPath,
root
)
)?.config as any)
: {}

if (configPath) {
debug(`loaded config at ${chalk.yellow(configPath)}`)
} else {
debug(`no config file found.`)
}
return resolveConfigExtends(userConfig)

return [await resolveConfigExtends(userConfig), configPath]
}

async function resolveConfigExtends(
Expand Down Expand Up @@ -180,9 +214,11 @@ function isObject(value: unknown): value is Record<string, any> {

export async function resolveSiteData(
root: string,
userConfig?: UserConfig
userConfig?: UserConfig,
command: 'serve' | 'build' = 'serve',
mode = 'development'
): Promise<SiteData> {
userConfig = userConfig || (await resolveUserConfig(root))
userConfig = userConfig || (await resolveUserConfig(root, command, mode))[0]
return {
lang: userConfig.lang || 'en-US',
title: userConfig.title || 'VitePress',
Expand Down
4 changes: 3 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export function createVitePressPlugin(
},

configureServer(server) {
server.watcher.add(configPath)
if (configPath) {
server.watcher.add(configPath)
}

// serve our index.html after vite history fallback
return () => {
Expand Down
2 changes: 1 addition & 1 deletion src/node/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ServeOptions {

export async function serve(options: ServeOptions = {}) {
const port = options.port !== undefined ? options.port : 5000
const site = await resolveConfig(options.root)
const site = await resolveConfig(options.root, 'serve', 'production')

const compress = compression()
const serve = sirv(site.outDir, {
Expand Down

0 comments on commit d3b1521

Please sign in to comment.