{"name":"c12","version":"2.0.0","license":"MIT","_id":"c12@2.0.0","homepage":"https://github.com/unjs/c12#readme","bugs":{"url":"https://github.com/unjs/c12/issues"},"dist":{"shasum":"a372084a910b19ee28f78e57559eba24874391e0","tarball":"http://registry-npm.ywwl.com/c12/download/c12-2.0.0.tgz","fileCount":16,"integrity":"sha512-lxBXBw++beYvDiZhtIDQYtNpuHs+Fj+VgBxY+CyuDanuDwn8hUNFntvDi+AOZCyr5VQ2+pTFIFzFgRve3nLEtw==","signatures":[{"sig":"MEYCIQCih9EAGTs0D4OGzkcQsZ5wwlQ+pohL0+xJ7I21YKlcvAIhAMlYCgLuJtvyGrCeGtwHmWq1qhaRMmwf/ewiUQBgOTwR","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":72671,"size":18606,"noattachment":false,"key":"/c12/-/c12-2.0.0.tgz"},"main":"./dist/index.cjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"},"./update":{"types":"./dist/update.d.ts","import":"./dist/update.mjs","require":"./dist/update.cjs"}},"gitHead":"5b629266216caf09a4f84b67b4dc0c6856998861","scripts":{"dev":"vitest dev","lint":"eslint . && prettier -c src test","test":"pnpm lint && vitest run --coverage && pnpm test:types","build":"automd && unbuild","prepack":"unbuild","release":"pnpm build && pnpm test && changelogen --release --push --publish","lint:fix":"eslint . --fix && prettier -w src test","test:types":"tsc --noEmit"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/unjs/c12.git","type":"git"},"_npmVersion":"10.8.2","description":"Smart Config Loader","directories":{},"sideEffects":false,"_nodeVersion":"20.17.0","dependencies":{"rc9":"^2.1.2","defu":"^6.1.4","jiti":"^2.0.0","mlly":"^1.7.1","giget":"^1.2.3","ohash":"^1.1.4","pathe":"^1.1.2","dotenv":"^16.4.5","confbox":"^0.1.7","chokidar":"^4.0.1","pkg-types":"^1.2.0","perfect-debounce":"^1.0.0"},"_hasShrinkwrap":false,"packageManager":"pnpm@9.11.0","devDependencies":{"automd":"^0.3.8","eslint":"^9.11.1","vitest":"^2.1.1","unbuild":"3.0.0-rc.8","magicast":"^0.3.5","prettier":"^3.3.3","typescript":"^5.6.2","@types/node":"^22.7.4","changelogen":"^0.5.7","expect-type":"^0.20.0","eslint-config-unjs":"^0.4.1","@vitest/coverage-v8":"^2.1.1"},"peerDependencies":{"magicast":"^0.3.5"},"peerDependenciesMeta":{"magicast":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/c12_2.0.0_1727547896401_0.5367883686318184","host":"s3://npm-registry-packages"},"readme":"# ⚙️ c12\n\n<!-- automd:badges color=yellow codecov -->\n\n[![npm version](https://img.shields.io/npm/v/c12?color=yellow)](https://npmjs.com/package/c12)\n[![npm downloads](https://img.shields.io/npm/dm/c12?color=yellow)](https://npm.chart.dev/c12)\n[![codecov](https://img.shields.io/codecov/c/gh/unjs/c12?color=yellow)](https://codecov.io/gh/unjs/c12)\n\n<!-- /automd -->\n\nc12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.\n\n## ✅ Features\n\n- `.js`, `.ts`, `.mjs`, `.cjs`, `.mts`, `.cts` `.json` config loader with [unjs/jiti](https://jiti.unjs.io)\n- `.jsonc`, `.json5`, `.yaml`, `.yml`, `.toml` config loader with [unjs/confbox](https://confbox.unjs.io)\n- `.config/` directory support following [config dir proposal](https://github.com/pi0/config-dir)\n- `.rc` config support with [unjs/rc9](https://github.com/unjs/rc9)\n- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)\n- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)\n- Reads config from the nearest `package.json` file\n- [Extends configurations](https://github.com/unjs/c12#extending-configuration) from multiple local or git sources\n- Overwrite with [environment-specific configuration](#environment-specific-configuration)\n- Config watcher with auto-reload and HMR support\n- Create or update configuration files with [magicast](https://github.com/unjs/magicast)\n\n## 🦴 Used by\n\n- [Nuxt](https://nuxt.com/)\n- [Nitro](https://nitro.unjs.io/)\n- [Unbuild](https://unbuild.unjs.io)\n- [Automd](https://automd.unjs.io)\n- [Changelogen](https://changelogen.unjs.io)\n- [RemixKit](https://github.com/jrestall/remix-kit)\n- [Hey API](https://github.com/hey-api/openapi-ts)\n\n## Usage\n\nInstall package:\n\n<!-- automd:pm-install -->\n\n```sh\n# ✨ Auto-detect\nnpx nypm install c12\n\n# npm\nnpm install c12\n\n# yarn\nyarn add c12\n\n# pnpm\npnpm install c12\n\n# bun\nbun install c12\n\n# deno\ndeno install c12\n```\n\n<!-- /automd -->\n\nImport:\n\n<!-- automd:jsimport cjs imports=\"loadConfig,watchConfig\" -->\n\n**ESM** (Node.js, Bun, Deno)\n\n```js\nimport { loadConfig, watchConfig } from \"c12\";\n```\n\n**CommonJS** (Legacy Node.js)\n\n```js\nconst { loadConfig, watchConfig } = require(\"c12\");\n```\n\n<!-- /automd -->\n\nLoad configuration:\n\n```js\n// Get loaded config\nconst { config } = await loadConfig({});\n\n// Get resolved config and extended layers\nconst { config, configFile, layers } = await loadConfig({});\n```\n\n## Loading priority\n\nc12 merged config sources with [unjs/defu](https://github.com/unjs/defu) by below order:\n\n1. Config overrides passed by options\n2. Config file in CWD\n3. RC file in CWD\n4. Global RC file in the user's home directory\n5. Config from `package.json`\n6. Default config passed by options\n7. Extended config layers\n\n## Options\n\n### `cwd`\n\nResolve configuration from this working directory. The default is `process.cwd()`\n\n### `name`\n\nConfiguration base name. The default is `config`.\n\n### `configFile`\n\nConfiguration file name without extension. Default is generated from `name` (f.e., if `name` is `foo`, the config file will be => `foo.config`).\n\nSet to `false` to avoid loading the config file.\n\n### `rcFile`\n\nRC Config file name. Default is generated from `name` (name=foo => `.foorc`).\n\nSet to `false` to disable loading RC config.\n\n### `globalRC`\n\nLoad 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.\n\n### `dotenv`\n\nLoads `.env` file if enabled. It is disabled by default.\n\n### `packageJson`\n\nLoads config from nearest `package.json` file. It is disabled by default.\n\nIf `true` value is passed, c12 uses `name` field from `package.json`.\n\nYou can also pass either a string or an array of strings as a value to use those fields.\n\n### `defaults`\n\nSpecify default configuration. It has the **lowest** priority and is applied **after extending** config.\n\n### `defaultConfig`\n\nSpecify default configuration. It is applied **before** extending config.\n\n### `overrides`\n\nSpecify override configuration. It has the **highest** priority and is applied **before extending** config.\n\n### `omit$Keys`\n\nExclude environment-specific and built-in keys start with `$` in the resolved config. The default is `false`.\n\n### `jiti`\n\nCustom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configuration files.\n\n### `jitiOptions`\n\nCustom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.\n\n### `giget`\n\nOptions passed to [unjs/giget](https://github.com/unjs/giget) when extending layer from git source.\n\n### `merger`\n\nCustom options merger function. Default is [defu](https://github.com/unjs/defu).\n\n**Note:** Custom merge function should deeply merge options with arguments high -> low priority.\n\n### `envName`\n\nEnvironment name used for [environment specific configuration](#environment-specific-configuration).\n\nThe default is `process.env.NODE_ENV`. You can set `envName` to `false` or an empty string to disable the feature.\n\n## Extending configuration\n\nIf resolved config contains a `extends` key, it will be used to extend the configuration.\n\nExtending can be nested and each layer can extend from one base or more.\n\nThe final config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu).\n\nEach 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.\nIf it starts with either `github:`, `gitlab:`, `bitbucket:`, or `https:`, c12 automatically clones it.\n\nFor custom merging strategies, you can directly access each layer with `layers` property.\n\n**Example:**\n\n```js\n// config.ts\nexport default {\n  colors: {\n    primary: \"user_primary\",\n  },\n  extends: [\"./theme\"],\n};\n```\n\n```js\n// config.dev.ts\nexport default {\n  dev: true,\n};\n```\n\n```js\n// theme/config.ts\nexport default {\n  extends: \"../base\",\n  colors: {\n    primary: \"theme_primary\",\n    secondary: \"theme_secondary\",\n  },\n};\n```\n\n```js\n// base/config.ts\nexport default {\n  colors: {\n    primary: \"base_primary\",\n    text: \"base_text\",\n  },\n};\n```\n\nThe loaded configuration would look like this:\n\n```js\nconst config = {\n  dev: true,\n  colors: {\n    primary: \"user_primary\",\n    secondary: \"theme_secondary\",\n    text: \"base_text\",\n  },\n};\n```\n\nLayers:\n\n```js\n[\n  {\n    config: {\n      /* theme config */\n    },\n    configFile: \"/path/to/theme/config.ts\",\n    cwd: \"/path/to/theme \",\n  },\n  {\n    config: {\n      /* base  config */\n    },\n    configFile: \"/path/to/base/config.ts\",\n    cwd: \"/path/to/base\",\n  },\n  {\n    config: {\n      /* dev   config */\n    },\n    configFile: \"/path/to/config.dev.ts\",\n    cwd: \"/path/\",\n  },\n];\n```\n\n## Extending config layer from remote sources\n\nYou can also extend configuration from remote sources such as npm or github.\n\nIn the repo, there should be a `config.ts` (or `config.{name}.ts`) file to be considered as a valid config layer.\n\n**Example:** Extend from a github repository\n\n```js\n// config.ts\nexport default {\n  extends: \"gh:user/repo\",\n};\n```\n\n**Example:** Extend from a github repository with branch and subpath\n\n```js\n// config.ts\nexport default {\n  extends: \"gh:user/repo/theme#dev\",\n};\n```\n\n**Example:** Extend a private repository and install dependencies:\n\n```js\n// config.ts\nexport default {\n  extends: [\"gh:user/repo\", { auth: process.env.GITHUB_TOKEN, install: true }],\n};\n```\n\nYou can pass more options to `giget: {}` in layer config or disable it by setting it to `false`.\n\nRefer to [unjs/giget](https://giget.unjs.io) for more information.\n\n## Environment-specific configuration\n\nUsers can define environment-specific configuration using these config keys:\n\n- `$test: {...}`\n- `$development: {...}`\n- `$production: {...}`\n- `$env: { [env]: {...} }`\n\nc12 tries to match [`envName`](#envname) and override environment config if specified.\n\n**Note:** Environment will be applied when extending each configuration layer. This way layers can provide environment-specific configuration.\n\n**Example:**\n\n```js\nexport default {\n  // Default configuration\n  logLevel: \"info\",\n\n  // Environment overrides\n  $test: { logLevel: \"silent\" },\n  $development: { logLevel: \"warning\" },\n  $production: { logLevel: \"error\" },\n  $env: {\n    staging: { logLevel: \"debug\" },\n  },\n};\n```\n\n## Watching configuration\n\nyou can use `watchConfig` instead of `loadConfig` to load config and watch for changes, add and removals in all expected configuration paths and auto reload with new config.\n\n### Lifecycle hooks\n\n- `onWatch`: This function is always called when config is updated, added, or removed before attempting to reload the config.\n- `acceptHMR`: By implementing this function, you can compare old and new functions and return `true` if a full reload is not needed.\n- `onUpdate`: This function is always called after the new config is updated. If `acceptHMR` returns true, it will be skipped.\n\n```ts\nimport { watchConfig } from \"c12\";\n\nconst config = watchConfig({\n  cwd: \".\",\n  // chokidarOptions: {}, // Default is { ignoreInitial: true }\n  // debounce: 200 // Default is 100. You can set it to false to disable debounced watcher\n  onWatch: (event) => {\n    console.log(\"[watcher]\", event.type, event.path);\n  },\n  acceptHMR({ oldConfig, newConfig, getDiff }) {\n    const diff = getDiff();\n    if (diff.length === 0) {\n      console.log(\"No config changed detected!\");\n      return true; // No changes!\n    }\n  },\n  onUpdate({ oldConfig, newConfig, getDiff }) {\n    const diff = getDiff();\n    console.log(\"Config updated:\\n\" + diff.map((i) => i.toJSON()).join(\"\\n\"));\n  },\n});\n\nconsole.log(\"watching config files:\", config.watchingFiles);\nconsole.log(\"initial config\", config.config);\n\n// Stop watcher when not needed anymore\n// await config.unwatch();\n```\n\n## Updating config\n\n> [!NOTE]\n> This feature is experimental\n\nUpdate or create a new configuration files.\n\nAdd `magicast` peer dependency:\n\n<!-- automd:pm-install name=\"magicast\" dev -->\n\n```sh\n# ✨ Auto-detect\nnpx nypm install -D magicast\n\n# npm\nnpm install -D magicast\n\n# yarn\nyarn add -D magicast\n\n# pnpm\npnpm install -D magicast\n\n# bun\nbun install -D magicast\n\n# deno\ndeno install --dev magicast\n```\n\n<!-- /automd -->\n\nImport util from `c12/update`\n\n```js\nconst { configFile, created } = await updateConfig({\n  cwd: \".\",\n  configFile: \"foo.config\",\n  onCreate: ({ configFile }) => {\n    // You can prompt user if wants to create a new config file and return false to cancel\n    console.log(`Creating new config file in ${configFile}...`);\n    return \"export default { test: true }\";\n  },\n  onUpdate: (config) => {\n    // You can update the config contents just like an object\n    config.test2 = false;\n  },\n});\n\nconsole.log(`Config file ${created ? \"created\" : \"updated\"} in ${configFile}`);\n```\n\n## Contribution\n\n<details>\n  <summary>Local development</summary>\n\n- Clone this repository\n- Install the latest LTS version of [Node.js](https://nodejs.org/en/)\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`\n- Install dependencies using `pnpm install`\n- Run tests using `pnpm dev` or `pnpm test`\n\n</details>\n\n<!-- /automd -->\n\n## License\n\n<!-- automd:contributors license=MIT author=\"pi0\" -->\n\nPublished under the [MIT](https://github.com/unjs/c12/blob/main/LICENSE) license.\nMade by [@pi0](https://github.com/pi0) and [community](https://github.com/unjs/c12/graphs/contributors) 💛\n<br><br>\n<a href=\"https://github.com/unjs/c12/graphs/contributors\">\n<img src=\"https://contrib.rocks/image?repo=unjs/c12\" />\n</a>\n\n<!-- /automd -->\n\n<!-- automd:with-automd -->\n\n---\n\n_🤖 auto updated with [automd](https://automd.unjs.io)_\n\n<!-- /automd -->\n","publish_time":1727547896575,"maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"_cnpm_publish_time":1727547896575,"dist-tags":{"3x":"3.3.4","latest":"4.0.0-rc.4"}}