2022-08-17 15:23:13 +00:00
|
|
|
import { reactive } from 'vue'
|
2023-04-20 12:33:45 +00:00
|
|
|
import { klona } from 'klona'
|
2023-02-13 22:42:04 +00:00
|
|
|
import type { AppConfig } from 'nuxt/schema'
|
2023-03-11 21:16:01 +00:00
|
|
|
import { useNuxtApp } from './nuxt'
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error virtual file
|
2022-08-17 15:23:13 +00:00
|
|
|
import __appConfig from '#build/app.config.mjs'
|
|
|
|
|
2022-08-24 16:35:02 +00:00
|
|
|
type DeepPartial<T> = T extends Function ? T : T extends Record<string, any> ? { [P in keyof T]?: DeepPartial<T[P]> } : T
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
// Workaround for vite HMR with virtual modules
|
|
|
|
export const _getAppConfig = () => __appConfig as AppConfig
|
|
|
|
|
2022-08-24 16:35:02 +00:00
|
|
|
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') {
|
2023-01-13 23:28:07 +00:00
|
|
|
obj[key] = obj[key] || {}
|
2022-08-24 16:35:02 +00:00
|
|
|
deepAssign(obj[key], val)
|
|
|
|
} else {
|
|
|
|
obj[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
export function useAppConfig (): AppConfig {
|
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
if (!nuxtApp._appConfig) {
|
2023-08-07 22:03:40 +00:00
|
|
|
nuxtApp._appConfig = (import.meta.server ? klona(__appConfig) : reactive(__appConfig)) as AppConfig
|
2022-08-17 15:23:13 +00:00
|
|
|
}
|
|
|
|
return nuxtApp._appConfig
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:35:02 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
// HMR Support
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.dev) {
|
2023-11-09 17:01:13 +00:00
|
|
|
const applyHMR = (newConfig: AppConfig) => {
|
2022-08-17 15:23:13 +00:00
|
|
|
const appConfig = useAppConfig()
|
|
|
|
if (newConfig && appConfig) {
|
2022-08-22 09:14:25 +00:00
|
|
|
deepAssign(appConfig, newConfig)
|
2022-08-24 16:35:02 +00:00
|
|
|
deepDelete(appConfig, newConfig)
|
2022-08-22 09:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:23:13 +00:00
|
|
|
// Vite
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.accept((newModule) => {
|
2023-03-03 10:20:15 +00:00
|
|
|
const newConfig = newModule?._getAppConfig()
|
2022-08-17 15:23:13 +00:00
|
|
|
applyHMR(newConfig)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-13 15:00:57 +00:00
|
|
|
// webpack
|
2022-08-17 15:23:13 +00:00
|
|
|
if (import.meta.webpackHot) {
|
|
|
|
import.meta.webpackHot.accept('#build/app.config.mjs', () => {
|
|
|
|
applyHMR(__appConfig)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|