Ensure CLI flags override function-style server config (#5110)

This commit is contained in:
Bjorn Lu 2022-10-18 13:10:16 +08:00 committed by GitHub
parent 0db9c08a8d
commit 0edfdd3259
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Ensure CLI flags override function-style server config

View file

@ -271,8 +271,18 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.default({}),
server: z.preprocess(
// preprocess
(val) =>
typeof val === 'function' ? val({ command: cmd === 'dev' ? 'dev' : 'preview' }) : val,
(val) => {
if (typeof val === 'function') {
const result = val({ command: cmd === 'dev' ? 'dev' : 'preview' });
// @ts-expect-error revive attached prop added from CLI flags
if (val.port) result.port = val.port;
// @ts-expect-error revive attached prop added from CLI flags
if (val.host) result.host = val.host;
return result;
} else {
return val;
}
},
// validate
z
.object({

View file

@ -4,4 +4,6 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
integrations: [preact()],
// make sure CLI flags have precedence
server: () => ({ port: 3000 })
});