Skip to content

Commit

Permalink
fix: avoid double merging of layers
Browse files Browse the repository at this point in the history
resolves nuxt/framework#3800
  • Loading branch information
pi0 committed Mar 21, 2022
1 parent 389e6d2 commit 367cbf8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
23 changes: 12 additions & 11 deletions src/loader.ts
Expand Up @@ -95,27 +95,28 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
configRC
) as T

// Preserve unmerged sources
const baseLayers = [
opts.overrides && { config: opts.overrides, configFile: undefined, cwd: undefined },
{ config, configFile: opts.configFile, cwd: opts.cwd },
opts.rcFile && { config: configRC, configFile: opts.rcFile }
].filter(l => l && l.config)

// Allow extending
if (opts.extend) {
await extendConfig(r.config, opts)
r.layers = [
...baseLayers,
...r.config._layers
]
r.layers = r.config._layers
delete r.config._layers
r.config = defu(
r.config,
...r.layers.map(e => e.config)
) as T
}

// Preserve unmerged sources as layers
const baseLayers = [
opts.overrides && { config: opts.overrides, configFile: undefined, cwd: undefined },
{ config, configFile: opts.configFile, cwd: opts.cwd },
opts.rcFile && { config: configRC, configFile: opts.rcFile }
].filter(l => l && l.config) as ConfigLayer<T>[]
r.layers = [
...baseLayers,
...r.layers
]

// Apply defaults
if (opts.defaults) {
r.config = defu(r.config, opts.defaults) as T
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/base/config.ts
Expand Up @@ -3,5 +3,6 @@ export default {
colors: {
primary: 'base_primary',
text: 'base_text'
}
},
array: ['b']
}
3 changes: 2 additions & 1 deletion test/fixture/config.ts
Expand Up @@ -7,5 +7,6 @@ export default {
primary: 'user_primary'
},
configFile: true,
overriden: false
overriden: false,
array: ['a']
}
15 changes: 10 additions & 5 deletions test/index.test.ts
Expand Up @@ -27,6 +27,10 @@ describe('c12', () => {

expect(transformPaths(config)).toMatchInlineSnapshot(`
{
"array": [
"b",
"a",
],
"baseConfig": true,
"colors": {
"primary": "user_primary",
Expand All @@ -36,11 +40,6 @@ describe('c12', () => {
"configFile": true,
"defaultConfig": true,
"devConfig": true,
"extends": [
"./theme",
"./config.dev",
"virtual",
],
"overriden": true,
"rcFile": true,
"virtual": true,
Expand All @@ -59,6 +58,9 @@ describe('c12', () => {
},
{
"config": {
"array": [
"a",
],
"colors": {
"primary": "user_primary",
},
Expand Down Expand Up @@ -90,6 +92,9 @@ describe('c12', () => {
},
{
"config": {
"array": [
"b",
],
"baseConfig": true,
"colors": {
"primary": "base_primary",
Expand Down

0 comments on commit 367cbf8

Please sign in to comment.