From 0edfdd325932b0b493b2228e3a121d217c38a727 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 18 Oct 2022 13:10:16 +0800 Subject: [PATCH] Ensure CLI flags override function-style server config (#5110) --- .changeset/eighty-planes-kneel.md | 5 +++++ packages/astro/src/core/config/schema.ts | 14 ++++++++++++-- .../test/fixtures/astro-basic/astro.config.mjs | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/eighty-planes-kneel.md diff --git a/.changeset/eighty-planes-kneel.md b/.changeset/eighty-planes-kneel.md new file mode 100644 index 000000000..5f3eec576 --- /dev/null +++ b/.changeset/eighty-planes-kneel.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Ensure CLI flags override function-style server config diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 794c756ce..f9cddbf40 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -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({ diff --git a/packages/astro/test/fixtures/astro-basic/astro.config.mjs b/packages/astro/test/fixtures/astro-basic/astro.config.mjs index 08916b1fe..b7b7dafe6 100644 --- a/packages/astro/test/fixtures/astro-basic/astro.config.mjs +++ b/packages/astro/test/fixtures/astro-basic/astro.config.mjs @@ -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 }) });