@exadev/config
The unified exadev.config.ts. Each ExaDev tool contributes a typed, schema-validated section to one config file, or reads the same section from a standalone exadev.<tool>.config.ts, and this package imports no tool to make that work. It builds on cosmiconfig-extends, so a config file is TypeScript evaluated through jiti and can extends presets, under that package's trust policy.
Getting started
pnpm add @exadev/config cosmiconfig
cosmiconfig is a peer dependency (^9.0.0 || ^10.0.0). The package supports Node 20 and later; cosmiconfig 10 itself declares Node ^22.18 || >=24, so a consumer on an older Node sees an engines warning from cosmiconfig, not from this package. The package replaces the only part of cosmiconfig 10 that depends on that Node version, its .ts loading.
Authoring a config
Each tool package exports the section it owns. The authoring file passes the sections it uses to withSections, which returns a defineConfig typed for exactly those sections:
// exadev.config.ts
import { layoutSection, withSections } from '@exadev/config';
import { myTool } from 'my-tool';
export default withSections(layoutSection, myTool)({
layout: {
groups: [
{ name: 'core', rank: 0 },
{ name: 'features', rank: 1, slice: { segment: 0 } },
],
},
myTool: { include: ['src'] },
});
A section is accepted exactly when its descriptor is passed, so the type system reports the mistakes a config file is likely to contain:
- A key, or a value of a type, that the section's type does not allow is an error on that key, with a near-miss suggestion for a misspelt one. The type is the schema's input type, so a rule the schema enforces only at runtime (an integer minimum, unique names, a valid pattern) passes the type checker and is checked when a tool loads the section.
- A section whose tool is installed but not passed to
withSections is an unknown-property error on its key.
- A descriptor that could not be told apart in the type (a name that is not a literal, an envelope key such as
extends, a name listed twice) is refused with a message saying why.
- A section whose tool is not installed is a module-not-found error on the import.
- A config that lists no sections accepts none. Without a section an empty mapped type would be
{}, which TypeScript does not check for excess properties, so defineConfig accepts only the extends key and withSections needs at least one descriptor.
The unknown-key and misspelt-name errors come from TypeScript's excess-property check, which applies to an object literal passed directly to the function. A config built elsewhere and passed by variable or spread is not checked for extra keys, so exadev-config doctor (below) remains the check for a key nobody reads.
extends names presets, resolved and merged by cosmiconfig-extends. For the unified file a preset is a whole config file, so it can supply any section.
defineConfig and the function withSections returns hand back their argument untouched. They apply no schema defaults, because extends merges what a file exports and a default applied while authoring would look like a value the author wrote and override the presets it extends. Defaults belong to a section's schema, applied when a tool loads it.
Emitting declarations
Under isolatedDeclarations a default export cannot be inferred. Name the value and annotate it with ConfigOf:
import { type ConfigOf, layoutSection, withSections } from '@exadev/config';
const config: ConfigOf<[typeof layoutSection]> = withSections(layoutSection)({ layout: { groups: [] } });
export default config;
Standalone and unified files
A tool reads its section with loadSection(section, { cwd }). cwd must be an existing directory, and a mistyped one throws instead of reading as a directory with no config. The section comes from either of two files in cwd, and neither is searched for in a parent directory:
exadev.config.ts, under the key section.name.
exadev.<section.name>.config.ts, whose default export is the section itself.
The standalone name is namespaced because the plain <name>.config.ts is the native config file of many tools (eslint.config.ts, vitest.config.ts), so a section called eslint would otherwise read the ESLint flat config as its own. Either file may use the extension .mts or .cts instead of .ts; a file that exists under more than one extension throws, since choosing one silently would hide a configuration that never takes effect. Other extensions, and the plain <name>.config.ts, are never read.
Both are evaluated with extends applied, and both produce the same section for the same content, given layer options that apply the same rules to each shape (below). A section defined in both files (the unified file counts a section a preset supplies) throws an error naming both, since choosing one silently would hide a configuration that never takes effect.
The result is undefined when neither file defines the section, and otherwise a LoadedSection:
interface LoadedSection<Value> {
readonly value: Value; // the schema's output, defaults included
readonly shape: ConfigFileShape; // 'unified' or 'standalone'
readonly file: string; // the absolute path of the file it was read from, with its extension
}
shape and file say where the section lives, so a tool that writes the section back, or reports where a value came from, need not hard-code the two file names and three extensions. For a section that a preset of the unified file supplies, file is the unified file, not the preset.
loadSection returns undefined both when neither file exists and when a file exists but does not define the section. A tool that must tell those apart, for example to name the file when evaluating it fails, can ask which files a section can live in without evaluating any:
import { CONFIG_EXTENSIONS, configFileNames, findConfigFiles } from '@exadev/config';
CONFIG_EXTENSIONS; // ['.ts', '.mts', '.cts'], in the order they are looked up
configFileNames(myTool);
// { unified: ['exadev.config.ts', 'exadev.config.mts', 'exadev.config.cts'], standalone: ['exadev.myTool.config.ts', 'exadev.myTool.config.mts', 'exadev.myTool.config.cts'] }
findConfigFiles(myTool, { cwd });
// { unified: '/path/to/exadev.config.ts', standalone: undefined }
configFileNames is pure: it returns the candidate names of each shape relative to the config directory, in extension order, for a caller that checks for files through its own file system. findConfigFiles looks in cwd itself and returns the absolute path of the file of each shape that exists, or undefined, reading neither; like loadSection, it throws when cwd is not an existing directory or a file exists under more than one extension. Both are built from the same names loadSection reads, so they cannot disagree with it.
A section that fails its schema throws ConfigValidationError, which this package re-exports along with its ConfigValidationIssue type so a caller can catch it by class without depending on cosmiconfig-extends, naming the file, with one normalised { path, message } entry per problem. A unified file that exports anything but an object throws a TypeError, and so does any config file with no default export or a null one, since a configuration that never takes effect must not read as an absent file. Only an empty file, or one whose default export is undefined, is an empty config.
loadSection also takes the options cosmiconfig-extends passes through for every file: alias (the directory an authoring import such as @exadev/config resolves to, whatever the install layout), fsCache and trust (which extends references may load; local paths only by default). How a file and its presets are checked and combined depends on the file's shape, so it is given per shape: unified applies to the layers of the unified file and standalone to the layers of the standalone file, and each is a LayerOptions pair of merge (how a file and its presets combine; arrays replace by default) and presetSchema (a Standard Schema each preset must pass before it is merged; none by default). A shape given no pair uses those defaults. doctor takes alias, fsCache, trust and unified; it reads only the unified file, so it has no standalone pair. No other cosmiconfig-extends option reaches the loader, even on a wider options object.
The two pairs are separate because the two shapes hold different layers. In the unified file each layer is a whole config file, holding the section under its name beside other tools' sections, so the unified merge folds whole files and the unified presetSchema describes a whole preset file (typically a loose object whose sections are optional layers). In a standalone file each layer is a value of the section itself, with the preset's extends key beside its fields, so the standalone merge folds section values and the standalone presetSchema describes a section layer plus extends. A rule written for one shape cannot be applied to the other: the value alone does not say which shape it is, since a misspelt section key looks like another tool's section.
Without a presetSchema a preset is only checked as part of the merged section, so an invalid key a preset supplies is reported against the file that extends it. With it, the failing preset throws ConfigValidationError with a message beginning invalid preset '<ref>':, where <ref> is the extends value as the file naming the preset wrote it (./presets/base.ts, or a package name). The schema's output replaces the preset, so it must keep the extends key, and it must apply no defaults, since a default would turn "no opinion" into an override. The config file itself is not a preset; its section is checked by the section's own schema.