Hey guys, I’m building an app in Tauri, and using SvelteKit on the front-end (Svelte 5).
The Stack
- Framework: Tauri 2.0
- Frontend: SvelteKit (
adapter-static
,ssr = false
,prerender = true
) - Bundler: Vite
- Editor: CodeMirror 6
- Package Manager:
pnpm
The Problem
My application works perfectly in development mode (pnpm tauri dev
). The CodeMirror editor renders correctly, and all plugins (like autocomplete) function as expected.
However, when I create a production build (pnpm tauri build
), the editor breaks in two ways:
- Styling Issue: The core layout is broken. The gutter and line numbers are stacked vertically at the top of the editor, and other default CM styles are not present.
- Functionality Issue: Plugins like autocomplete do not work at all.
This clearly points to an issue with Vite/Rollup’s production bundling and tree-shaking process.
What I’ve Tried
I have spent many hours trying to resolve this and have gone through every conceivable configuration and implementation strategy, including:
- Vite Configurations:
optimizeDeps.include
andoptimizeDeps.exclude
with all@codemirror
packages.ssr.noExternal
to force bundling of the packages.build.rollupOptions.treeshake.moduleSideEffects
to explicitly tell Rollup not to tree-shake CodeMirror’s side effects.
- Implementation Changes:
- Started with
svelte-codemirror-editor
and then removed it to use the native CodeMirror 6 API directly. The problem persisted. - Tried dynamically importing the autocomplete plugin (
await import(...)
) to prevent it from being tree-shaken. The build produced a warning that the module was also statically imported by other CodeMirror packages, neutralizing the fix.
- Started with
- Dependency Management:
- Downgraded and pinned all
@codemirror
packages to older, known-stable versions from late 2023/early 2024. The issue remained.
- Downgraded and pinned all
My Conclusion
The issue seems to be that Vite/Rollup’s tree-shaking is incorrectly removing the “side effects” from the @codemirror
packages - both the dynamically injected CSS for layout and the JavaScript logic for plugins. None of the standard Vite configurations to control this behavior have worked.
My final attempt at a workaround involved dynamically importing the plugins, but this also failed.
I’m at a completely loss. Any help would be greatly appreciated Thanks