config.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { reactive } from "vue";
  2. import { useNuxtApp } from "./nuxt.mjs";
  3. import { defineNuxtPlugin } from '#app'
  4. export const _getAppConfig = () => defineNuxtPlugin;
  5. function deepDelete(obj, newObj) {
  6. for (const key in obj) {
  7. const val = newObj[key];
  8. if (!(key in newObj)) {
  9. delete obj[key];
  10. }
  11. if (val !== null && typeof val === "object") {
  12. deepDelete(obj[key], newObj[key]);
  13. }
  14. }
  15. }
  16. function deepAssign(obj, newObj) {
  17. for (const key in newObj) {
  18. const val = newObj[key];
  19. if (val !== null && typeof val === "object") {
  20. deepAssign(obj[key], val);
  21. } else {
  22. obj[key] = val;
  23. }
  24. }
  25. }
  26. export function useAppConfig() {
  27. const nuxtApp = useNuxtApp();
  28. if (!nuxtApp._appConfig) {
  29. nuxtApp._appConfig = reactive(defineNuxtPlugin);
  30. }
  31. return nuxtApp._appConfig;
  32. }
  33. export function updateAppConfig(appConfig) {
  34. const _appConfig = useAppConfig();
  35. deepAssign(_appConfig, appConfig);
  36. }
  37. if (process.dev) {
  38. let applyHMR = function(newConfig) {
  39. const appConfig = useAppConfig();
  40. if (newConfig && appConfig) {
  41. deepAssign(appConfig, newConfig);
  42. deepDelete(appConfig, newConfig);
  43. }
  44. };
  45. if (import.meta.hot) {
  46. import.meta.hot.accept((newModule) => {
  47. const newConfig = newModule._getAppConfig();
  48. applyHMR(newConfig);
  49. });
  50. }
  51. if (import.meta.webpackHot) {
  52. import.meta.webpackHot.accept("#build/app.config.mjs", () => {
  53. applyHMR(defineNuxtPlugin);
  54. });
  55. }
  56. }