load-esm-config
Load configuration files written in TypeScript and JavaScript
Why?
Tools like vite
have popularized the idea of code as configuration within the JavaScript / TypeScript ecosystem. Some advantages of using TypeScript for are:
- increased flexibility around apis
- type-safety for users
- inline documentation for an enhanced learning process
This project is solely focused on automating the process of loading these configuration files. The configuration is searched from the provided cwd
and loaded automatically.
- support for both commonjs and esm modules
- nested configuration folders (by default configuration can be located in the
.config
folder)
Installation
pnpm add load-esm-config
Usage
import { loadEsmConfig } from 'load-esm-config';
const result = loadEsmConfig({ name: 'something' });
if (result) {
const { config, path } = result;
}
By default it supports these extensions: ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs']
. It is also possible to limit support for extensions by passing the extensions
property.
By default ./
(current) and .config/
folders will be searched for matching configuration files. In the example above both something.config.ts
and .config/something.config.mjs
are valid configuration locations. This optionality should help remove configuration file overload in top level Further configuration folders can be added via the dirs
property.
API
loadEsmConfig
LoadEsmConfigOptions
These are the options that can be provided to the loadEsmConfig function.
|
export interface LoadEsmConfigOptions<Config extends object = object, Argument = unknown> {
name: string;
cwd?: string;
config?: PartialDeep<Config>;
extensions?: SupportedExtensions[];
dirs?: string[];
getArgument?: (options: LoadEsmConfig<Config>) => Argument;
mergeOptions?: DeepMergeOptions;
disableUpwardLookup?: boolean;
}
|
interface LoadEsmConfigResult
The value returned when calling loadEsmConfig . This will either be undefined or an object with the properties shown.
|
interface LoadEsmConfigResult<Config extends object = any> {
path: string;
config: Config;
dependencies: string[];
}
|
Gratitude
This package is adapted from vite. The work the vite developers are doing is outstanding and this package would not exist without their efforts.