Skip to content

Commit

Permalink
feat: load userConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dedance committed Oct 14, 2023
1 parent 4b9cafc commit 7a7b3de
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"rules": {
"prettier/prettier": [
"error",
"warn",
{
"singleQuote": true,
"semi": false,
Expand Down
5 changes: 5 additions & 0 deletions e2e/playground/basic/docs/rpress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'r-press/dist'
export default defineConfig({
title: 'RPress',
description: 'RPress Playground',
})
1 change: 1 addition & 0 deletions e2e/playground/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "basic",
"type": "module",
"scripts": {
"dev": "r-press dev docs",
"build": "r-press build docs"
Expand Down
3 changes: 3 additions & 0 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { createDevServer } from './dev'
import { build } from './build'

import { version } from '../../package.json'
import { resolveConfig } from './config'

const cli = cac('r-press').version(version).help()

cli
.command('[root]', 'start dev server')
.alias('dev')
.action(async (root: string) => {
await resolveConfig(root, 'serve', 'development')
const server = await createDevServer(root)
await server.listen()
server.printUrls()
Expand All @@ -18,6 +20,7 @@ cli
cli
.command('build [root]', 'build for production')
.action(async (root: string) => {
await resolveConfig(root, 'build', 'production')
await build(root)
})

Expand Down
73 changes: 73 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import path from 'path'
import fs from 'fs-extra'
import { loadConfigFromFile } from 'vite'
import type { SiteConfig, UserConfig } from '../shared/types'

type RawUserConfig =
| UserConfig
| Promise<UserConfig>
| (() => UserConfig | Promise<UserConfig>)

export async function resolveConfig(
root: string,
command: 'serve' | 'build',
mode: 'development' | 'production'
): Promise<SiteConfig> {
const [configPath, userConfig] = await resolveUserConfig(root, command, mode)
const siteConfig: SiteConfig = {
root,
configPath: configPath,
siteData: resolveSiteData(userConfig as UserConfig),
}
console.log(siteConfig, 'siteConfig')
return siteConfig
}

export async function resolveUserConfig(
root: string,
command: 'serve' | 'build',
mode: 'development' | 'production'
) {
// 1. 获取配置路径 支持js 和 ts 格式
const configPath = getUserConfigPath(root)
// 2.解析配置文件
const { config: rawConfig = {} as RawUserConfig } = await loadConfigFromFile(
{
command,
mode,
},
configPath,
root
)
if (rawConfig) {
const config =
typeof rawConfig === 'function' ? await rawConfig() : rawConfig
return [configPath, config] as const
}
return [configPath, {} as UserConfig] as const
}

export function resolveSiteData(userConfig: UserConfig): UserConfig {
return {
title: userConfig.title || 'Rpress.js',
description: userConfig.description || 'SSG Framework',
themeConfig: userConfig.themeConfig || {},
vite: userConfig.vite || {},
}
}

function getUserConfigPath(root: string) {
const configFileNames = ['rpress.config.ts', 'rpress.config.js']
try {
const configPath = configFileNames
.map((name) => path.resolve(root, name))
.find(fs.pathExistsSync)
return configPath
} catch (e) {
console.error('Failed to load config file.', e)
throw e
}
}
export function defineConfig(config: UserConfig): UserConfig {
return config
}
1 change: 1 addition & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { defineConfig } from './config'
42 changes: 42 additions & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { UserConfig as ViteUserConfig } from 'vite'

// 用户配置的超集
export interface SiteConfig {
root: string
configPath: string
siteData: UserConfig
}

export interface UserConfig {
title?: string
description?: string
themeConfig?: ThemeConfig
vite?: ViteUserConfig
}

// 主题配置:导航栏 侧边栏
export interface ThemeConfig {
nav?: NavItemWithLink[]
sidebar?: Sidebar
footer?: Footer
}

type NavItemWithLink = {
text: string
link: string
}
export interface Sidebar {
[path: string]: SidebarGroup[]
}
export interface Footer {
message: string
}

export interface SidebarGroup {
text: string
items: SidebarItem[]
}
type SidebarItem = {
text: string
link: string
}
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entryPoints: ['src/node/cli.ts'],
entryPoints: ['src/node/cli.ts', 'src/node/index.ts'],
bundle: true,
splitting: true,
outDir: 'dist',
Expand Down

0 comments on commit 7a7b3de

Please sign in to comment.