Skip to content

Commit

Permalink
Ensure CLI flags override function-style server config (#5110)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 18, 2022
1 parent 0db9c08 commit 0edfdd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-planes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Ensure CLI flags override function-style server config
14 changes: 12 additions & 2 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/test/fixtures/astro-basic/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
});

0 comments on commit 0edfdd3

Please sign in to comment.