版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

README.md 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # c12
  2. [![npm version][npm-version-src]][npm-version-href]
  3. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  4. [![Github Actions][github-actions-src]][github-actions-href]
  5. [![Codecov][codecov-src]][codecov-href]
  6. > Smart Configuration Loader
  7. ## Features
  8. - JSON, CJS, Typescript, and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti)
  9. - RC config support with [unjs/rc9](https://github.com/unjs/rc9)
  10. - Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
  11. - `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
  12. - Support reading config from the nearest `package.json` file
  13. - Support extending nested configurations from multiple local or git sources
  14. ## Usage
  15. Install package:
  16. ```sh
  17. # npm
  18. npm install c12
  19. # yarn
  20. yarn add c12
  21. # pnpm
  22. pnpm install c12
  23. ```
  24. Import:
  25. ```js
  26. // ESM
  27. import { loadConfig } from "c12";
  28. // CommonJS
  29. const { loadConfig } = require("c12");
  30. ```
  31. Load configuration:
  32. ```js
  33. // Get loaded config
  34. const { config } = await loadConfig({});
  35. // Get resolved config and extended layers
  36. const { config, configFile, layers } = await loadConfig({});
  37. ```
  38. ## Loading priority
  39. c12 merged config sources with [unjs/defu](https://github.com/unjs/defu) by below order:
  40. 1. Config overrides passed by options
  41. 2. Config file in CWD
  42. 3. RC file in CWD
  43. 4. Global RC file in user's home directory
  44. 5. Config from `package.json`
  45. 6. Default config passed by options
  46. 7. Extended config layers
  47. ## Options
  48. ### `cwd`
  49. Resolve configuration from this working directory. The default is `process.cwd()`
  50. ### `name`
  51. Configuration base name. The default is `config`.
  52. ### `configName`
  53. Configuration file name without extension. Default is generated from `name` (name=foo => `foo.config`).
  54. Set to `false` to avoid loading the config file.
  55. ### `rcFile`
  56. RC Config file name. Default is generated from `name` (name=foo => `.foorc`).
  57. Set to `false` to disable loading RC config.
  58. ### `globalRC`
  59. Load RC config from the workspace directory and the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.
  60. ### `dotenv`
  61. Loads `.env` file if enabled. It is disabled by default.
  62. ### `packageJson`
  63. Loads config from nearest `package.json` file. It is disabled by default.
  64. If `true` value is passed, c12 uses `name` field from `package.json`.
  65. You can also pass either a string or an array of strings as a value to use those fields.
  66. ### `defaults`
  67. Specify default configuration. It has the **lowest** priority and is applied **after extending** config.
  68. ### `defaultConfig`
  69. Specify default configuration. It is applied **before** extending config.
  70. ### `overrides`
  71. Specify override configuration. It has the **highest** priority and is applied **before extending** config.
  72. ### `jiti`
  73. Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configuration files.
  74. ### `jitiOptions`
  75. Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.
  76. ### `envName`
  77. Environment name used for [environment specific configuration](#environment-specific-configuration).
  78. The default is `process.env.NODE_ENV`. You can set `envName` to `false` or an empty string to disable the feature.
  79. ## Extending configuration
  80. If resolved config contains a `extends` key, it will be used to extend the configuration.
  81. Extending can be nested and each layer can extend from one base or more.
  82. The final config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu).
  83. Each item in extends is a string that can be either an absolute or relative path to the current config file pointing to a config file for extending or the directory containing the config file.
  84. If it starts with either `github:`, `gitlab:`, `bitbucket:`, or `https:`, c12 automatically clones it.
  85. For custom merging strategies, you can directly access each layer with `layers` property.
  86. **Example:**
  87. ```js
  88. // config.ts
  89. export default {
  90. colors: {
  91. primary: "user_primary",
  92. },
  93. extends: ["./theme"],
  94. };
  95. ```
  96. ```js
  97. // config.dev.ts
  98. export default {
  99. dev: true,
  100. };
  101. ```
  102. ```js
  103. // theme/config.ts
  104. export default {
  105. extends: "../base",
  106. colors: {
  107. primary: "theme_primary",
  108. secondary: "theme_secondary",
  109. },
  110. };
  111. ```
  112. ```js
  113. // base/config.ts
  114. export default {
  115. colors: {
  116. primary: 'base_primary'
  117. text: 'base_text'
  118. }
  119. }
  120. ```
  121. The loaded configuration would look like this:
  122. ```js
  123. {
  124. dev: true,
  125. colors: {
  126. primary: 'user_primary',
  127. secondary: 'theme_secondary',
  128. text: 'base_text'
  129. }
  130. }
  131. ```
  132. Layers:
  133. ```js
  134. [
  135. { config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },
  136. { config: /* base config */, configFile: /* path/to/base/config.ts */, cwd: /* path/to/base */ },
  137. { config: /* dev config */, configFile: /* path/to/config.dev.ts */, cwd: /* path/ */ },
  138. ]
  139. ```
  140. ## Environment-specific configuration
  141. Users can define environment-specific configuration using these config keys:
  142. - `$test: {...}`
  143. - `$development: {...}`
  144. - `$production: {...}`
  145. - `$env: { [env]: {...} }`
  146. c12 tries to match [`envName`](#envname) and override environment config if specified.
  147. **Note:** Environment will be applied when extending each configuration layer. This way layers can provide environment-specific configuration.
  148. **Example:**
  149. ```js
  150. {
  151. // Default configuration
  152. logLevel: 'info',
  153. // Environment overrides
  154. $test: { logLevel: 'silent' },
  155. $development: { logLevel: 'warning' },
  156. $production: { logLevel: 'error' },
  157. $env: {
  158. staging: { logLevel: 'debug' }
  159. }
  160. }
  161. ```
  162. ## 💻 Development
  163. - Clone this repository
  164. - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
  165. - Install dependencies using `pnpm install`
  166. - Run interactive tests using `pnpm dev`
  167. ## License
  168. Made with 💛 Published under [MIT License](./LICENSE).
  169. <!-- Badges -->
  170. [npm-version-src]: https://img.shields.io/npm/v/c12?style=flat-square
  171. [npm-version-href]: https://npmjs.com/package/c12
  172. [npm-downloads-src]: https://img.shields.io/npm/dm/c12?style=flat-square
  173. [npm-downloads-href]: https://npmjs.com/package/c12
  174. [github-actions-src]: https://img.shields.io/github/actions/workflow/status/unjs/c12/ci.yml?branch=main&style=flat-square
  175. [github-actions-href]: https://github.com/unjs/c12/actions?query=workflow%3Aci
  176. [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/c12/main?style=flat-square
  177. [codecov-href]: https://codecov.io/gh/unjs/c12