Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat(app.config): improve merging strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahul committed Aug 24, 2022
1 parent 856c2a6 commit c54292b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 16 additions & 1 deletion examples/advanced/config-extends/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
export default defineAppConfig({
foo: 'user',
bar: 'user',
baz: 'base'
baz: 'base',
array: [
'user',
'user',
'user'
],
arrayNested: {
nested: {
key: 'user',
array: [
'user',
'user',
'user'
]
}
}
})
16 changes: 15 additions & 1 deletion examples/advanced/config-extends/base/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export default defineAppConfig({
bar: 'base',
baz: 'base'
baz: 'base',
array: [
'base',
'base',
'base'
],
arrayNested: {
nested: {
array: [
'base',
'base',
'base'
]
}
}
})
12 changes: 10 additions & 2 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,20 @@ export const appConfigTemplate: NuxtTemplate = {
write: true,
getContents: ({ app, nuxt }) => {
return `
import defu from 'defu'
import { createDefu } from 'defu'
const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id)}`).join('\n')}
export default defu(${app.configs.map((_id: string, index: number) => `cfg${index}`).concat(['inlineConfig']).join(', ')})
const merge = createDefu((obj, key, value) => {
if (obj[key] && Array.isArray(obj[key])) {
obj[key] = value
return true
}
})
export default merge(${app.configs.map((_id: string, index: number) => `cfg${index}`).concat(['inlineConfig']).join(', ')})
`
}
}
Expand Down

0 comments on commit c54292b

Please sign in to comment.