From 3d424f7c0b64354616958d9d2625d75f51d2be40 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Sun, 14 Jun 2026 21:44:18 +0700 Subject: [PATCH] perf(build): split CodeMirror core chunk to clear 500 kB warning (#74) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrate vite.config.ts chunking from the deprecated manualChunks function form to advancedChunks.groups. Rolldown's optimizer silently re-merged manualChunks sub-chunks, so a cm-core split was ignored. Split the ~614 kB cm-core into cm-view (~245 kB, the @codemirror/view engine) + cm-core (~362 kB, state/language/lezer/@uiw); cm-langs is unchanged (~60 kB). No chunk now exceeds Vite's 500 kB warning limit. Same total/gzip bytes — the editor still loads all three behind its existing React.lazy boundary, so this is finer file granularity for better cross-deploy caching. No converter or UI behavior changed. Verified: clean build (no warning), 209 tests pass, production preview renders/highlights/converts with all cm-* chunks 200 OK, no console errors. Co-authored-by: Claude Opus 4.8 (1M context) --- CHANGELOG.md | 4 ++- vite.config.ts | 85 ++++++++++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e00c03..379726b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and ## [Unreleased] -No unreleased changes at this time. Future work tracked in `docs/ROADMAP.md` under "Beyond". +### Changed + +- **Split the CodeMirror core chunk to clear the 500 kB build warning.** The `cm-core` chunk was ~614 kB raw (210 kB gzip) and tripped Vite's `chunkSizeWarningLimit` on every build, even though it is already lazy-loaded behind the editor. Vite 8 / Rolldown deprecates the `manualChunks` function form (its optimizer silently merged sub-chunks back together), so `vite.config.ts` now uses `advancedChunks.groups` with explicit `priority`-ordered, honored boundaries. The CodeMirror core splits into `cm-view` (~245 kB, the `@codemirror/view` DOM engine) + `cm-core` (~362 kB, state/language/lezer/`@uiw` wrapper); `cm-langs` is unchanged (~60 kB). No chunk now exceeds 500 kB. Total bytes and gzip size are unchanged — the editor still loads all three pieces together behind its existing `React.lazy` boundary, so this is purely finer file granularity (better cache reuse across deploys). No converter or UI behavior changed; verified in a production preview build (editor renders, highlights, and converts; all `cm-*` chunks 200 OK, no console errors). ## [1.6.0] — 2026-06-14 diff --git a/vite.config.ts b/vite.config.ts index c405e0e..388123d 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -59,43 +59,54 @@ export default defineConfig(({ mode }) => ({ minify: 'oxc', rollupOptions: { output: { - // Vite 8 / Rolldown only accepts the function form here. Split into the - // smallest sensible vendor chunks so a stale deploy only re-downloads - // the pieces that actually changed, and so heavy parsers/lang-packs are - // their own files (loaded lazily once their format/editor is needed). - // Clause order matters: react must win before react-router/react-i18next. - manualChunks(id) { - if (!id.includes('node_modules')) return undefined; - // React core (react, react-dom, scheduler) — exclude react-router / - // react-i18next / @uiw/react-codemirror which match their own clauses. - if (id.includes('/react-dom/') || /\/react\//.test(id) || id.includes('/scheduler/')) { - return 'react'; - } - if (id.includes('react-router')) return 'router'; - if (id.includes('i18next')) return 'i18n'; - // CodeMirror: language packs split from the core editor. - if (id.includes('@codemirror/lang-') || id.includes('@codemirror/legacy-modes')) { - return 'cm-langs'; - } - if ( - id.includes('@codemirror/') || - id.includes('@uiw/react-codemirror') || - id.includes('@lezer/') - ) { - return 'cm-core'; - } - // One chunk per parser library. - if (id.includes('js-yaml')) return 'p-yaml'; - if (id.includes('papaparse')) return 'p-csv'; - if (id.includes('fast-xml-parser')) return 'p-xml'; - if (id.includes('smol-toml')) return 'p-toml'; - if (id.includes('node_modules/bson/')) return 'p-bson'; - if (id.includes('node_modules/cbor-x/')) return 'p-cbor'; - if (id.includes('node_modules/@msgpack/')) return 'p-msgpack'; - if (id.includes('fflate')) return 'fflate'; - // Radix primitives + icon set. - if (id.includes('@radix-ui') || id.includes('lucide-react')) return 'ui'; - return undefined; + // Rolldown deprecates the `manualChunks` function form — its optimizer + // silently merges small manual chunks back together (it folded a + // `@codemirror/view` split straight back into `cm-core`). + // `advancedChunks.groups` are explicit, honored boundaries; higher + // `priority` wins when a module matches several groups. + // + // CodeMirror gotcha: a group captures its matched modules AND their deps + // recursively, so the leaf groups (`cm-kit`, `cm-langs`) would otherwise + // drag the shared core (view/state/language) in with them — that ballooned + // `cm-langs` to ~600 kB. Fix: give `cm-core` the highest cm priority so it + // claims the shared modules first, scope each cm test so it can't match + // another's packages, and put a `maxSize` on `cm-core` so Rolldown peels + // the heavy `@codemirror/view` engine into its own file. Net: every chunk + // stays under the 500 kB warning, the pieces still load together behind + // the editor's lazy boundary (same total bytes), and a stale deploy + // re-downloads only the part that moved. + advancedChunks: { + groups: [ + // React core (react, react-dom, scheduler) — `[\\/]react[\\/]` + // excludes react-router / react-i18next / @uiw/react-codemirror. + { name: 'react', priority: 100, test: /[\\/](?:react-dom|react|scheduler)[\\/]/ }, + { name: 'router', priority: 95, test: /react-router/ }, + { name: 'i18n', priority: 95, test: /i18next/ }, + // `@codemirror/view` is the ~370 kB DOM/render engine — the single + // heaviest module and the reason the old `cm-core` tripped 500 kB. + // Pin it (highest cm priority) so nothing drags it elsewhere. + { name: 'cm-view', priority: 93, test: /@codemirror[\\/]view[\\/]/ }, + // The rest of the core: state + language + lezer + the @uiw wrapper + // (whose recursive deps also pull in autocomplete/commands/search/lint). + // Outranks cm-langs so the lang packs' dep capture can't steal it. + { + name: 'cm-core', + priority: 92, + test: /@codemirror[\\/](?:state|language|theme-|autocomplete|commands|search|lint)|@lezer[\\/]|@uiw[\\/]react-codemirror/, + }, + { name: 'cm-langs', priority: 90, test: /@codemirror[\\/](?:lang-|legacy-modes)/ }, + // One chunk per parser library. + { name: 'p-yaml', priority: 70, test: /js-yaml/ }, + { name: 'p-csv', priority: 70, test: /papaparse/ }, + { name: 'p-xml', priority: 70, test: /fast-xml-parser/ }, + { name: 'p-toml', priority: 70, test: /smol-toml/ }, + { name: 'p-bson', priority: 70, test: /node_modules[\\/]bson[\\/]/ }, + { name: 'p-cbor', priority: 70, test: /node_modules[\\/]cbor-x[\\/]/ }, + { name: 'p-msgpack', priority: 70, test: /node_modules[\\/]@msgpack[\\/]/ }, + { name: 'fflate', priority: 70, test: /fflate/ }, + // Radix primitives + icon set. + { name: 'ui', priority: 60, test: /(?:@radix-ui|lucide-react)/ }, + ], }, }, },