Skip to content

Commit

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

## Features

- `.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)
- `.js`, `.ts`, `.cjs`, `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
- `.json`, `.json5` and `.jsonc` config support.
- `.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)
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",
"json5": "^2.2.3",
"jsonc-parser": "^3.2.1",
"mlly": "^1.5.0",
"ohash": "^1.1.3",
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function loadConfig<
".cts",
".json",
".jsonc",
".json5",
],
...options.jitiOptions,
});
Expand Down Expand Up @@ -329,6 +330,9 @@ async function resolveConfig<
if (res.configFile!.endsWith(".jsonc")) {
const { parse } = await import("jsonc-parser");
res.config = parse(await readFile(res.configFile!, "utf8"));
} else if (res.configFile!.endsWith(".json5")) {
const { parse } = await import("json5");
res.config = parse(await readFile(res.configFile!, "utf8"));
} else {
res.config = options.jiti!(res.configFile!);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
{
extends: "../.base",
colors: {
primary: "theme_primary",
secondary: "theme_secondary",
},
};
}
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe("c12", () => {
"secondary": "theme_secondary",
},
},
"configFile": "<path>/fixture/theme/config.ts",
"configFile": "<path>/fixture/theme/config.json5",
"cwd": "<path>/fixture/theme",
"meta": {},
"source": "config",
Expand Down

0 comments on commit 58fd51b

Please sign in to comment.