Vite uses pre-bundling to optimize development performance. However, combining plugins or preprocessors that rewrite imports with pre-bundling can cause issues because pre-bundling scans files on disk and cannot detect dynamically added/changed imports.
Exclude a library from pre-bundling
If you use tools that rewrite imports, exclude those libraries using optimizeDeps.exclude in vite.config.js:
// vite.config.js
export default defineConfig({
optimizeDeps: {
exclude: ['some-library']
}
});
Disable pre-bundling for all Svelte libraries
You can disable this behavior globally for Svelte libraries in svelte.config.js:
// svelte.config.js
export default {
vitePlugin: {
prebundleSvelteLibraries: false
}
};
Import Strategies
- Index imports (
import { X } from 'lib'): Better DX and intellisense, but slower builds as the whole library is compiled. - Deep imports (
import X from 'lib/src/X.svelte'): Faster builds and snappier dev, but requires more explicit imports and can increase initial load time if overused.