|  | 3 月之前 | |
|---|---|---|
| .. | ||
| LICENSE | 3 月之前 | |
| package.json | 3 月之前 | |
| readme.md | 3 月之前 | |
A zero-dependency alternative to cosmiconfig with the same API.
npm install lilconfig
import {lilconfig, lilconfigSync} from 'lilconfig';
// all keys are optional
const options = {
    stopDir: '/Users/you/some/dir',
    searchPlaces: ['package.json', 'myapp.conf.js'],
    ignoreEmptySearchPlaces: false
}
lilconfig(
    'myapp',
    options // optional
).search() // Promise<LilconfigResult>
lilconfigSync(
    'myapp',
    options // optional
).load(pathToConfig) // LilconfigResult
/**
 * LilconfigResult
 * {
 *   config: any; // your config
 *   filepath: string;
 * }
 */
cosmiconfigLilconfig does not intend to be 100% compatible with cosmiconfig but tries to mimic it where possible. The key differences are:
lilconfig attempts to parse files with no extension as JSON instead of YAML). You can still add the support for YAML files by providing a loader, see an example below.| cosmiconfig option | lilconfig | 
|---|---|
| cache | ❌ | 
| loaders | ✅ | 
| ignoreEmptySearchPlaces | ✅ | 
| packageProp | ✅ | 
| searchPlaces | ✅ | 
| stopDir | ✅ | 
| transform | ✅ | 
If you need the YAML support you can provide your own loader
import {lilconfig} from 'lilconfig';
import yaml from 'yaml';
function loadYaml(filepath, content) {
    return yaml.parse(content);
}
const options = {
    loaders: {
        '.yaml': loadYaml,
        '.yml': loadYaml,
        // loader for files with no extension
        noExt: loadYaml
    }
};
lilconfig('myapp', options)
    .search()
    .then(result => {
        result // {config, filepath}
    });
Lilconfig v2 does not support ESM modules out of the box. However, you can support it with a custom a loader. Note that this will only work with the async lilconfig function and won't work with the sync lilconfigSync.
import {lilconfig} from 'lilconfig';
const loadEsm = filepath => import(filepath);
lilconfig('myapp', {
    loaders: {
        '.js': loadEsm,
        '.mjs': loadEsm,
    }
})
    .search()
    .then(result => {
        result // {config, filepath}
        result.config.default // if config uses `export default`
    });