Skip to content

Commit 4a0227d

Browse files
committed
feat: envName config
1 parent ec03711 commit 4a0227d

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configu
121121

122122
Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.
123123

124+
### `envName`
125+
126+
Environment name used for [environment specific configuration](#environment-specific-configuration).
127+
128+
Default is `process.env.NODE_ENV`. You can set `envName` to `false` or an empty string to disable the feature.
129+
124130
## Extending configuration
125131

126132
If resolved config contains a `extends` key, it will be used to extend configuration.
@@ -206,7 +212,7 @@ Users can define environment specific configuration using those config keys:
206212
- `$production: {...}`
207213
- `$env: { [env]: {...} }`
208214

209-
c12 matches `$envName` or `NODE_ENV` environment variable to the env config and overrides it.
215+
c12 matches [`envName`](#envname) confi the env config and overrides it.
210216

211217
**Note:** Environment will be applied when extending each configuration layer. This way layers can provide environment specific configuration.
212218

src/loader.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface ConfigLayerMeta {
1717
}
1818

1919
export interface C12InputConfig {
20-
$envName?: string;
2120
$test?: UserInputConfig;
2221
$development?: UserInputConfig;
2322
$production?: UserInputConfig;
@@ -63,6 +62,8 @@ export interface LoadConfigOptions<T extends InputConfig = InputConfig> {
6362

6463
dotenv?: boolean | DotenvOptions;
6564

65+
envName?: string | false;
66+
6667
packageJson?: boolean | string | string[];
6768

6869
defaults?: T;
@@ -90,6 +91,7 @@ export async function loadConfig<T extends InputConfig = InputConfig>(
9091
// Normalize options
9192
options.cwd = resolve(process.cwd(), options.cwd || ".");
9293
options.name = options.name || "config";
94+
options.envName = options.envName ?? process.env.NODE_ENV;
9395
options.configFile =
9496
options.configFile ??
9597
(options.name !== "config" ? `${options.name}.config` : "config");
@@ -332,13 +334,14 @@ async function resolveConfig(
332334
}
333335

334336
// Extend env specific config
335-
const envName = res.config.$envName || process.env.NODE_ENV || "default";
336-
const envConfig = {
337-
...res.config["$" + envName],
338-
...res.config.$env?.[envName],
339-
};
340-
if (Object.keys(envConfig).length > 0) {
341-
res.config = defu(envConfig, res.config);
337+
if (options.envName) {
338+
const envConfig = {
339+
...res.config["$" + options.envName],
340+
...res.config.$env?.[options.envName],
341+
};
342+
if (Object.keys(envConfig).length > 0) {
343+
res.config = defu(envConfig, res.config);
344+
}
342345
}
343346

344347
// Meta

test/index.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("c12", () => {
1313
dotenv: true,
1414
packageJson: ["c12", "c12-alt"],
1515
globalRc: true,
16+
envName: "test",
1617
extend: {
1718
extendKey: ["theme", "extends"],
1819
},

0 commit comments

Comments
 (0)