From 295263807df5a0cdff3b04d5131a3cebc76ec491 Mon Sep 17 00:00:00 2001 From: JounQin Date: Wed, 22 Jun 2022 13:35:24 +0800 Subject: [PATCH] feat(build): allow setting base from command line Usage: ```sh pnpm docs-dev -- --base /foo/ pnpm docs-build-only -- --base /foo/ pnpm docs-serve -- --base /foo/ ``` Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- src/node/build/build.ts | 7 ++++++- src/node/serve/serve.ts | 3 ++- src/node/server.ts | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/node/build/build.ts b/src/node/build/build.ts index f35ad510..1ef2053f 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -9,13 +9,18 @@ import { bundle, okMark, failMark } from './bundle' export async function build( root: string, - buildOptions: BuildOptions & { mpa?: string } = {} + buildOptions: BuildOptions & { base?:string; mpa?: string } = {} ) { const start = Date.now() process.env.NODE_ENV = 'production' const siteConfig = await resolveConfig(root, 'build', 'production') + if (buildOptions.base) { + siteConfig.site.base = buildOptions.base + delete buildOptions.base + } + if (buildOptions.mpa) { siteConfig.mpa = true delete buildOptions.mpa diff --git a/src/node/serve/serve.ts b/src/node/serve/serve.ts index 3ec0bc3e..6b81e306 100644 --- a/src/node/serve/serve.ts +++ b/src/node/serve/serve.ts @@ -16,6 +16,7 @@ function trimChar(str: string, char: string) { } export interface ServeOptions { + base?: string root?: string port?: number } @@ -23,7 +24,7 @@ export interface ServeOptions { export async function serve(options: ServeOptions = {}) { const port = options.port !== undefined ? options.port : 5000 const site = await resolveConfig(options.root, 'serve', 'production') - const base = trimChar(site?.site?.base ?? '', '/') + const base = trimChar(options?.base ?? site?.site?.base ?? '', '/') const compress = compression() const serve = sirv(site.outDir, { diff --git a/src/node/server.ts b/src/node/server.ts index 4f81fef4..fff9186d 100644 --- a/src/node/server.ts +++ b/src/node/server.ts @@ -8,6 +8,11 @@ export async function createServer( ) { const config = await resolveConfig(root) + if (serverOptions.base) { + config.site.base = serverOptions.base + delete serverOptions.base + } + return createViteServer({ root: config.srcDir, base: config.site.base,