Home / File/ merge.ts — astro Source File

merge.ts — astro Source File

Architecture documentation for merge.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 4 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  59d6cf60_ff4d_d499_5395_b2c915d5ed58["merge.ts"]
  aa7ecf5d_ecc0_6ab7_c8df_09c64e200c44["../../type-utils.js"]
  59d6cf60_ff4d_d499_5395_b2c915d5ed58 --> aa7ecf5d_ecc0_6ab7_c8df_09c64e200c44
  baa53824_73a3_1e03_2043_4d0c058ecca5["../types/public/index.js"]
  59d6cf60_ff4d_d499_5395_b2c915d5ed58 --> baa53824_73a3_1e03_2043_4d0c058ecca5
  f68003f1_292f_ca44_03ce_21af87a33c7b["../core/util.js"]
  59d6cf60_ff4d_d499_5395_b2c915d5ed58 --> f68003f1_292f_ca44_03ce_21af87a33c7b
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  59d6cf60_ff4d_d499_5395_b2c915d5ed58 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 59d6cf60_ff4d_d499_5395_b2c915d5ed58 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { mergeConfig as mergeViteConfig } from 'vite';
import type { DeepPartial } from '../../type-utils.js';
import type { AstroConfig, AstroInlineConfig } from '../../types/public/index.js';
import { arraify, isObject, isURL } from '../util.js';

function mergeConfigRecursively(
	defaults: Record<string, any>,
	overrides: Record<string, any>,
	rootPath: string,
) {
	const merged: Record<string, any> = { ...defaults };
	for (const key in overrides) {
		const value = overrides[key];
		if (value == null) {
			continue;
		}

		const existing = merged[key];

		if (existing == null) {
			merged[key] = value;
			continue;
		}

		// fields that require special handling:
		if (key === 'vite' && rootPath === '') {
			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;
			}
		}

		// for server.allowedHosts, if the value is a boolean
		if (key === 'allowedHosts' && rootPath === 'server' && typeof existing === 'boolean') {
			continue;
		}

		if (Array.isArray(existing) || Array.isArray(value)) {
			merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])];
			continue;
		}
		if (isURL(existing) && isURL(value)) {
			merged[key] = value;
			continue;
		}
		if (isObject(existing) && isObject(value)) {
			merged[key] = mergeConfigRecursively(existing, value, rootPath ? `${rootPath}.${key}` : key);
			continue;
		}

		merged[key] = value;
	}
	return merged;
}

export function mergeConfig<C extends AstroConfig | AstroInlineConfig>(
	defaults: C,
	overrides: DeepPartial<C>,
): C {
	return mergeConfigRecursively(defaults, overrides, '') as C;
}

Domain

Subdomains

Dependencies

  • ../../type-utils.js
  • ../core/util.js
  • ../types/public/index.js
  • vite

Frequently Asked Questions

What does merge.ts do?
merge.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in merge.ts?
merge.ts defines 2 function(s): mergeConfig, mergeConfigRecursively.
What does merge.ts depend on?
merge.ts imports 4 module(s): ../../type-utils.js, ../core/util.js, ../types/public/index.js, vite.
Where is merge.ts in the architecture?
merge.ts is located at packages/astro/src/core/config/merge.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/config).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free