diff --git a/README.md b/README.md index 504d341..31e63d9 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index 7cf637c..ec9cb2f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b869877..84551af 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ dependencies: jiti: specifier: ^1.21.0 version: 1.21.0 + json5: + specifier: ^2.2.3 + version: 2.2.3 jsonc-parser: specifier: ^3.2.1 version: 3.2.1 @@ -2966,7 +2969,6 @@ packages: resolution: {integrity: sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==} engines: {node: '>=6'} hasBin: true - dev: true /jsonc-parser@3.2.1: resolution: {integrity: sha512-AilxAyFOAcK5wA1+LeaySVBrHsGQvUFCDWXKpZjzaL0PqW+xfBOttn8GNtWKFWqneyMZj41MWF9Kl6iPWLwgOA==} diff --git a/src/loader.ts b/src/loader.ts index 5143857..edc7455 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -56,6 +56,7 @@ export async function loadConfig< ".cts", ".json", ".jsonc", + ".json5", ], ...options.jitiOptions, }); @@ -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!); } diff --git a/test/fixture/theme/config.ts b/test/fixture/theme/config.json5 similarity index 83% rename from test/fixture/theme/config.ts rename to test/fixture/theme/config.json5 index 63ce94f..36e8af5 100644 --- a/test/fixture/theme/config.ts +++ b/test/fixture/theme/config.json5 @@ -1,7 +1,7 @@ -export default { +{ extends: "../.base", colors: { primary: "theme_primary", secondary: "theme_secondary", }, -}; +} diff --git a/test/index.test.ts b/test/index.test.ts index 5805b6a..395edda 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -143,7 +143,7 @@ describe("c12", () => { "secondary": "theme_secondary", }, }, - "configFile": "/fixture/theme/config.ts", + "configFile": "/fixture/theme/config.json5", "cwd": "/fixture/theme", "meta": {}, "source": "config",