Skip to content

@ogygia/tsdown-plugin-svelte

Rolldown

The plugin uses only standard build hooks, so it also works in a plain rolldown build. Set up one output file per source file and the .ts declarations yourself, which tsdown would otherwise give it.

On this page

tsdown runs on rolldown, and the plugin uses only two standard build hooks, buildStart and writeBundle, and this.emitFile, so it also works in a plain rolldown build. This setup was tested with rolldown 1.2.16 and rolldown-plugin-dts 0.28. You set up the two things tsdown would otherwise give it:

  • One output file per source file, so a component’s ./util.ts import finds ./util.js beside it: preserveModules with preserveModulesRoot: 'src', and entryFileNames: '[name].js'.
  • Types for your .ts files: rolldown-plugin-dts, the plugin tsdown uses. This plugin only adds the .svelte.d.ts files.
import { defineConfig } from 'rolldown';
import { dts } from 'rolldown-plugin-dts';
import { svelte } from '@ogygia/tsdown-plugin-svelte';

export default defineConfig({
  input: ['src/index.ts', 'src/util.ts'],
  external: ['svelte', /\.svelte$/],
  plugins: [dts(), svelte()],
  output: {
    dir: 'dist',
    format: 'esm',
    preserveModules: true,
    preserveModulesRoot: 'src',
    entryFileNames: '[name].js'
  }
});

Inputs

List every .ts file a component imports in input, so each one is built to its own file. A file only a component imports is otherwise never built, since rolldown doesn’t read the components: they’re assets to it. A .svelte.ts rune module goes in input too.

Output folder

Set the plugin’s out to output.dir when that isn’t dist. Unlike tsdown, rolldown doesn’t empty the folder first, so stale files from an earlier build stay unless you remove them.

import { defineConfig } from 'rolldown';
import { dts } from 'rolldown-plugin-dts';
import { svelte } from '@ogygia/tsdown-plugin-svelte';

export default defineConfig({
  input: ['src/index.ts'],
  external: ['svelte', /\.svelte$/],
  plugins: [dts(), svelte({ out: 'build' })],
  output: {
    dir: 'build',
    format: 'esm',
    preserveModules: true,
    preserveModulesRoot: 'src',
    entryFileNames: '[name].js'
  }
});