@ogygia/tsdown-plugin-svelte
Options
Every option of svelte(), with its default. Where the components are and where they go, the preprocessors that turn their script into JavaScript, the types and the tsconfig they're made with, and setting root in a monorepo.
On this page
svelte() takes one options object, and every key is optional. With none, it reads components from src in the folder tsdown runs in, writes them to dist, preprocesses them with vitePreprocess, and generates their types with your tsconfig.json.
| Option | Default | What it does |
|---|---|---|
root | process.cwd() | The package root that src, out and tsconfig are relative to. |
src | 'src' | Where your components and .css files live. Every .svelte and .css file under it ships. |
out | 'dist' | Where they’re written. Match tsdown’s outDir. |
preprocess | vitePreprocess({ script: true }) | Svelte preprocessors, one or a list, run on each component before its template is stripped. |
dts | true | Write a .svelte.d.ts beside each component. Needs svelte2tsx and typescript. |
tsconfig | 'tsconfig.json' | The tsconfig the component types are generated with. |
import { defineConfig } from 'tsdown';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { svelte } from '@ogygia/tsdown-plugin-svelte';
export default defineConfig({
entry: ['src/**/*.ts'],
unbundle: true,
fixedExtension: false,
dts: true,
plugins: [
svelte({
root: process.cwd(),
src: 'src',
out: 'dist',
preprocess: vitePreprocess({ script: true }),
dts: true,
tsconfig: 'tsconfig.json'
})
],
deps: { neverBundle: ['svelte', /\.svelte$/] }
});Preprocessors
The default is vitePreprocess({ script: true }) from @sveltejs/vite-plugin-svelte, which turns <script lang="ts"> into JavaScript and handles <style lang="…"> for the preprocessors you have installed. Pass your own to add others, such as a CSS preprocessor, or to drop the @sveltejs/vite-plugin-svelte dependency. Without it installed and without preprocess, the build stops with an error that says to pass preprocess or install @sveltejs/vite-plugin-svelte.
Whatever you pass must turn the script’s TypeScript into JavaScript. The plugin removes TypeScript only from the template, and removes lang="ts" from each script, so a script left as TypeScript would ship as broken JavaScript. preprocess: [] is only right for components whose scripts are JavaScript already.
Types
After tsdown writes the bundle, svelte2tsx generates every component’s declarations, and the plugin copies the .svelte.d.ts files next to the components. Your .ts declarations are still tsdown’s, built with your config as it is.
The component types are generated with tsconfig extended with isolatedDeclarations turned off. svelte2tsx’s component code never carries the annotations that option demands, so a library that turns it on for its .ts files would otherwise get no component types. When the file doesn’t exist, the plugin passes no tsconfig, and svelte2tsx looks for the nearest tsconfig.json or jsconfig.json from src upward. It uses what it finds as it is, without the isolatedDeclarations override, and the build fails with Failed to locate tsconfig or jsconfig when it finds none. So a package needs a tsconfig unless it sets dts: false, and a tsconfig option should name a file that exists (#2314, item 15).
dts: false skips all of it, and then neither svelte2tsx nor typescript needs to be installed.
Monorepos
root, and the paths under it, default to the folder tsdown runs in. When tsdown runs from another folder, such as a workspace root or a shared build script, set root to the package’s own folder:
import { fileURLToPath } from 'node:url';
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({
root: fileURLToPath(
new URL('.', import.meta.url)
)
})
],
deps: { neverBundle: ['svelte', /\.svelte$/] }
});