Config loader (Node)

Node-only helpers for reading theme-config.json and producing a fully resolved ThemeConfig.

The config loader reads files with Node’s fs, so it is exported from the Node build only: @flexpixels/cortex under Node or Bun, or @flexpixels/cortex/node. It is not in @flexpixels/cortex/browser, and a bundler that resolves the root import with the browser condition gets the browser build, without it. Use it from build scripts, CLIs and dev servers; in the browser, use mergeThemeConfig on an object you already have.

ts
import { loadUserConfig, loadConfigFromPath, mergeWithDefaults } from "@flexpixels/cortex";

Resolution pipeline

All three helpers run the same pipeline — the one mergeThemeConfig runs:

  1. Read the user partial (from disk in loadUserConfig / loadConfigFromPath, from arguments in mergeWithDefaults). A file must hold a JSON object; it may contain // and /* */ comments and trailing commas (JSONC). JSON.parse and editors that check the file against $schema as strict JSON do not accept them, so strict JSON is the portable form.
  2. Apply scenarios the partial names (typography.scenario, elevation.scenario) over the defaults.
  3. Deep-merge the partial over that. User fields always win.
  4. Validate the result against the schema — unknown keys, out-of-range numbers, unsafe CSS values and cross-field rules included (see what validation checks).

The result is a fully resolved, validated ThemeConfig the caller owns (it shares nothing with defaultThemeConfig), ready for generateFullThemeCSS, exportDTCGTokens, exportDesignMd, or new ThemeEngine(config).

loadUserConfig(configPath?)

ts
function loadUserConfig(configPath?: string): ThemeConfig;

Reads configPath relative to process.cwd() (default: "theme-config.json") and runs the pipeline. Returns a copy of the defaults when:

  • the file does not exist (silent for the default theme-config.json; a path you wrote out that is not there logs a CONFIG_NOT_FOUND warning), or
  • the file cannot be read, is not JSON (comments and trailing commas allowed), holds no object, or fails validation — a CONFIG_LOAD_FAILED warning names the file and each field that is wrong, then the defaults are returned.

Suited for ergonomic “use the defaults if there’s nothing here” behaviour.

ts
const config = loadUserConfig("theme-config.json");

loadConfigFromPath(absolutePath)

ts
function loadConfigFromPath(absolutePath: string): ThemeConfig;

Same pipeline, but throws instead of falling back: Config file not found: <path> when the file is missing, and an error naming the file for invalid JSON, a non-object, or a config that fails validation (listing each field). Use it from CLIs that accept a --config flag, where silently falling back to defaults would mask a typo.

ts
import { resolve } from "node:path";

const config = loadConfigFromPath(resolve(process.argv[2] ?? "theme-config.json"));

mergeWithDefaults(partial)

ts
function mergeWithDefaults(partial: DeepPartial<ThemeConfig>): ThemeConfig;

mergeThemeConfig under its older name, kept for the loader pipeline: pass a partial config object and get back a resolved, validated ThemeConfig. Throws a ZodError naming each invalid field.

ts
const config = mergeWithDefaults({
  seeds: { primary: "#4f46e5", secondary: "#c026d3", neutral: "#71717a" },
});

Picking the right helper

HelperReads disk?Applies scenarios?Validates?
loadUserConfig(path?)yes (falls back)yesyes (warns, falls back)
loadConfigFromPath(absolutePath)yes (throws on miss)yesyes (throws, names file)
mergeWithDefaults(partial)noyesyes (throws via Zod)
mergeThemeConfig(partial)noyesyes (throws via Zod)
validateThemeConfig(input)noyesyes (throws via Zod)