mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Refactor merge server config (#7639)
This commit is contained in:
parent
4ce2ba972a
commit
bde08c4b14
3 changed files with 26 additions and 28 deletions
|
@ -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) {
|
||||
|
|
|
@ -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 ?? [])];
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue