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({}), .default({}),
server: z.preprocess( server: z.preprocess(
// preprocess // preprocess
(val) => (val) => {
typeof val === 'function' ? val({ command: cmd === 'dev' ? 'dev' : 'preview' }) : 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 // validate
z z
.object({ .object({

View file

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