Skip to content

Commit

Permalink
feat(cli): specify config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Waleed-KH committed Jan 4, 2024
1 parent d279e63 commit 9254dd5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
18 changes: 10 additions & 8 deletions docs/reference/cli.md
Expand Up @@ -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`

Expand All @@ -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"`) |
Expand Down
14 changes: 12 additions & 2 deletions src/node/build/build.ts
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/node/cli.ts
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/node/config.ts
Expand Up @@ -55,15 +55,17 @@ 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))

const [userConfig, configPath, configDeps] = await resolveUserConfig(
root,
command,
mode
mode,
configFile
)

const logger =
Expand Down Expand Up @@ -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 = {}
Expand Down
8 changes: 7 additions & 1 deletion src/node/serve/serve.ts
Expand Up @@ -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) =>
Expand Down
10 changes: 8 additions & 2 deletions src/node/server.ts
Expand Up @@ -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
Expand Down

0 comments on commit 9254dd5

Please sign in to comment.