diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 85517620bfaa..4ff30697c30b 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -16,14 +16,15 @@ vitepress dev [root] ### Options -| Option | Description | -| --------------- | ----------------------------------------------------------------- | -| `--open [path]` | Open browser on startup (`boolean \| string`) | -| `--port ` | Specify port (`number`) | -| `--base ` | 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 ` | Specify port (`number`) | +| `--base ` | Public base path (default: `/`) (`string`) | +| `--config ` | 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 ` | Public base path (default: `/`) (`string`) | +| `--config ` | Use specified config file (`string`) | | `--target ` | Transpile target (default: `"modules"`) (`string`) | | `--outDir ` | Output directory relative to **cwd** (default: `/.vitepress/dist`) (`string`) | | `--minify [minifier]` | Enable/disable minification, or specify minifier to use (default: `"esbuild"`) (`boolean \| "terser" \| "esbuild"`) | diff --git a/src/node/build/build.ts b/src/node/build/build.ts index d6177864ebc0..e4c12eb7fe17 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -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) { diff --git a/src/node/cli.ts b/src/node/cli.ts index 71eb4c0f26b4..36fa8f95929c 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -20,6 +20,9 @@ if (root) { argv.root = root } +argv.configFile = argv.config +delete argv.config + if (!command || command === 'dev') { if (argv.force) { delete argv.force diff --git a/src/node/config.ts b/src/node/config.ts index 6d1bbd4938c1..71d365f69ae8 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -55,7 +55,8 @@ export function defineConfigWithTheme( export async function resolveConfig( root: string = process.cwd(), command: 'serve' | 'build' = 'serve', - mode = 'development' + mode = 'development', + configFile?: string ): Promise { // 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 = {} diff --git a/src/node/serve/serve.ts b/src/node/serve/serve.ts index e2434e902b95..4cb7e0004ae3 100644 --- a/src/node/serve/serve.ts +++ b/src/node/serve/serve.ts @@ -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) => diff --git a/src/node/server.ts b/src/node/server.ts index 4105edfe5aaa..dd3f6d8ae655 100644 --- a/src/node/server.ts +++ b/src/node/server.ts @@ -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 ) { - 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