Преглед изворни кода

build(架构): 添加热更新插件,解决每次修改都需要重新刷新的问题

shizhongming пре 2 година
родитељ
комит
9a1e267e12
2 измењених фајлова са 29 додато и 0 уклоњено
  1. 24 0
      internal/vite-config/src/plugins/hmr.ts
  2. 5 0
      internal/vite-config/src/plugins/index.ts

+ 24 - 0
internal/vite-config/src/plugins/hmr.ts

@@ -0,0 +1,24 @@
+import type { Plugin } from 'vite';
+
+/**
+ * TODO
+ * Temporarily solve the Vite circular dependency problem, and wait for a better solution to fix it later. I don't know what problems this writing will bring.
+ * @returns
+ */
+
+export function configHmrPlugin(): Plugin {
+  return {
+    name: 'singleHMR',
+    handleHotUpdate({ modules, file }) {
+      if (file.match(/xml$/)) return [];
+      if (file.match(/\.(ts|js)/)) return undefined;
+
+      modules.forEach((m) => {
+        if (!m.url.match(/\.(css|less)/)) {
+          m.importers = new Set();
+        }
+      });
+      return modules;
+    },
+  };
+}

+ 5 - 0
internal/vite-config/src/plugins/index.ts

@@ -5,6 +5,7 @@ import purgeIcons from 'vite-plugin-purge-icons';
 
 import { createAppConfigPlugin } from './appConfig';
 import { configCompressPlugin } from './compress';
+import { configHmrPlugin } from './hmr';
 import { configHtmlPlugin } from './html';
 import { configMockPlugin } from './mock';
 import { configSvgIconsPlugin } from './svgSprite';
@@ -53,6 +54,10 @@ async function createPlugins({ isBuild, root, enableMock, compress, enableAnalyz
     vitePlugins.push(configMockPlugin({ isBuild }));
   }
 
+  if (!isBuild) {
+    vitePlugins.push(configHmrPlugin());
+  }
+
   return vitePlugins;
 }