mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-16 02:44:51 +00:00
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
|
import type { AppConfig } from '@nuxt/schema'
|
||
|
import { reactive } 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 {
|
||
|
const nuxtApp = useNuxtApp()
|
||
|
if (!nuxtApp._appConfig) {
|
||
|
nuxtApp._appConfig = reactive(__appConfig) as AppConfig
|
||
|
}
|
||
|
return nuxtApp._appConfig
|
||
|
}
|
||
|
|
||
|
// HMR Support
|
||
|
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]
|
||
|
}
|
||
|
for (const key in appConfig) {
|
||
|
if (!(key in newConfig)) {
|
||
|
delete (appConfig as any)[key]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Vite
|
||
|
if (import.meta.hot) {
|
||
|
import.meta.hot.accept((newModule) => {
|
||
|
const newConfig = newModule._getAppConfig()
|
||
|
applyHMR(newConfig)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Webpack
|
||
|
if (import.meta.webpackHot) {
|
||
|
import.meta.webpackHot.accept('#build/app.config.mjs', () => {
|
||
|
applyHMR(__appConfig)
|
||
|
})
|
||
|
}
|
||
|
}
|