-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
], | ||
"rules": { | ||
"prettier/prettier": [ | ||
"error", | ||
"warn", | ||
{ | ||
"singleQuote": true, | ||
"semi": false, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { defineConfig } from './config' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters