@ogygia/tsdown-plugin-svelte
tsdown plugin
A tsdown plugin that ships a library's Svelte components the way svelte-package does, inside the same build as its TypeScript. Components ship uncompiled, with TypeScript removed from the script and the template, a .svelte.d.ts beside each, and plain CSS copied.
On this page
A Svelte component library usually has two builds: tsdown or another bundler for its TypeScript, and svelte-package for its components. @ogygia/tsdown-plugin-svelte folds the second into the first. Your .ts goes through tsdown as usual, and your .svelte components ship beside it in the same run:
In src/ | In dist/ | By |
|---|---|---|
index.ts | index.js, index.d.ts | tsdown |
Button.svelte | Button.svelte, with plain JS, uncompiled | this plugin |
Button.svelte.d.ts, its typed props | this plugin | |
theme.css | theme.css, copied | this plugin |
It works for any Svelte library, not only ones used in ogygia apps: nothing ogygia-specific ends up in what you ship. ogygia’s own packages are built with it.
- Components ship uncompiled. Each
.sveltefile lands indistat the same path, as source. Your users’ own Svelte compiles it, so your library never pins them to the Svelte version you built with. - TypeScript everywhere, plain JS shipped. TypeScript is removed from the
<script>and from the template, so{item as Row},{user!.name}and{#snippet row(item: Row)}are all fine. Your users need no TypeScript setup. - Typed props. A
.svelte.d.tsis generated beside every component with svelte2tsx, the enginesvelte-packageuses. - Careful stripping. Types are found with Svelte’s own parser, not with patterns, so a string or a comment that only looks like a type is left alone.
- Imports that resolve. A component’s
./util.tsimport becomes./util.js, the built module beside it. - Styles come along. Plain
.cssfiles undersrcare copied as they are.
Installation
It needs tsdown 0.15 or newer and svelte 5.40 or newer. For the defaults, also install:
svelte2tsx and typescript generate the .svelte.d.ts types, with your package’s tsconfig.json, which the build then needs. svelte2tsx needs the classic TypeScript compiler, typescript 5 or 6, not the native port. Skip all of this with dts: false. @sveltejs/vite-plugin-svelte provides the default preprocessor; skip it by passing your own preprocess (Options).
pnpm add -D @ogygia/tsdown-plugin-sveltepnpm add -D svelte2tsx typescript @sveltejs/vite-plugin-svelteFirst config
List svelte() in tsdown’s plugins:
unbundle: truekeeps one output file per source file, soimport Button from './Button.svelte'in your.tsstill points at the shipped component, and a component’s./util.tsimport finds./util.js.fixedExtension: falsemakes tsdown write.jsin a"type": "module"package. Without it, recent tsdown versions (0.23 does) write.mjs, while the plugin points a component’s./util.tsimport at./util.js, a file that isn’t there. See Limits.deps.neverBundlekeeps the shipped components, and Svelte, out of your JavaScript.
The examples on these pages use tsdown 0.23’s option names.
import { defineConfig } from 'tsdown';
import { svelte } from '@ogygia/tsdown-plugin-svelte';
export default defineConfig({
entry: ['src/**/*.ts'],
unbundle: true,
fixedExtension: false,
dts: true,
plugins: [svelte()],
deps: { neverBundle: ['svelte', /\.svelte$/] }
});{
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js",
"default": "./dist/index.js"
},
"./Button.svelte": {
"types": "./dist/Button.svelte.d.ts",
"svelte": "./dist/Button.svelte"
}
},
"files": ["dist"],
"peerDependencies": { "svelte": "^5.0.0" }
}How it works
The plugin does its work in two build hooks, so tsdown stays in charge of your .ts files and their declarations. At buildStart it preprocesses each component, removes the TypeScript left in its template, and emits it as a build asset, with each .css file beside it. At writeBundle it generates the components’ types with svelte2tsx and copies only the .svelte.d.ts files next to them. How it works goes step by step.
Integrations
A component library built this way is already in the shape an ogygia app needs. The app imports my-lib/Button.svelte with its own wake mark, and its build turns the component into a region; your package ships nothing for that. Packaging covers the rest, including ogygia.files for marks that belong on your side.
Limits
.mjsoutput. The plugin always rewrites a component’s relative./x.tsimports to./x.js. When tsdown writes.mjs, its default on recent versions, those imports point at files that don’t exist, and the build doesn’t say so. SetfixedExtension: falsein a"type": "module"package, as above, oroutExtensions: () => ({ js: '.js', dts: '.d.ts' })(#2314, item 1).- Rune modules. A
.svelte.tsmodule such ascounter.svelte.tsis TypeScript, so tsdown builds it, with its runes left as they are, tocounter.svelte.js. List it inentry(thesrc/**/*.tsglob does). The plugin doesn’t touch it. - Every component under
src. The plugin ships every.svelteand.cssfile undersrc, test fixtures included. Keep files that shouldn’t ship outsidesrc, or pointsrcat a narrower folder. - A tsconfig. With types on, the package needs a
tsconfig.json(or ajsconfig.json) insrcor a folder above it, normally atroot. Without one the build fails withFailed to locate tsconfig or jsconfig(Types, #2314, item 15). - No compiled output. It ships components as source for your users’ Svelte to compile. To compile
.sveltefiles into an app, use@sveltejs/vite-plugin-svelteorrollup-plugin-svelte.
Package contents
| Page | What it covers |
|---|---|
| Options | Every option of svelte() with its default, preprocessors, types, and monorepos. |
| Rolldown | The same plugin in a plain rolldown build. |
| How it works | The four steps of a build, and the files it writes and removes. |
| Markup stripping | strip_markup_ts and js_specifiers, for tools that ship Svelte source another way. |
| API | The generated reference. |
Moved sections
These sections of this page moved to new addresses:
- Next steps, now Package contents
- ogygia, now Integrations