| 1 | # Lilconfig ⚙️
|
|---|
| 2 | [](https://badge.fury.io/js/lilconfig)
|
|---|
| 3 | [](https://packagephobia.now.sh/result?p=lilconfig)
|
|---|
| 4 | [](https://coveralls.io/github/antonk52/lilconfig)
|
|---|
| 5 |
|
|---|
| 6 | A zero-dependency alternative to [cosmiconfig](https://www.npmjs.com/package/cosmiconfig) with the same API.
|
|---|
| 7 |
|
|---|
| 8 | ## Installation
|
|---|
| 9 |
|
|---|
| 10 | ```sh
|
|---|
| 11 | npm install lilconfig
|
|---|
| 12 | ```
|
|---|
| 13 |
|
|---|
| 14 | ## Usage
|
|---|
| 15 |
|
|---|
| 16 | ```js
|
|---|
| 17 | import {lilconfig, lilconfigSync} from 'lilconfig';
|
|---|
| 18 |
|
|---|
| 19 | // all keys are optional
|
|---|
| 20 | const options = {
|
|---|
| 21 | stopDir: '/Users/you/some/dir',
|
|---|
| 22 | searchPlaces: ['package.json', 'myapp.conf.js'],
|
|---|
| 23 | ignoreEmptySearchPlaces: false
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | lilconfig(
|
|---|
| 27 | 'myapp',
|
|---|
| 28 | options // optional
|
|---|
| 29 | ).search() // Promise<LilconfigResult>
|
|---|
| 30 |
|
|---|
| 31 | lilconfigSync(
|
|---|
| 32 | 'myapp',
|
|---|
| 33 | options // optional
|
|---|
| 34 | ).load(pathToConfig) // LilconfigResult
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * LilconfigResult
|
|---|
| 38 | * {
|
|---|
| 39 | * config: any; // your config
|
|---|
| 40 | * filepath: string;
|
|---|
| 41 | * }
|
|---|
| 42 | */
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | ## Difference to `cosmiconfig`
|
|---|
| 46 | Lilconfig does not intend to be 100% compatible with `cosmiconfig` but tries to mimic it where possible. The key differences are:
|
|---|
| 47 | - **no** support for yaml files out of the box(`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](#yaml-loader) below.
|
|---|
| 48 | - **no** cache
|
|---|
| 49 |
|
|---|
| 50 | ### Options difference between the two.
|
|---|
| 51 |
|
|---|
| 52 | |cosmiconfig option | lilconfig |
|
|---|
| 53 | |------------------------|-----------|
|
|---|
| 54 | |cache | ❌ |
|
|---|
| 55 | |loaders | ✅ |
|
|---|
| 56 | |ignoreEmptySearchPlaces | ✅ |
|
|---|
| 57 | |packageProp | ✅ |
|
|---|
| 58 | |searchPlaces | ✅ |
|
|---|
| 59 | |stopDir | ✅ |
|
|---|
| 60 | |transform | ✅ |
|
|---|
| 61 |
|
|---|
| 62 | ## Loaders examples
|
|---|
| 63 |
|
|---|
| 64 | ### Yaml loader
|
|---|
| 65 |
|
|---|
| 66 | If you need the YAML support you can provide your own loader
|
|---|
| 67 |
|
|---|
| 68 | ```js
|
|---|
| 69 | import {lilconfig} from 'lilconfig';
|
|---|
| 70 | import yaml from 'yaml';
|
|---|
| 71 |
|
|---|
| 72 | function loadYaml(filepath, content) {
|
|---|
| 73 | return yaml.parse(content);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | const options = {
|
|---|
| 77 | loaders: {
|
|---|
| 78 | '.yaml': loadYaml,
|
|---|
| 79 | '.yml': loadYaml,
|
|---|
| 80 | // loader for files with no extension
|
|---|
| 81 | noExt: loadYaml
|
|---|
| 82 | }
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | lilconfig('myapp', options)
|
|---|
| 86 | .search()
|
|---|
| 87 | .then(result => {
|
|---|
| 88 | result // {config, filepath}
|
|---|
| 89 | });
|
|---|
| 90 | ```
|
|---|
| 91 |
|
|---|
| 92 | ### ESM loader
|
|---|
| 93 |
|
|---|
| 94 | 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`.
|
|---|
| 95 |
|
|---|
| 96 | ```js
|
|---|
| 97 | import {lilconfig} from 'lilconfig';
|
|---|
| 98 |
|
|---|
| 99 | const loadEsm = filepath => import(filepath);
|
|---|
| 100 |
|
|---|
| 101 | lilconfig('myapp', {
|
|---|
| 102 | loaders: {
|
|---|
| 103 | '.js': loadEsm,
|
|---|
| 104 | '.mjs': loadEsm,
|
|---|
| 105 | }
|
|---|
| 106 | })
|
|---|
| 107 | .search()
|
|---|
| 108 | .then(result => {
|
|---|
| 109 | result // {config, filepath}
|
|---|
| 110 |
|
|---|
| 111 | result.config.default // if config uses `export default`
|
|---|
| 112 | });
|
|---|
| 113 | ```
|
|---|
| 114 |
|
|---|
| 115 | ## Version correlation
|
|---|
| 116 |
|
|---|
| 117 | - lilconig v1 → cosmiconfig v6
|
|---|
| 118 | - lilconig v2 → cosmiconfig v7
|
|---|