Skip to content

perf: split vendor chunks and load the editor on demand - #387

Open
sunxiaobin89 wants to merge 1 commit into
GOODBOY008:mainfrom
sunxiaobin89:perf/code-splitting
Open

sunxiaobin89 wants to merge 1 commit into
GOODBOY008:mainfrom
sunxiaobin89:perf/code-splitting

Conversation

@sunxiaobin89

@sunxiaobin89 sunxiaobin89 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

概要

生产构建原本只有一个 3.45 MB 的入口 chunk,其中唯一的动态导入还是无效的。

  • vite.config.ts — 将 @xterm、图表库(recharts/d3)、CodeMirror 和 src/locales 拆成命名 chunk。只对无环的叶子依赖分组。最初用 catch-all vendor 组 + maxSize 的尝试把 sonner 和 cmdk 分到不同 chunk,导致生产应用在首屏前崩溃(Uncaught TypeError: t is not a function)。该问题通过把构建产物加载到浏览器中复现后,收紧了配置,直到模块图能正常初始化。
  • src/main.tsx — 独立文件查看窗口改为按需 import() FileViewerWindow。
  • terminal-tab-portals.tsx — FileEditorView 改为 lazy() + Suspense,1.1 MB 的 CodeMirror chunk 只在首次打开文件时下载,而不是启动时。
  • App.tsx — getCurrentWindow 改为静态导入。菜单栏本来就静态导入了同一个模块,所以原来的动态导入产生 INEFFECTIVE_DYNAMIC_IMPORT 且没有拆分任何东西。

结果:入口 3.45 MB → 1.61 MB;1.11 MB 的编辑器 chunk 已不再出现在预加载列表中。

测试计划

  • pnpm exec tsc --noEmit → 通过
  • pnpm test → 2181 passed / 16 skipped(编辑器标签页测试覆盖了新的 Suspense 边界)
  • pnpm build → 成功;对构建产物做浏览器加载探测,无模块初始化错误
  • 真实应用:pnpm tauri build --debug --bundles app 后启动,SSH 连接 localhost → 主窗口正常渲染并连接;双击 ~/rshell-split-check.txt → 编辑器标签页打开并显示文件内容,证明动态导入的 chunk 在 Tauri 资源协议下可正常加载

说明

  • index(1.61 MB)和按需加载的 vendor-editor(1.11 MB)仍高于 Vite 的 500 kB 建议值。要再降低入口体积,需要把更多常驻界面(设置、对话框)改为懒挂载,本 PR 有意不做。
  • 无 Rust 改动。

The production bundle was a single 3.45 MB entry chunk. The ineffective
import('@tauri-apps/api/window') in App.tsx was the only dynamic import
and split nothing, because the menu bar already imports that module
statically.

- vite: split xterm, charts, CodeMirror and the locale JSON into named
  chunks. Only self-contained leaf packages are grouped: an earlier
  catch-all vendor group created a sonner<->cmdk cycle across chunks and
  the production app died with "t is not a function" before first paint.
- main.tsx: the detached file viewer loads FileViewerWindow lazily.
- terminal-tab-portals: lazy-load FileEditorView behind Suspense so the
  ~1.1 MB CodeMirror chunk only downloads when a file is opened.
- App.tsx: import getCurrentWindow statically instead of dynamically.

Result: entry 3.45 MB -> 1.61 MB, and the 1.11 MB editor chunk is no
longer preloaded. Verified with 2181 frontend tests, tsc, and a real
tauri build --debug: the main window renders, the SSH session connects,
and double-clicking a file opens the editor tab with its content.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant