mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(nuxt): app.config
improvements (#6905)
This commit is contained in:
parent
a4dfe232f0
commit
1700bf822e
20
docs/content/3.api/1.composables/update-app-config.md
Normal file
20
docs/content/3.api/1.composables/update-app-config.md
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# `updateAppConfig`
|
||||||
|
|
||||||
|
::StabilityEdge
|
||||||
|
::
|
||||||
|
|
||||||
|
Updates [app config](/guide/features/app-config) using deep assignment. Existing (nested) properties will be preserved.
|
||||||
|
|
||||||
|
**Usage:**
|
||||||
|
|
||||||
|
```js
|
||||||
|
const appConfig = useAppConfig() // { foo: 'bar' }
|
||||||
|
|
||||||
|
const newAppConfig = { foo: 'baz' }
|
||||||
|
|
||||||
|
updateAppConfig(newAppConfig)
|
||||||
|
|
||||||
|
console.log(appConfig) // { foo: 'baz' }
|
||||||
|
```
|
||||||
|
|
||||||
|
::ReadMore{link="/guide/features/app-config"}
|
@ -1,5 +1,10 @@
|
|||||||
export default defineAppConfig({
|
export default defineAppConfig({
|
||||||
foo: 'user',
|
foo: 'user',
|
||||||
bar: 'user',
|
bar: 'user',
|
||||||
baz: 'base'
|
baz: 'base',
|
||||||
|
array: [
|
||||||
|
'user',
|
||||||
|
'user',
|
||||||
|
'user'
|
||||||
|
]
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,18 @@
|
|||||||
export default defineAppConfig({
|
export default defineAppConfig({
|
||||||
bar: 'base',
|
bar: 'base',
|
||||||
baz: 'base'
|
baz: 'base',
|
||||||
|
array: () => [
|
||||||
|
'base',
|
||||||
|
'base',
|
||||||
|
'base'
|
||||||
|
],
|
||||||
|
arrayNested: {
|
||||||
|
nested: {
|
||||||
|
array: [
|
||||||
|
'base',
|
||||||
|
'base',
|
||||||
|
'base'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
@ -4,9 +4,35 @@ import { useNuxtApp } from './nuxt'
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import __appConfig from '#build/app.config.mjs'
|
import __appConfig from '#build/app.config.mjs'
|
||||||
|
|
||||||
|
type DeepPartial<T> = T extends Function ? T : T extends Record<string, any> ? { [P in keyof T]?: DeepPartial<T[P]> } : T
|
||||||
|
|
||||||
// Workaround for vite HMR with virtual modules
|
// Workaround for vite HMR with virtual modules
|
||||||
export const _getAppConfig = () => __appConfig as AppConfig
|
export const _getAppConfig = () => __appConfig as AppConfig
|
||||||
|
|
||||||
|
function deepDelete (obj: any, newObj: any) {
|
||||||
|
for (const key in obj) {
|
||||||
|
const val = newObj[key]
|
||||||
|
if (!(key in newObj)) {
|
||||||
|
delete (obj as any)[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val !== null && typeof val === 'object') {
|
||||||
|
deepDelete(obj[key], newObj[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function deepAssign (obj: any, newObj: any) {
|
||||||
|
for (const key in newObj) {
|
||||||
|
const val = newObj[key]
|
||||||
|
if (val !== null && typeof val === 'object') {
|
||||||
|
deepAssign(obj[key], val)
|
||||||
|
} else {
|
||||||
|
obj[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function useAppConfig (): AppConfig {
|
export function useAppConfig (): AppConfig {
|
||||||
const nuxtApp = useNuxtApp()
|
const nuxtApp = useNuxtApp()
|
||||||
if (!nuxtApp._appConfig) {
|
if (!nuxtApp._appConfig) {
|
||||||
@ -15,28 +41,23 @@ export function useAppConfig (): AppConfig {
|
|||||||
return nuxtApp._appConfig
|
return nuxtApp._appConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deep assign the current appConfig with the new one.
|
||||||
|
*
|
||||||
|
* Will preserve existing properties.
|
||||||
|
*/
|
||||||
|
export function updateAppConfig (appConfig: DeepPartial<AppConfig>) {
|
||||||
|
const _appConfig = useAppConfig()
|
||||||
|
deepAssign(_appConfig, appConfig)
|
||||||
|
}
|
||||||
|
|
||||||
// HMR Support
|
// HMR Support
|
||||||
if (process.dev) {
|
if (process.dev) {
|
||||||
function applyHMR (newConfig: AppConfig) {
|
function applyHMR (newConfig: AppConfig) {
|
||||||
const appConfig = useAppConfig()
|
const appConfig = useAppConfig()
|
||||||
if (newConfig && appConfig) {
|
if (newConfig && appConfig) {
|
||||||
deepAssign(appConfig, newConfig)
|
deepAssign(appConfig, newConfig)
|
||||||
for (const key in appConfig) {
|
deepDelete(appConfig, newConfig)
|
||||||
if (!(key in newConfig)) {
|
|
||||||
delete (appConfig as any)[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function deepAssign (obj: any, newObj: any) {
|
|
||||||
for (const key in newObj) {
|
|
||||||
const val = newObj[key]
|
|
||||||
if (val !== null && typeof val === 'object') {
|
|
||||||
deepAssign(obj[key], val)
|
|
||||||
} else {
|
|
||||||
obj[key] = val
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,12 +207,13 @@ export const appConfigTemplate: NuxtTemplate = {
|
|||||||
write: true,
|
write: true,
|
||||||
getContents: ({ app, nuxt }) => {
|
getContents: ({ app, nuxt }) => {
|
||||||
return `
|
return `
|
||||||
import defu from 'defu'
|
import { defuFn } from 'defu'
|
||||||
|
|
||||||
const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
|
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')}
|
${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(', ')})
|
|
||||||
|
export default defuFn(${app.configs.map((_id: string, index: number) => `cfg${index}`).concat(['inlineConfig']).join(', ')})
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ const appPreset = defineUnimportPreset({
|
|||||||
'createError',
|
'createError',
|
||||||
'defineNuxtLink',
|
'defineNuxtLink',
|
||||||
'useAppConfig',
|
'useAppConfig',
|
||||||
|
'updateAppConfig',
|
||||||
'defineAppConfig',
|
'defineAppConfig',
|
||||||
'preloadComponents',
|
'preloadComponents',
|
||||||
'prefetchComponents'
|
'prefetchComponents'
|
||||||
|
Loading…
Reference in New Issue
Block a user