diff --git a/.changeset/modern-keys-spend.md b/.changeset/modern-keys-spend.md new file mode 100644 index 0000000000..551b310e7d --- /dev/null +++ b/.changeset/modern-keys-spend.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/rspeedy": patch +--- + +Support cli flag `--base` to specify the base path of the server. diff --git a/packages/rspeedy/core/src/cli/commands.ts b/packages/rspeedy/core/src/cli/commands.ts index ac01178830..be7432ac5f 100644 --- a/packages/rspeedy/core/src/cli/commands.ts +++ b/packages/rspeedy/core/src/cli/commands.ts @@ -53,6 +53,7 @@ export function apply(program: Command): Command { .description( 'Run the dev server and watch for source file changes while serving.', ) + .option('--base ', 'specify the base path of the server') .action( (devOptions: DevOptions) => import('./dev.js').then(({ dev }) => @@ -75,6 +76,7 @@ export function apply(program: Command): Command { const previewCommand = program.command('preview') previewCommand .description('Preview the production build outputs locally.') + .option('--base ', 'specify the base path of the server') .action((previewOptions: PreviewOptions) => import('./preview.js').then(({ preview }) => preview.call(previewCommand, cwd, previewOptions) diff --git a/packages/rspeedy/core/src/cli/dev.ts b/packages/rspeedy/core/src/cli/dev.ts index 91f1a79d2f..d85626922d 100644 --- a/packages/rspeedy/core/src/cli/dev.ts +++ b/packages/rspeedy/core/src/cli/dev.ts @@ -13,7 +13,9 @@ import { loadConfig, resolveConfigPath } from '../config/loadConfig.js' import { createRspeedy } from '../create-rspeedy.js' import { exit } from './exit.js' -export type DevOptions = CommonOptions +export interface DevOptions extends CommonOptions { + base?: string | undefined +} export async function dev( this: Command, @@ -29,6 +31,11 @@ export async function dev( configPath, }) + if (devOptions.base) { + rspeedyConfig.server ??= {} + rspeedyConfig.server.base = devOptions.base + } + const watchedFiles = [configPath] if (Array.isArray(rspeedyConfig.dev?.watchFiles)) { diff --git a/packages/rspeedy/core/src/cli/preview.ts b/packages/rspeedy/core/src/cli/preview.ts index 24951f4193..7a3cb3a54d 100644 --- a/packages/rspeedy/core/src/cli/preview.ts +++ b/packages/rspeedy/core/src/cli/preview.ts @@ -9,22 +9,31 @@ import color from 'picocolors' import type { CommonOptions } from './commands.js' import { exit } from './exit.js' -import { loadConfig } from '../config/loadConfig.js' +import { loadConfig, resolveConfigPath } from '../config/loadConfig.js' import { createRspeedy } from '../create-rspeedy.js' -export type PreviewOptions = CommonOptions +export interface PreviewOptions extends CommonOptions { + base?: string | undefined +} export async function preview( this: Command, cwd: string, - options: PreviewOptions, + previewOptions: PreviewOptions, ): Promise { try { + const configPath = resolveConfigPath(cwd, previewOptions.config) + const { content: rspeedyConfig } = await loadConfig({ cwd, - configPath: options.config, + configPath, }) + if (previewOptions.base) { + rspeedyConfig.server ??= {} + rspeedyConfig.server.base = previewOptions.base + } + const rspeedy = await createRspeedy({ cwd, rspeedyConfig }) await rspeedy.initConfigs() diff --git a/website/docs/en/guide/cli.md b/website/docs/en/guide/cli.md index 08b171715e..31687de2dd 100644 --- a/website/docs/en/guide/cli.md +++ b/website/docs/en/guide/cli.md @@ -64,6 +64,7 @@ The `rspeedy dev` command is used to start a local dev server and compile the so Usage: rspeedy dev [options] Options: + -b --base specify the base path of the server -c --config specify the configuration file, can be a relative or absolute path -h, --help display help for command ``` @@ -94,6 +95,7 @@ The `rspeedy preview` command is used to preview the production build outputs lo Usage: rspeedy preview [options] Options: + -b --base specify the base path of the server -c --config specify the configuration file, can be a relative or absolute path -h, --help display help for command ``` diff --git a/website/docs/zh/guide/cli.md b/website/docs/zh/guide/cli.md index 8f1d99d1fe..aee1e7d5ee 100644 --- a/website/docs/zh/guide/cli.md +++ b/website/docs/zh/guide/cli.md @@ -64,6 +64,7 @@ Commands: Usage: rspeedy dev [options] Options: + -b --base specify the base path of the server -c --config specify the configuration file, can be a relative or absolute path -h, --help display help for command ``` @@ -94,6 +95,7 @@ Options: Usage: rspeedy preview [options] Options: + -b --base specify the base path of the server -c --config specify the configuration file, can be a relative or absolute path -h, --help display help for command ```