Skip to content

Commit

Permalink
feat: .jsonc config support (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 7, 2024
1 parent cab1d6e commit cbb19a0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.

## Features

- JSON, CJS, Typescript, and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti)
- RC config support with [unjs/rc9](https://github.com/unjs/rc9)
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
- `.json`, `.js`, `.ts`, and `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
- `.jsonc` config support with [jsonc-parser](https://github.com/microsoft/node-jsonc-parser)
- `.rc` config support with [unjs/rc9](https://github.com/unjs/rc9)
- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
- Reads config from the nearest `package.json` file
- [Extends configurations](https://github.com/unjs/c12#extending-configuration) from multiple local or git sources
- Overwrite with [environment-specific configuration](#environment-specific-configuration)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dotenv": "^16.3.2",
"giget": "^1.2.1",
"jiti": "^1.21.0",
"jsonc-parser": "^3.2.1",
"mlly": "^1.5.0",
"ohash": "^1.1.3",
"pathe": "^1.1.2",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from "node:fs";
import { rm } from "node:fs/promises";
import { readFile, rm } from "node:fs/promises";
import { homedir } from "node:os";
import { resolve, extname, dirname, basename, join } from "pathe";
import createJiti from "jiti";
Expand Down Expand Up @@ -47,6 +47,16 @@ export async function loadConfig<
interopDefault: true,
requireCache: false,
esmResolve: true,
extensions: [
".js",
".mjs",
".cjs",
".ts",
".mts",
".cts",
".json",
".jsonc",
],
...options.jitiOptions,
});

Expand Down Expand Up @@ -316,7 +326,12 @@ async function resolveConfig<
if (!existsSync(res.configFile!)) {
return res;
}
res.config = options.jiti!(res.configFile!);
if (res.configFile!.endsWith(".jsonc")) {
const { parse } = await import("jsonc-parser");
res.config = parse(await readFile(res.configFile!, "utf8"));
} else {
res.config = options.jiti!(res.configFile!);
}
if (res.config instanceof Function) {
res.config = await res.config();
}
Expand Down
16 changes: 16 additions & 0 deletions test/fixture/.base/config.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// jsonc can support comments!
"$meta": {
"name": "base",
"version": "1.0.0",
},
"baseConfig": true,
"colors": {
"primary": "base_primary",
"text": "base_text",
},
"array": ["b"],
"$env": {
"test": { "baseEnvConfig": true },
},
}
15 changes: 0 additions & 15 deletions test/fixture/.base/config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("c12", () => {
"text": "base_text",
},
},
"configFile": "<path>/fixture/.base/config.ts",
"configFile": "<path>/fixture/.base/config.jsonc",
"cwd": "<path>/fixture/.base",
"meta": {
"name": "base",
Expand Down Expand Up @@ -276,7 +276,7 @@ describe("c12", () => {
expect(Object.keys(configLayer.config!)).toContain("$test");

const baseConfigLay = transformdLayers.find(
(layer) => layer.configFile === "<path>/fixture/.base/config.ts",
(layer) => layer.configFile === "<path>/fixture/.base/config.jsonc",
)!;
expect(Object.keys(baseConfigLay.config!)).toContain("$env");
});
Expand Down

0 comments on commit cbb19a0

Please sign in to comment.