You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.0 KiB

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