c12 0.2.10 → 0.2.12

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
@@ -85,7 +85,7 @@ Set to `false` to disable loading RC config.
85
85
 
86
86
  ### `globalRC`
87
87
 
88
- Load RC config from the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.
88
+ Load RC config from the workspace directory and user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.
89
89
 
90
90
  ### `dotenv`
91
91
 
@@ -93,11 +93,23 @@ Loads `.env` file if enabled. It is disabled by default.
93
93
 
94
94
  ### `defaults`
95
95
 
96
- Specify default configuration. It has the **lowest** priority.
96
+ Specify default configuration. It has the **lowest** priority and is applied **after extending** config.
97
+
98
+ ### `defaultConfig`
99
+
100
+ Specify default configuration. It is applied **before** extending config.
97
101
 
98
102
  ### `overides`
99
103
 
100
- Specify override configuration. It has the **highest** priority.
104
+ Specify override configuration. It has the **highest** priority and is applied **before extending** config.
105
+
106
+ ### `jiti`
107
+
108
+ Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configuration files.
109
+
110
+ ### `jitiOptions`
111
+
112
+ Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.
101
113
 
102
114
  ## Extending configuration
103
115
 
package/dist/index.cjs CHANGED
@@ -9,6 +9,7 @@ const os = require('os');
9
9
  const createJiti = require('jiti');
10
10
  const rc9 = require('rc9');
11
11
  const defu = require('defu');
12
+ const pkgTypes = require('pkg-types');
12
13
 
13
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
14
15
 
@@ -28,7 +29,6 @@ const dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
28
29
  const os__default = /*#__PURE__*/_interopDefaultLegacy(os);
29
30
  const createJiti__default = /*#__PURE__*/_interopDefaultLegacy(createJiti);
30
31
  const rc9__namespace = /*#__PURE__*/_interopNamespace(rc9);
31
- const defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
32
32
 
33
33
  async function setupDotenv(options) {
34
34
  const targetEnv = options.env ?? process.env;
@@ -106,6 +106,12 @@ async function loadConfig(opts) {
106
106
  ...opts.extend
107
107
  };
108
108
  }
109
+ opts.jiti = opts.jiti || createJiti__default(null, {
110
+ interopDefault: true,
111
+ requireCache: false,
112
+ esmResolve: true,
113
+ ...opts.jitiOptions
114
+ });
109
115
  const r = {
110
116
  config: {},
111
117
  cwd: opts.cwd,
@@ -126,19 +132,24 @@ async function loadConfig(opts) {
126
132
  if (opts.rcFile) {
127
133
  if (opts.globalRc) {
128
134
  Object.assign(configRC, rc9__namespace.readUser({ name: opts.rcFile, dir: opts.cwd }));
135
+ const workspaceDir = await pkgTypes.findWorkspaceDir(opts.cwd).catch(() => null);
136
+ if (workspaceDir) {
137
+ Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: workspaceDir }));
138
+ }
129
139
  }
130
140
  Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: opts.cwd }));
131
141
  }
132
- r.config = defu__default(
142
+ r.config = defu.defu(
133
143
  opts.overrides,
134
144
  config,
135
- configRC
145
+ configRC,
146
+ opts.defaultConfig
136
147
  );
137
148
  if (opts.extend) {
138
149
  await extendConfig(r.config, opts);
139
150
  r.layers = r.config._layers;
140
151
  delete r.config._layers;
141
- r.config = defu__default(
152
+ r.config = defu.defu(
142
153
  r.config,
143
154
  ...r.layers.map((e) => e.config)
144
155
  );
@@ -153,7 +164,7 @@ async function loadConfig(opts) {
153
164
  ...r.layers
154
165
  ];
155
166
  if (opts.defaults) {
156
- r.config = defu__default(r.config, opts.defaults);
167
+ r.config = defu.defu(r.config, opts.defaults);
157
168
  }
158
169
  return r;
159
170
  }
@@ -172,9 +183,13 @@ async function extendConfig(config, opts) {
172
183
  delete config[key];
173
184
  }
174
185
  for (const extendSource of extendSources) {
186
+ if (typeof extendSource !== "string") {
187
+ console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${opts.cwd}`);
188
+ continue;
189
+ }
175
190
  const _config = await resolveConfig(extendSource, opts);
176
191
  if (!_config.config) {
177
- console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
192
+ console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
178
193
  continue;
179
194
  }
180
195
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -187,7 +202,6 @@ async function extendConfig(config, opts) {
187
202
  }
188
203
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
189
204
  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 });
191
205
  async function resolveConfig(source, opts) {
192
206
  if (opts.resolve) {
193
207
  const res2 = await opts.resolve(source, opts);
@@ -209,7 +223,7 @@ async function resolveConfig(source, opts) {
209
223
  }
210
224
  if (NPM_PACKAGE_RE.test(source)) {
211
225
  try {
212
- source = jiti.resolve(source, { paths: [opts.cwd] });
226
+ source = opts.jiti.resolve(source, { paths: [opts.cwd] });
213
227
  } catch (_err) {
214
228
  }
215
229
  }
@@ -220,13 +234,13 @@ async function resolveConfig(source, opts) {
220
234
  }
221
235
  const res = { config: null, cwd };
222
236
  try {
223
- res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
237
+ res.configFile = opts.jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
224
238
  } catch (_err) {
225
239
  }
226
240
  if (!fs.existsSync(res.configFile)) {
227
241
  return res;
228
242
  }
229
- res.config = jiti(res.configFile);
243
+ res.config = opts.jiti(res.configFile);
230
244
  if (typeof res.config === "function") {
231
245
  res.config = await res.config();
232
246
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { JITI } from 'jiti';
2
+ import { JITIOptions } from 'jiti/dist/types';
3
+
1
4
  interface DotenvOptions {
2
5
  /**
3
6
  * The project root directory (either absolute or relative to the current working directory).
@@ -56,8 +59,11 @@ interface LoadConfigOptions<T extends InputConfig = InputConfig> {
56
59
  globalRc?: boolean;
57
60
  dotenv?: boolean | DotenvOptions;
58
61
  defaults?: T;
62
+ defaultConfig?: T;
59
63
  overrides?: T;
60
64
  resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>;
65
+ jiti?: JITI;
66
+ jitiOptions?: JITIOptions;
61
67
  extend?: false | {
62
68
  extendKey?: string | string[];
63
69
  };
package/dist/index.mjs CHANGED
@@ -4,7 +4,8 @@ import * as dotenv from 'dotenv';
4
4
  import os from 'os';
5
5
  import createJiti from 'jiti';
6
6
  import * as rc9 from 'rc9';
7
- import defu from 'defu';
7
+ import { defu } from 'defu';
8
+ import { findWorkspaceDir } from 'pkg-types';
8
9
 
9
10
  async function setupDotenv(options) {
10
11
  const targetEnv = options.env ?? process.env;
@@ -82,6 +83,12 @@ async function loadConfig(opts) {
82
83
  ...opts.extend
83
84
  };
84
85
  }
86
+ opts.jiti = opts.jiti || createJiti(null, {
87
+ interopDefault: true,
88
+ requireCache: false,
89
+ esmResolve: true,
90
+ ...opts.jitiOptions
91
+ });
85
92
  const r = {
86
93
  config: {},
87
94
  cwd: opts.cwd,
@@ -102,13 +109,18 @@ async function loadConfig(opts) {
102
109
  if (opts.rcFile) {
103
110
  if (opts.globalRc) {
104
111
  Object.assign(configRC, rc9.readUser({ name: opts.rcFile, dir: opts.cwd }));
112
+ const workspaceDir = await findWorkspaceDir(opts.cwd).catch(() => null);
113
+ if (workspaceDir) {
114
+ Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: workspaceDir }));
115
+ }
105
116
  }
106
117
  Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: opts.cwd }));
107
118
  }
108
119
  r.config = defu(
109
120
  opts.overrides,
110
121
  config,
111
- configRC
122
+ configRC,
123
+ opts.defaultConfig
112
124
  );
113
125
  if (opts.extend) {
114
126
  await extendConfig(r.config, opts);
@@ -148,9 +160,13 @@ async function extendConfig(config, opts) {
148
160
  delete config[key];
149
161
  }
150
162
  for (const extendSource of extendSources) {
163
+ if (typeof extendSource !== "string") {
164
+ console.warn(`Cannot extend config from \`${JSON.stringify(extendSource)}\` (which should be a string) in ${opts.cwd}`);
165
+ continue;
166
+ }
151
167
  const _config = await resolveConfig(extendSource, opts);
152
168
  if (!_config.config) {
153
- console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
169
+ console.warn(`Cannot extend config from \`${extendSource}\` in ${opts.cwd}`);
154
170
  continue;
155
171
  }
156
172
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -163,7 +179,6 @@ async function extendConfig(config, opts) {
163
179
  }
164
180
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
165
181
  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 });
167
182
  async function resolveConfig(source, opts) {
168
183
  if (opts.resolve) {
169
184
  const res2 = await opts.resolve(source, opts);
@@ -185,7 +200,7 @@ async function resolveConfig(source, opts) {
185
200
  }
186
201
  if (NPM_PACKAGE_RE.test(source)) {
187
202
  try {
188
- source = jiti.resolve(source, { paths: [opts.cwd] });
203
+ source = opts.jiti.resolve(source, { paths: [opts.cwd] });
189
204
  } catch (_err) {
190
205
  }
191
206
  }
@@ -196,13 +211,13 @@ async function resolveConfig(source, opts) {
196
211
  }
197
212
  const res = { config: null, cwd };
198
213
  try {
199
- res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
214
+ res.configFile = opts.jiti.resolve(resolve(cwd, source), { paths: [cwd] });
200
215
  } catch (_err) {
201
216
  }
202
217
  if (!existsSync(res.configFile)) {
203
218
  return res;
204
219
  }
205
- res.config = jiti(res.configFile);
220
+ res.config = opts.jiti(res.configFile);
206
221
  if (typeof res.config === "function") {
207
222
  res.config = await res.config();
208
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Smart Config Loader",
5
5
  "repository": "unjs/c12",
6
6
  "license": "MIT",
@@ -19,24 +19,25 @@
19
19
  "dist"
20
20
  ],
21
21
  "dependencies": {
22
- "defu": "^6.0.0",
23
- "dotenv": "^16.0.1",
22
+ "defu": "^6.1.0",
23
+ "dotenv": "^16.0.2",
24
24
  "gittar": "^0.1.1",
25
- "jiti": "^1.14.0",
26
- "mlly": "^0.5.11",
27
- "pathe": "^0.3.4",
25
+ "jiti": "^1.15.0",
26
+ "mlly": "^0.5.14",
27
+ "pathe": "^0.3.7",
28
+ "pkg-types": "^0.3.5",
28
29
  "rc9": "^1.2.2"
29
30
  },
30
31
  "devDependencies": {
31
- "@nuxtjs/eslint-config-typescript": "latest",
32
- "c8": "latest",
33
- "eslint": "latest",
34
- "standard-version": "latest",
35
- "typescript": "latest",
36
- "unbuild": "latest",
37
- "vitest": "latest"
32
+ "@nuxtjs/eslint-config-typescript": "^11.0.0",
33
+ "@vitest/coverage-c8": "^0.23.1",
34
+ "eslint": "^8.23.0",
35
+ "standard-version": "^9.5.0",
36
+ "typescript": "^4.8.2",
37
+ "unbuild": "^0.8.10",
38
+ "vitest": "^0.23.1"
38
39
  },
39
- "packageManager": "pnpm@7.9.0",
40
+ "packageManager": "pnpm@7.11.0",
40
41
  "scripts": {
41
42
  "build": "unbuild",
42
43
  "dev": "vitest dev",