c12 0.2.7 → 0.2.10

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
@@ -24,7 +24,7 @@ Install package:
24
24
  npm install c12
25
25
 
26
26
  # yarn
27
- yarn install c12
27
+ yarn add c12
28
28
 
29
29
  # pnpm
30
30
  pnpm install c12
package/dist/index.cjs CHANGED
@@ -129,12 +129,19 @@ async function loadConfig(opts) {
129
129
  }
130
130
  Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: opts.cwd }));
131
131
  }
132
- r.config = defu__default(opts.overrides, config, configRC);
132
+ r.config = defu__default(
133
+ opts.overrides,
134
+ config,
135
+ configRC
136
+ );
133
137
  if (opts.extend) {
134
138
  await extendConfig(r.config, opts);
135
139
  r.layers = r.config._layers;
136
140
  delete r.config._layers;
137
- r.config = defu__default(r.config, ...r.layers.map((e) => e.config));
141
+ r.config = defu__default(
142
+ r.config,
143
+ ...r.layers.map((e) => e.config)
144
+ );
138
145
  }
139
146
  const baseLayers = [
140
147
  opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
@@ -155,12 +162,19 @@ async function extendConfig(config, opts) {
155
162
  if (!opts.extend) {
156
163
  return;
157
164
  }
158
- const key = opts.extend.extendKey;
159
- const extendSources = (Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean);
160
- delete config[key];
165
+ let keys = opts.extend.extendKey;
166
+ if (typeof keys === "string") {
167
+ keys = [keys];
168
+ }
169
+ const extendSources = [];
170
+ for (const key of keys) {
171
+ extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean));
172
+ delete config[key];
173
+ }
161
174
  for (const extendSource of extendSources) {
162
175
  const _config = await resolveConfig(extendSource, opts);
163
176
  if (!_config.config) {
177
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
164
178
  continue;
165
179
  }
166
180
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -172,7 +186,8 @@ async function extendConfig(config, opts) {
172
186
  }
173
187
  }
174
188
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
175
- const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false });
189
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
190
+ const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
176
191
  async function resolveConfig(source, opts) {
177
192
  if (opts.resolve) {
178
193
  const res2 = await opts.resolve(source, opts);
@@ -192,12 +207,18 @@ async function resolveConfig(source, opts) {
192
207
  await gittar.extract(tarFile, tmpdir);
193
208
  source = pathe.resolve(tmpdir, subPath);
194
209
  }
210
+ if (NPM_PACKAGE_RE.test(source)) {
211
+ try {
212
+ source = jiti.resolve(source, { paths: [opts.cwd] });
213
+ } catch (_err) {
214
+ }
215
+ }
195
216
  const isDir = !pathe.extname(source);
196
217
  const cwd = pathe.resolve(opts.cwd, isDir ? source : pathe.dirname(source));
197
218
  if (isDir) {
198
219
  source = opts.configFile;
199
220
  }
200
- const res = { config: {}, cwd };
221
+ const res = { config: null, cwd };
201
222
  try {
202
223
  res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
203
224
  } catch (_err) {
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ declare function loadDotenv(opts: DotenvOptions): Promise<Env>;
37
37
  interface InputConfig extends Record<string, any> {
38
38
  }
39
39
  interface ConfigLayer<T extends InputConfig = InputConfig> {
40
- config: T;
40
+ config: T | null;
41
41
  cwd?: string;
42
42
  configFile?: string;
43
43
  }
@@ -59,7 +59,7 @@ interface LoadConfigOptions<T extends InputConfig = InputConfig> {
59
59
  overrides?: T;
60
60
  resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>;
61
61
  extend?: false | {
62
- extendKey?: string;
62
+ extendKey?: string | string[];
63
63
  };
64
64
  }
65
65
  declare function loadConfig<T extends InputConfig = InputConfig>(opts: LoadConfigOptions<T>): Promise<ResolvedConfig<T>>;
package/dist/index.mjs CHANGED
@@ -105,12 +105,19 @@ async function loadConfig(opts) {
105
105
  }
106
106
  Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: opts.cwd }));
107
107
  }
108
- r.config = defu(opts.overrides, config, configRC);
108
+ r.config = defu(
109
+ opts.overrides,
110
+ config,
111
+ configRC
112
+ );
109
113
  if (opts.extend) {
110
114
  await extendConfig(r.config, opts);
111
115
  r.layers = r.config._layers;
112
116
  delete r.config._layers;
113
- r.config = defu(r.config, ...r.layers.map((e) => e.config));
117
+ r.config = defu(
118
+ r.config,
119
+ ...r.layers.map((e) => e.config)
120
+ );
114
121
  }
115
122
  const baseLayers = [
116
123
  opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
@@ -131,12 +138,19 @@ async function extendConfig(config, opts) {
131
138
  if (!opts.extend) {
132
139
  return;
133
140
  }
134
- const key = opts.extend.extendKey;
135
- const extendSources = (Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean);
136
- delete config[key];
141
+ let keys = opts.extend.extendKey;
142
+ if (typeof keys === "string") {
143
+ keys = [keys];
144
+ }
145
+ const extendSources = [];
146
+ for (const key of keys) {
147
+ extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean));
148
+ delete config[key];
149
+ }
137
150
  for (const extendSource of extendSources) {
138
151
  const _config = await resolveConfig(extendSource, opts);
139
152
  if (!_config.config) {
153
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
140
154
  continue;
141
155
  }
142
156
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -148,7 +162,8 @@ async function extendConfig(config, opts) {
148
162
  }
149
163
  }
150
164
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
151
- const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false });
165
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
166
+ const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
152
167
  async function resolveConfig(source, opts) {
153
168
  if (opts.resolve) {
154
169
  const res2 = await opts.resolve(source, opts);
@@ -168,12 +183,18 @@ async function resolveConfig(source, opts) {
168
183
  await gittar.extract(tarFile, tmpdir);
169
184
  source = resolve(tmpdir, subPath);
170
185
  }
186
+ if (NPM_PACKAGE_RE.test(source)) {
187
+ try {
188
+ source = jiti.resolve(source, { paths: [opts.cwd] });
189
+ } catch (_err) {
190
+ }
191
+ }
171
192
  const isDir = !extname(source);
172
193
  const cwd = resolve(opts.cwd, isDir ? source : dirname(source));
173
194
  if (isDir) {
174
195
  source = opts.configFile;
175
196
  }
176
- const res = { config: {}, cwd };
197
+ const res = { config: null, cwd };
177
198
  try {
178
199
  res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
179
200
  } catch (_err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "0.2.7",
3
+ "version": "0.2.10",
4
4
  "description": "Smart Config Loader",
5
5
  "repository": "unjs/c12",
6
6
  "license": "MIT",
@@ -20,12 +20,12 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "defu": "^6.0.0",
23
- "dotenv": "^16.0.0",
23
+ "dotenv": "^16.0.1",
24
24
  "gittar": "^0.1.1",
25
- "jiti": "^1.12.14",
26
- "mlly": "^0.5.1",
27
- "pathe": "^0.2.0",
28
- "rc9": "^1.2.1"
25
+ "jiti": "^1.14.0",
26
+ "mlly": "^0.5.11",
27
+ "pathe": "^0.3.4",
28
+ "rc9": "^1.2.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@nuxtjs/eslint-config-typescript": "latest",
@@ -36,13 +36,12 @@
36
36
  "unbuild": "latest",
37
37
  "vitest": "latest"
38
38
  },
39
- "packageManager": "pnpm@6.32.3",
39
+ "packageManager": "pnpm@7.9.0",
40
40
  "scripts": {
41
41
  "build": "unbuild",
42
42
  "dev": "vitest dev",
43
43
  "lint": "eslint --ext .ts,.js,.mjs,.cjs .",
44
44
  "release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
45
45
  "test": "vitest run --coverage"
46
- },
47
- "readme": "# c12\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![Github Actions][github-actions-src]][github-actions-href]\n[![Codecov][codecov-src]][codecov-href]\n\n> Smart Configuration Loader\n\n## Features\n\n- JSON, CJS, Typescript and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti)\n- RC config support with [unjs/rc9](https://github.com/unjs/rc9)\n- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)\n- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)\n- Support extending nested configurations from multiple local or git sources\n\n## Usage\n\nInstall package:\n\n```sh\n# npm\nnpm install c12\n\n# yarn\nyarn install c12\n\n# pnpm\npnpm install c12\n```\n\nImport:\n\n```js\n// ESM\nimport { loadConfig } from 'c12'\n\n// CommonJS\nconst { loadConfig } = require('c12')\n```\n\nLoad configuration:\n\n```js\n// Get loaded config\nconst { config } = await loadConfig({})\n\n// Get resolved config and extended layers\nconst { config, configFile, layers } = await loadConfig({})\n```\n\n## Loading priority\n\nc12 merged config sources with [unjs/defu](https://github.com/unjs/defu) by below order:\n\n1. config overrides passed by options\n2. config file in CWD\n3. RC file in CWD\n4. global RC file in user's home directory\n5. default config passed by options\n6. Extended config layers\n\n## Options\n\n### `cwd`\n\nResolve configuration from this working directory. Default is `process.cwd()`\n\n### `name`\n\nConfiguration base name. Default is `config`.\n\n### `configName`\n\nConfiguration file name without extension . Default is generated from `name` (name=foo => `foo.config`).\n\nSet to `false` to avoid loading config file.\n\n### `rcFile`\n\nRC Config file name. Default is generated from `name` (name=foo => `.foorc`).\n\nSet to `false` to disable loading RC config.\n\n### `globalRC`\n\nLoad RC config from the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.\n\n### `dotenv`\n\nLoads `.env` file if enabled. It is disabled by default.\n\n### `defaults`\n\nSpecify default configuration. It has the **lowest** priority.\n\n### `overides`\n\nSpecify override configuration. It has the **highest** priority.\n\n## Extending configuration\n\nIf resolved config contains a `extends` key, it will be used to extend configuration.\n\nExtending can be nested and each layer can extend from one base or more.\n\nFinal config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu).\n\nEach item in extends, is a string that can be either an absolute or relative path to current config file pointing to a config file for extending or directory containing config file.\nIf it starts with either of `github:`, `gitlab:`, `bitbucket:` or `https:`, c12 autmatically clones it.\n\nFor custom merging strategies, you can directly access each layer with `layers` property.\n\n**Example:**\n\n```js\n// config.ts\nexport default {\n colors: {\n primary: 'user_primary'\n },\n extends: [\n './theme',\n './config.dev.ts'\n ]\n}\n```\n\n```js\n// config.dev.ts\nexport default {\n dev: true\n}\n```\n\n```js\n// theme/config.ts\nexport default {\n extends: '../base',\n colors: {\n primary: 'theme_primary',\n secondary: 'theme_secondary'\n }\n}\n```\n\n```js\n// base/config.ts\nexport default {\n colors: {\n primary: 'base_primary'\n text: 'base_text'\n }\n}\n```\n\nLoaded configuration would look like this:\n\n```js\n{\n dev: true,\n colors: {\n primary: 'user_primary',\n secondary: 'theme_secondary',\n text: 'base_text'\n }\n}\n```\n\nLayers:\n\n```js\n[\n { config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },\n { config: /* base config */, configFile: /* path/to/base/config.ts */, cwd: /* path/to/base */ },\n { config: /* dev config */, configFile: /* path/to/config.dev.ts */, cwd: /* path/ */ },\n]\n```\n\n## 💻 Development\n\n- Clone this repository\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n## License\n\nMade with 💛 Published under [MIT License](./LICENSE).\n\n<!-- Badges -->\n[npm-version-src]: https://img.shields.io/npm/v/c12?style=flat-square\n[npm-version-href]: https://npmjs.com/package/c12\n\n[npm-downloads-src]: https://img.shields.io/npm/dm/c12?style=flat-square\n[npm-downloads-href]: https://npmjs.com/package/c12\n\n[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/c12/ci/main?style=flat-square\n[github-actions-href]: https://github.com/unjs/c12/actions?query=workflow%3Aci\n\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/c12/main?style=flat-square\n[codecov-href]: https://codecov.io/gh/unjs/c12\n"
46
+ }
48
47
  }