UNPKG

nuxt

Version:

Nuxt is a free and open-source framework with an intuitive and extendable way to create type-safe, performant and production-grade full-stack web applications and websites with Vue.js.

63 lines (62 loc) 1.97 kB
import { useNuxtApp } from "./nuxt.js"; import { reactive } from "vue"; import { klona } from "klona"; import __appConfig from "#build/app.config.mjs"; //#region src/app/config.ts const _getAppConfig = () => __appConfig; function isPojoOrArray(val) { return Array.isArray(val) || !!val && typeof val === "object" && val.constructor?.name === "Object"; } function deepDelete(obj, newObj) { if (Array.isArray(obj) && Array.isArray(newObj)) { obj.length = 0; obj.push(...newObj); return; } for (const key in obj) { const val = newObj[key]; if (!(key in newObj)) delete obj[key]; if (isPojoOrArray(val)) deepDelete(obj[key], newObj[key]); } } function deepAssign(obj, newObj) { for (const key in newObj) { if (key === "__proto__" || key === "constructor") continue; const val = newObj[key]; if (isPojoOrArray(val)) { const defaultVal = Array.isArray(val) ? [] : {}; if (Array.isArray(obj[key]) !== Array.isArray(val)) obj[key] = defaultVal; else obj[key] ??= defaultVal; deepAssign(obj[key], val); } else obj[key] = val; } } function useAppConfig() { const nuxtApp = useNuxtApp(); nuxtApp._appConfig ||= import.meta.server ? klona(__appConfig) : reactive(__appConfig); return nuxtApp._appConfig; } function _replaceAppConfig(newConfig) { const appConfig = useAppConfig(); deepAssign(appConfig, newConfig); deepDelete(appConfig, newConfig); } /** * Deep assign the current appConfig with the new one. * * Will preserve existing properties. */ function updateAppConfig(appConfig) { deepAssign(useAppConfig(), appConfig); } if (import.meta.dev) { if (import.meta.hot) import.meta.hot.accept((newModule) => { const newConfig = newModule?._getAppConfig(); if (newConfig) _replaceAppConfig(newConfig); }); if (import.meta.webpackHot) import.meta.webpackHot.accept("#build/app.config.mjs", () => { _replaceAppConfig(__appConfig); }); } //#endregion export { _getAppConfig, _replaceAppConfig, updateAppConfig, useAppConfig };