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.
import { loadUserConfig, loadConfigFromPath, mergeWithDefaults } from "@flexpixels/cortex";Resolution pipeline
All three helpers run the same pipeline — the one mergeThemeConfig runs:
- Read the user partial (from disk in
loadUserConfig/loadConfigFromPath, from arguments inmergeWithDefaults). A file must hold a JSON object; it may contain//and/* */comments and trailing commas (JSONC).JSON.parseand editors that check the file against$schemaas strict JSON do not accept them, so strict JSON is the portable form. - Apply scenarios the partial names (
typography.scenario,elevation.scenario) over the defaults. - Deep-merge the partial over that. User fields always win.
- 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?)
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 aCONFIG_NOT_FOUNDwarning), or - the file cannot be read, is not JSON (comments and trailing commas allowed), holds no object, or fails validation — a
CONFIG_LOAD_FAILEDwarning 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.
const config = loadUserConfig("theme-config.json");loadConfigFromPath(absolutePath)
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.
import { resolve } from "node:path";
const config = loadConfigFromPath(resolve(process.argv[2] ?? "theme-config.json"));mergeWithDefaults(partial)
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.
const config = mergeWithDefaults({
seeds: { primary: "#4f46e5", secondary: "#c026d3", neutral: "#71717a" },
});Picking the right helper
| Helper | Reads disk? | Applies scenarios? | Validates? |
|---|---|---|---|
loadUserConfig(path?) | yes (falls back) | yes | yes (warns, falls back) |
loadConfigFromPath(absolutePath) | yes (throws on miss) | yes | yes (throws, names file) |
mergeWithDefaults(partial) | no | yes | yes (throws via Zod) |
mergeThemeConfig(partial) | no | yes | yes (throws via Zod) |
validateThemeConfig(input) | no | yes | yes (throws via Zod) |