When distributing Svelte component libraries built with tsdown, follow these best practices:
Recommended: Ship .svelte source files
Instead of shipping precompiled JS, ship the original .svelte files. This allows consumers to use their own tooling (like Vite + @sveltejs/vite-plugin-svelte) to compile the components. This approach:
- Avoids version compatibility issues with
svelte/internal. - Ensures better SSR/hydration consistency.
- Provides consumers with better HMR, diagnostics, and tree-shaking.
- Reduces the need for republishing when Svelte is upgraded.
Exceptions
Shipping precompiled JS is only recommended if you are:
- Building Web Components via
customElement mode. - Providing a CDN direct-load option that requires no build step.
Implementation Details
- Mark
svelte and svelte/* as external. - Declare
svelte in your package's peerDependencies. - Use
svelte2tsx to emit .d.ts files for your Svelte components.