Bläddra i källkod

fix: vite manualChunks 固定 navigation store/CustomTabBar 为共享 chunk

根因:不同 Node 环境(本地 v22 / 服务器 v20)下 rollup 自动 chunk 拆分
结果不稳定。服务器构建把 navigation store、CustomTabBar 内联进了单个
页面 chunk(pages-mine-index),导致首页等其它页面 chunk 拿不到,
表现为底部 tab 栏消失、用户中心菜单为空。

解决:用 manualChunks 把这两个跨页面共享模块固定到命名 chunk app-core,
所有引用页面都会显式 import 它,保证全局单例与组件在任何构建环境可用。

Co-Authored-By: Claude <noreply@anthropic.com>
MyFramework User 2 månader sedan
förälder
incheckning
09f9df5282
1 ändrade filer med 21 tillägg och 0 borttagningar
  1. 21 0
      my-uniapp-vue3/vite.config.ts

+ 21 - 0
my-uniapp-vue3/vite.config.ts

@@ -4,6 +4,27 @@ import uni from "@dcloudio/vite-plugin-uni";
 // https://vitejs.dev/config/
 export default defineConfig({
   plugins: [uni()],
+  build: {
+    rollupOptions: {
+      output: {
+        // 把跨页面共享的核心模块(导航 store / 全局组件)固定到一个共享 chunk。
+        // 原因:不同 Node 环境下 rollup 的自动 chunk 拆分结果不稳定,曾出现
+        //   navigation store / CustomTabBar 被内联进单个页面 chunk,导致其它
+        //   页面(如首页)拿不到底部 tab 栏、用户中心菜单为空。
+        // 固定成命名 chunk 后,所有引用它的页面 chunk 都会显式 import 它,保证
+        //   全局单例与组件在任何构建环境下都可用。
+        manualChunks(id) {
+          if (id.includes('/src/store/navigation') || id.includes('\\src\\store\\navigation')) {
+            return 'app-core';
+          }
+          if (id.includes('/src/components/CustomTabBar') || id.includes('\\src\\components\\CustomTabBar')) {
+            return 'app-core';
+          }
+          return undefined;
+        },
+      },
+    },
+  },
   server: {
     host: '0.0.0.0',
     port: 5173,