From c329c4ad86fb839a3c6799df7f3c0682ee924c7c Mon Sep 17 00:00:00 2001 From: tyanxie <494966054@qq.com> Date: Sun, 9 Aug 2026 12:10:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(web):=20=E4=BD=BF=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=BC=8F=20manualChunks=20=E4=BC=98=E5=8C=96=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E5=88=86=E5=8C=85=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 从枚举式改为函数式 manualChunks,按模块路径匹配避免遗漏深层依赖 - 拆分为 4 个 chunk:framework / markdown / vendor / app(业务代码) - 修复 parse5、js-yaml 等 markdown 子依赖未纳入 markdown chunk 的问题 - 业务代码 chunk 从 635KB 降至 101KB,提升增量更新时的缓存效率 - 调高 chunkSizeWarningLimit 至 600 消除 markdown 生态的体积警告 --- vite.config.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 1f15587..c8773e1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -15,15 +15,28 @@ export default defineConfig({ build: { outDir: "../../dist/web", emptyOutDir: true, + chunkSizeWarningLimit: 600, rollupOptions: { output: { - manualChunks: { - markdown: [ - "react-markdown", - "remark-gfm", - "rehype-highlight", - "highlight.js", - ], + manualChunks(id) { + if (!id.includes("node_modules")) return; + + // React 核心:极少更新,长期缓存 + if (/[\/](?:react-dom|react|scheduler)[\/]/.test(id)) { + return "framework"; + } + + // Markdown 渲染生态:体积大、独立功能 + if ( + /[\/](?:react-markdown|remark-[\w-]+|rehype-[\w-]+|highlight\.js|lowlight|parse5|unified|micromark[\w-]*|mdast[\w-]*|hast[\w-]*|vfile[\w-]*|unist[\w-]*|js-yaml|property-information|get-east-asian-width|markdown-table|comma-separated-tokens|space-separated-tokens|longest-streak|ccount|escape-string-regexp|trim-lines|decode-named-character-reference|html-void-elements|html-url-attributes|web-namespaces|inline-style-parser|style-to-object|style-to-js|is-plain-obj|bail|trough|zwitch|fault|extend|format)[\/]/.test( + id, + ) + ) { + return "markdown"; + } + + // 其余第三方依赖:偶尔更新 + return "vendor"; }, }, },