From bde08c4b141995d77f502bb263f2b7d15c95b12d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 14 Jul 2023 16:13:58 +0800 Subject: [PATCH] Refactor merge server config (#7639) --- packages/astro/src/core/config/config.ts | 35 +++++++++--------------- packages/astro/src/core/config/merge.ts | 12 ++++++++ packages/astro/src/core/config/schema.ts | 7 +---- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index dc7de2bc7..b04aae727 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -6,6 +6,7 @@ import * as colors from 'kleur/colors'; import path from 'path'; import { fileURLToPath, pathToFileURL } from 'url'; import { AstroError, AstroErrorData } from '../errors/index.js'; +import { mergeConfig } from './merge.js'; import { createRelativeSchema } from './schema.js'; import { loadConfigWithVite } from './vite-load.js'; @@ -114,28 +115,18 @@ export function resolveRoot(cwd?: string | URL): string { /** Merge CLI flags & user config object (CLI flags take priority) */ function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) { - astroConfig.server = astroConfig.server || {}; - astroConfig.markdown = astroConfig.markdown || {}; - astroConfig.experimental = astroConfig.experimental || {}; - if (typeof flags.site === 'string') astroConfig.site = flags.site; - if (typeof flags.base === 'string') astroConfig.base = flags.base; - if (typeof flags.drafts === 'boolean') astroConfig.markdown.drafts = flags.drafts; - if (typeof flags.port === 'number') { - // @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function. - // TODO: Come back here and refactor to remove this expected error. - astroConfig.server.port = flags.port; - } - if (typeof flags.host === 'string' || typeof flags.host === 'boolean') { - // @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function. - // TODO: Come back here and refactor to remove this expected error. - astroConfig.server.host = flags.host; - } - if (typeof flags.open === 'boolean') { - // @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function. - // TODO: Come back here and refactor to remove this expected error. - astroConfig.server.open = flags.open; - } - return astroConfig; + return mergeConfig(astroConfig, { + site: flags.site, + base: flags.base, + markdown: { + drafts: flags.drafts, + }, + server: { + port: flags.port, + host: flags.host, + open: flags.open, + }, + }); } async function search(fsMod: typeof fs, root: string) { diff --git a/packages/astro/src/core/config/merge.ts b/packages/astro/src/core/config/merge.ts index 3a498332b..93c386eca 100644 --- a/packages/astro/src/core/config/merge.ts +++ b/packages/astro/src/core/config/merge.ts @@ -25,6 +25,18 @@ function mergeConfigRecursively( merged[key] = mergeViteConfig(existing, value); continue; } + if (key === 'server' && rootPath === '') { + // server config can be a function or an object, if one of the two values is a function, + // create a new wrapper function that merges them + if (typeof existing === 'function' || typeof value === 'function') { + merged[key] = (...args: any[]) => { + const existingConfig = typeof existing === 'function' ? existing(...args) : existing; + const valueConfig = typeof value === 'function' ? value(...args) : value; + return mergeConfigRecursively(existingConfig, valueConfig, key); + }; + continue; + } + } if (Array.isArray(existing) || Array.isArray(value)) { merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])]; diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index ae681a543..5b559e444 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -299,12 +299,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { // preprocess (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; + return val({ command: cmd === 'dev' ? 'dev' : 'preview' }); } else { return val; }