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

Commit

Permalink
fix(nuxt): use deep assignment for app.config hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 19, 2022
1 parent d3ccb79 commit 1ed49f9
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/nuxt/src/app/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { AppConfig } from '@nuxt/schema'
import { reactive } from 'vue'
import { reactive, toRef } from 'vue'
import { useNuxtApp } from './nuxt'
// @ts-ignore
import __appConfig from '#build/app.config.mjs'

// Workaround for vite HMR with virtual modules
export const _getAppConfig = () => __appConfig as AppConfig

export function useAppConfig (): AppConfig {
export function useAppConfig (key?: string) {
const nuxtApp = useNuxtApp()
if (!nuxtApp._appConfig) {
nuxtApp._appConfig = reactive(__appConfig) as AppConfig
}
if (key) {
return reactive(toRef(nuxtApp._appConfig, key))
}
return nuxtApp._appConfig
}

Expand All @@ -20,9 +23,7 @@ if (process.dev) {
function applyHMR (newConfig: AppConfig) {
const appConfig = useAppConfig()
if (newConfig && appConfig) {
for (const key in newConfig) {
(appConfig as any)[key] = (newConfig as any)[key]
}
deepAssign(appConfig, newConfig)
for (const key in appConfig) {
if (!(key in newConfig)) {
delete (appConfig as any)[key]
Expand All @@ -31,6 +32,17 @@ if (process.dev) {
}
}

function deepAssign (obj: any, newObj: any) {
for (const key in newObj) {
const val = newObj[key]
if (val && typeof val === 'object') {
deepAssign(obj[key], val)
} else {
obj[key] = val
}
}
}

// Vite
if (import.meta.hot) {
import.meta.hot.accept((newModule) => {
Expand Down

0 comments on commit 1ed49f9

Please sign in to comment.