Skip to content

@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.tsindex.js, index.d.tstsdown
Button.svelteButton.svelte, with plain JS, uncompiledthis plugin
Button.svelte.d.ts, its typed propsthis plugin
theme.csstheme.css, copiedthis 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 .svelte file lands in dist at 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.ts is generated beside every component with svelte2tsx, the engine svelte-package uses.
  • 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.ts import becomes ./util.js, the built module beside it.
  • Styles come along. Plain .css files under src are 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-svelte
pnpm add -D svelte2tsx typescript @sveltejs/vite-plugin-svelte

First config

List svelte() in tsdown’s plugins:

  • unbundle: true keeps one output file per source file, so import Button from './Button.svelte' in your .ts still points at the shipped component, and a component’s ./util.ts import finds ./util.js.
  • fixedExtension: false makes tsdown write .js in a "type": "module" package. Without it, recent tsdown versions (0.23 does) write .mjs, while the plugin points a component’s ./util.ts import at ./util.js, a file that isn’t there. See Limits.
  • deps.neverBundle keeps 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$/] }
});

package.json exports

Expose the components to Svelte-aware tools with the svelte export condition:

{
  "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

  • .mjs output. The plugin always rewrites a component’s relative ./x.ts imports 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. Set fixedExtension: false in a "type": "module" package, as above, or outExtensions: () => ({ js: '.js', dts: '.d.ts' }) (#2314, item 1).
  • Rune modules. A .svelte.ts module such as counter.svelte.ts is TypeScript, so tsdown builds it, with its runes left as they are, to counter.svelte.js. List it in entry (the src/**/*.ts glob does). The plugin doesn’t touch it.
  • Every component under src. The plugin ships every .svelte and .css file under src, test fixtures included. Keep files that shouldn’t ship outside src, or point src at a narrower folder.
  • A tsconfig. With types on, the package needs a tsconfig.json (or a jsconfig.json) in src or a folder above it, normally at root. Without one the build fails with Failed 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 .svelte files into an app, use @sveltejs/vite-plugin-svelte or rollup-plugin-svelte.

Package contents

PageWhat it covers
OptionsEvery option of svelte() with its default, preprocessors, types, and monorepos.
RolldownThe same plugin in a plain rolldown build.
How it worksThe four steps of a build, and the files it writes and removes.
Markup strippingstrip_markup_ts and js_specifiers, for tools that ship Svelte source another way.
APIThe generated reference.

Moved sections

These sections of this page moved to new addresses: