feat(cli): specify config file

pull/3290/head
Waleed Khaled 7 months ago
parent 6461f5d42b
commit 550da4e6b5

@ -16,14 +16,15 @@ vitepress dev [root]
### Options
| Option | Description |
| --------------- | ----------------------------------------------------------------- |
| `--open [path]` | Open browser on startup (`boolean \| string`) |
| `--port <port>` | Specify port (`number`) |
| `--base <path>` | Public base path (default: `/`) (`string`) |
| `--cors` | Enable CORS |
| `--strictPort` | Exit if specified port is already in use (`boolean`) |
| `--force` | Force the optimizer to ignore the cache and re-bundle (`boolean`) |
| Option | Description |
| ----------------- | ----------------------------------------------------------------- |
| `--open [path]` | Open browser on startup (`boolean \| string`) |
| `--port <port>` | Specify port (`number`) |
| `--base <path>` | Public base path (default: `/`) (`string`) |
| `--config <file>` | Use specified config file (`string`) |
| `--cors` | Enable CORS |
| `--strictPort` | Exit if specified port is already in use (`boolean`) |
| `--force` | Force the optimizer to ignore the cache and re-bundle (`boolean`) |
## `vitepress build`
@ -41,6 +42,7 @@ vitepress build [root]
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------- |
| `--mpa` (experimental) | Build in [MPA mode](../guide/mpa-mode) without client-side hydration (`boolean`) |
| `--base <path>` | Public base path (default: `/`) (`string`) |
| `--config <file>` | Use specified config file (`string`) |
| `--target <target>` | Transpile target (default: `"modules"`) (`string`) |
| `--outDir <dir>` | Output directory relative to **cwd** (default: `<root>/.vitepress/dist`) (`string`) |
| `--minify [minifier]` | Enable/disable minification, or specify minifier to use (default: `"esbuild"`) (`boolean \| "terser" \| "esbuild"`) |

@ -18,12 +18,22 @@ import { renderPage } from './render'
export async function build(
root?: string,
buildOptions: BuildOptions & { base?: string; mpa?: string } = {}
buildOptions: BuildOptions & {
base?: string
mpa?: string
configFile?: string
} = {}
) {
const start = Date.now()
process.env.NODE_ENV = 'production'
const siteConfig = await resolveConfig(root, 'build', 'production')
const siteConfig = await resolveConfig(
root,
'build',
'production',
buildOptions.configFile
)
delete buildOptions.configFile
const unlinkVue = linkVue()
if (buildOptions.base) {

@ -22,6 +22,9 @@ if (root) {
let restartPromise: Promise<void> | undefined
argv.configFile = argv.config
delete argv.config
if (!command || command === 'dev') {
if (argv.force) {
delete argv.force

@ -55,7 +55,8 @@ export function defineConfigWithTheme<ThemeConfig>(
export async function resolveConfig(
root: string = process.cwd(),
command: 'serve' | 'build' = 'serve',
mode = 'development'
mode = 'development',
configFile?: string
): Promise<SiteConfig> {
// normalize root into absolute path
root = normalizePath(path.resolve(root))
@ -63,7 +64,8 @@ export async function resolveConfig(
const [userConfig, configPath, configDeps] = await resolveUserConfig(
root,
command,
mode
mode,
configFile
)
const logger =
@ -158,14 +160,15 @@ const supportedConfigExtensions = ['js', 'ts', 'mjs', 'mts']
export async function resolveUserConfig(
root: string,
command: 'serve' | 'build',
mode: string
mode: string,
configFile?: string
): Promise<[UserConfig, string | undefined, string[]]> {
// load user config
const configPath = supportedConfigExtensions
const configPath = (configFile ? [configFile] : supportedConfigExtensions
.flatMap((ext) => [
resolve(root, `config/index.${ext}`),
resolve(root, `config.${ext}`)
])
]))
.find(fs.pathExistsSync)
let userConfig: RawConfigExports = {}

@ -21,11 +21,17 @@ export interface ServeOptions {
base?: string
root?: string
port?: number
configFile?: string
}
export async function serve(options: ServeOptions = {}) {
const port = options.port ?? 4173
const config = await resolveConfig(options.root, 'serve', 'production')
const config = await resolveConfig(
options.root,
'serve',
'production',
options.configFile
)
const base = trimChar(options?.base ?? config?.site?.base ?? '', '/')
const notAnAsset = (pathname: string) =>

@ -4,10 +4,16 @@ import { createVitePressPlugin } from './plugin'
export async function createServer(
root: string = process.cwd(),
serverOptions: ServerOptions & { base?: string } = {},
serverOptions: ServerOptions & { base?: string; configFile?: string } = {},
recreateServer?: () => Promise<void>
) {
const config = await resolveConfig(root)
const config = await resolveConfig(
root,
'serve',
'development',
serverOptions.configFile
)
delete serverOptions.configFile
if (serverOptions.base) {
config.site.base = serverOptions.base

Loading…
Cancel
Save