Skip to content

Commit

Permalink
feat(build): allow setting base from command line
Browse files Browse the repository at this point in the history
Usage:

```sh
pnpm docs-dev -- --base /foo/
pnpm docs-build-only -- --base /foo/
pnpm docs-serve -- --base /foo/
```

Co-authored-by: Divyansh Singh <[email protected]>
  • Loading branch information
JounQin and brc-dd authored Jun 22, 2022
1 parent 8cbe0f9 commit 2952638
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/node/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/node/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ function trimChar(str: string, char: string) {
}

export interface ServeOptions {
base?: string
root?: string
port?: number
}

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, {
Expand Down
5 changes: 5 additions & 0 deletions src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2952638

Please sign in to comment.