Skip to content

Commit 58fd51b

Browse files
authored
feat: json5 support (#133)
1 parent cbb19a0 commit 58fd51b

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.
99

1010
## Features
1111

12-
- `.json`, `.js`, `.ts`, and `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
13-
- `.jsonc` config support with [jsonc-parser](https://github.com/microsoft/node-jsonc-parser)
12+
- `.js`, `.ts`, `.cjs`, `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
13+
- `.json`, `.json5` and `.jsonc` config support.
1414
- `.rc` config support with [unjs/rc9](https://github.com/unjs/rc9)
1515
- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
1616
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dotenv": "^16.3.2",
3636
"giget": "^1.2.1",
3737
"jiti": "^1.21.0",
38+
"json5": "^2.2.3",
3839
"jsonc-parser": "^3.2.1",
3940
"mlly": "^1.5.0",
4041
"ohash": "^1.1.3",

pnpm-lock.yaml

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loader.ts

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export async function loadConfig<
5656
".cts",
5757
".json",
5858
".jsonc",
59+
".json5",
5960
],
6061
...options.jitiOptions,
6162
});
@@ -329,6 +330,9 @@ async function resolveConfig<
329330
if (res.configFile!.endsWith(".jsonc")) {
330331
const { parse } = await import("jsonc-parser");
331332
res.config = parse(await readFile(res.configFile!, "utf8"));
333+
} else if (res.configFile!.endsWith(".json5")) {
334+
const { parse } = await import("json5");
335+
res.config = parse(await readFile(res.configFile!, "utf8"));
332336
} else {
333337
res.config = options.jiti!(res.configFile!);
334338
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export default {
1+
{
22
extends: "../.base",
33
colors: {
44
primary: "theme_primary",
55
secondary: "theme_secondary",
66
},
7-
};
7+
}

test/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe("c12", () => {
143143
"secondary": "theme_secondary",
144144
},
145145
},
146-
"configFile": "<path>/fixture/theme/config.ts",
146+
"configFile": "<path>/fixture/theme/config.json5",
147147
"cwd": "<path>/fixture/theme",
148148
"meta": {},
149149
"source": "config",

0 commit comments

Comments
 (0)