Skip to content

Commit

Permalink
feat: defaultConfig to be applied before extending
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 14, 2022
1 parent a90685c commit 1c4e898
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -93,11 +93,15 @@ Loads `.env` file if enabled. It is disabled by default.

### `defaults`

Specify default configuration. It has the **lowest** priority.
Specify default configuration. It has the **lowest** priority and is applied **after extending** config.

### `defaultConfig`

Specify default configuration. It is applied **before** extending config.

### `overides`

Specify override configuration. It has the **highest** priority.
Specify override configuration. It has the **highest** priority and is applied **before extending** config.

### `jiti`

Expand Down
4 changes: 3 additions & 1 deletion src/loader.ts
Expand Up @@ -37,6 +37,7 @@ export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
dotenv?: boolean | DotenvOptions

defaults?: T
defaultConfig?: T
overrides?: T

resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>
Expand Down Expand Up @@ -109,7 +110,8 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
r.config = defu(
opts.overrides,
config,
configRC
configRC,
opts.defaultConfig
) as T

// Allow extending
Expand Down
19 changes: 9 additions & 10 deletions test/index.test.ts
Expand Up @@ -21,11 +21,13 @@ describe('c12', () => {
}
},
overrides: {
overriden: true,
extends: ['virtual']
overriden: true
},
defaults: {
defaultConfig: true
},
defaultConfig: {
extends: ['virtual']
}
})

Expand Down Expand Up @@ -56,9 +58,6 @@ describe('c12', () => {
[
{
"config": {
"extends": [
"virtual",
],
"overriden": true,
},
},
Expand Down Expand Up @@ -112,11 +111,6 @@ describe('c12', () => {
"configFile": "<path>/fixture/base/config.ts",
"cwd": "<path>/fixture/base",
},
{
"config": {
"virtual": true,
},
},
{
"config": {
"devConfig": true,
Expand All @@ -131,6 +125,11 @@ describe('c12', () => {
"configFile": "<path>/fixture/node_modules/c12-npm-test/config.ts",
"cwd": "<path>/fixture/node_modules/c12-npm-test",
},
{
"config": {
"virtual": true,
},
},
]
`)
})
Expand Down

0 comments on commit 1c4e898

Please sign in to comment.