c12 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,16 +1,20 @@
1
1
  # ⚙️ c12
2
2
 
3
- [![npm version][npm-version-src]][npm-version-href]
4
- [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
- [![Codecov][codecov-src]][codecov-href]
6
- [![License][license-src]][license-href]
3
+ <!-- automd:badges color=yellow codecov -->
4
+
5
+ [![npm version](https://img.shields.io/npm/v/c12?color=yellow)](https://npmjs.com/package/c12)
6
+ [![npm downloads](https://img.shields.io/npm/dm/c12?color=yellow)](https://npmjs.com/package/c12)
7
+ [![codecov](https://img.shields.io/codecov/c/gh/unjs/c12?color=yellow)](https://codecov.io/gh/unjs/c12)
8
+
9
+ <!-- /automd -->
7
10
 
8
11
  c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.
9
12
 
10
- ## Features
13
+ ## Features
11
14
 
12
15
  - `.js`, `.ts`, `.cjs`, `.mjs` config loader with [unjs/jiti](https://github.com/unjs/jiti)
13
- - `.json`, `.json5` and `.jsonc` config support.
16
+ - `.json`, `.json5` and `.jsonc` config support
17
+ - `.config/` directory support following [config dir proposal](https://github.com/pi0/config-dir)
14
18
  - `.rc` config support with [unjs/rc9](https://github.com/unjs/rc9)
15
19
  - `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)
16
20
  - Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)
@@ -19,21 +23,40 @@ c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.
19
23
  - Overwrite with [environment-specific configuration](#environment-specific-configuration)
20
24
  - Config watcher with auto-reload and HMR support
21
25
 
26
+ ## 🦴 Used by
27
+
28
+ - [Nuxt](https://nuxt.com/)
29
+ - [Nitro](https://nitro.unjs.io/)
30
+ - [Unbuild](https://unbuild.unjs.io)
31
+ - [Automd](https://automd.unjs.io)
32
+ - [Changelogen](https://changelogen.unjs.io)
33
+ - [RemixKit](https://github.com/jrestall/remix-kit)
34
+
22
35
  ## Usage
23
36
 
24
37
  Install package:
25
38
 
39
+ <!-- automd:pm-install -->
40
+
26
41
  ```sh
42
+ # ✨ Auto-detect
43
+ npx nypm i c12@^1.7.0
44
+
27
45
  # npm
28
- npm install c12
46
+ npm install c12@^1.7.0
29
47
 
30
48
  # yarn
31
- yarn add c12
49
+ yarn add c12@^1.7.0
32
50
 
33
51
  # pnpm
34
- pnpm install c12
52
+ pnpm install c12@^1.7.0
53
+
54
+ # bun
55
+ bun install c12@^1.7.0
35
56
  ```
36
57
 
58
+ <!-- /automd -->
59
+
37
60
  Import:
38
61
 
39
62
  ```js
package/dist/index.cjs CHANGED
@@ -309,11 +309,14 @@ async function resolveConfig(source, options, sourceOptions = {}) {
309
309
  });
310
310
  source = cloned.dir;
311
311
  }
312
- if (NPM_PACKAGE_RE.test(source)) {
312
+ const tryResolve = (id) => {
313
313
  try {
314
- source = options.jiti.resolve(source, { paths: [options.cwd] });
314
+ return options.jiti.resolve(id, { paths: [options.cwd] });
315
315
  } catch {
316
316
  }
317
+ };
318
+ if (NPM_PACKAGE_RE.test(source)) {
319
+ source = tryResolve(source) || source;
317
320
  }
318
321
  const ext = pathe.extname(source);
319
322
  const isDir = !ext || ext === pathe.basename(source);
@@ -323,16 +326,12 @@ async function resolveConfig(source, options, sourceOptions = {}) {
323
326
  }
324
327
  const res = {
325
328
  config: void 0,
329
+ configFile: void 0,
326
330
  cwd,
327
331
  source,
328
332
  sourceOptions
329
333
  };
330
- try {
331
- res.configFile = options.jiti.resolve(pathe.resolve(cwd, source), {
332
- paths: [cwd]
333
- });
334
- } catch {
335
- }
334
+ res.configFile = tryResolve(pathe.resolve(cwd, source)) || tryResolve(pathe.resolve(cwd, ".config", source.replace(/\.config$/, ""))) || tryResolve(pathe.resolve(cwd, ".config", source)) || source;
336
335
  if (!node_fs.existsSync(res.configFile)) {
337
336
  return res;
338
337
  }
@@ -340,7 +339,9 @@ async function resolveConfig(source, options, sourceOptions = {}) {
340
339
  const { parse } = await import('jsonc-parser');
341
340
  res.config = parse(await promises.readFile(res.configFile, "utf8"));
342
341
  } else if (res.configFile.endsWith(".json5")) {
343
- const { parse } = await import('json5');
342
+ const parse = await import('json5').then(
343
+ (m) => m.parse || m.default?.parse || m.default
344
+ );
344
345
  res.config = parse(await promises.readFile(res.configFile, "utf8"));
345
346
  } else {
346
347
  res.config = options.jiti(res.configFile);
package/dist/index.mjs CHANGED
@@ -289,11 +289,14 @@ async function resolveConfig(source, options, sourceOptions = {}) {
289
289
  });
290
290
  source = cloned.dir;
291
291
  }
292
- if (NPM_PACKAGE_RE.test(source)) {
292
+ const tryResolve = (id) => {
293
293
  try {
294
- source = options.jiti.resolve(source, { paths: [options.cwd] });
294
+ return options.jiti.resolve(id, { paths: [options.cwd] });
295
295
  } catch {
296
296
  }
297
+ };
298
+ if (NPM_PACKAGE_RE.test(source)) {
299
+ source = tryResolve(source) || source;
297
300
  }
298
301
  const ext = extname(source);
299
302
  const isDir = !ext || ext === basename(source);
@@ -303,16 +306,12 @@ async function resolveConfig(source, options, sourceOptions = {}) {
303
306
  }
304
307
  const res = {
305
308
  config: void 0,
309
+ configFile: void 0,
306
310
  cwd,
307
311
  source,
308
312
  sourceOptions
309
313
  };
310
- try {
311
- res.configFile = options.jiti.resolve(resolve(cwd, source), {
312
- paths: [cwd]
313
- });
314
- } catch {
315
- }
314
+ res.configFile = tryResolve(resolve(cwd, source)) || tryResolve(resolve(cwd, ".config", source.replace(/\.config$/, ""))) || tryResolve(resolve(cwd, ".config", source)) || source;
316
315
  if (!existsSync(res.configFile)) {
317
316
  return res;
318
317
  }
@@ -320,7 +319,9 @@ async function resolveConfig(source, options, sourceOptions = {}) {
320
319
  const { parse } = await import('jsonc-parser');
321
320
  res.config = parse(await readFile(res.configFile, "utf8"));
322
321
  } else if (res.configFile.endsWith(".json5")) {
323
- const { parse } = await import('json5');
322
+ const parse = await import('json5').then(
323
+ (m) => m.parse || m.default?.parse || m.default
324
+ );
324
325
  res.config = parse(await readFile(res.configFile, "utf8"));
325
326
  } else {
326
327
  res.config = options.jiti(res.configFile);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Smart Config Loader",
5
5
  "repository": "unjs/c12",
6
6
  "license": "MIT",
@@ -47,6 +47,7 @@
47
47
  "devDependencies": {
48
48
  "@types/node": "^20.11.5",
49
49
  "@vitest/coverage-v8": "^1.2.1",
50
+ "automd": "^0.2.0",
50
51
  "changelogen": "^0.5.5",
51
52
  "eslint": "^8.56.0",
52
53
  "eslint-config-unjs": "^0.2.1",