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 }) });