tsdown Documentation

repository·main·Indexed 24 days ago

https://github.com/rolldown/tsdown

A high-performance bundler for libraries powered by Rolldown and Oxc. It features fast builds, declaration file generation, and compatibility with Rollup, unplugin, and select Vite plugins. The ecosystem includes create-tsdown for scaffolding, @tsdown/css for Lightning CSS-powered processing, @tsdown/exe for cross-platform executables, and tsdown-migrate for automating transitions from tsup.

Tokens
77.6K
Snippets
333
Records
530
Agent score
88%

What's inside tsdown

  1. Overview of tsdown features

    main

    tsdown is a high-performance bundler for libraries powered by Rolldown and Oxc. Key features include:

    • Blazing fast: High-speed builds and declaration file generation.
    • Powerful ecosystem: Supports Rollup, Rolldown, unplugin plugins, and select Vite plugins.
    • Easy to use: Preconfigured for immediate use.
    • Seamless migration: Compatible with tsup options and features for easy transitions.
  2. Overview of tsdown

    main

    tsdown is an elegant library bundler designed specifically for TypeScript and JavaScript library authors. Built on top of the Rust-based Rolldown engine, it provides a complete out-of-the-box solution that simplifies the bundling process compared to general-purpose bundlers.

    Key capabilities include:

    • TypeScript & JavaScript: Seamless bundling of .ts and .js files.
    • Automatic Declarations: Automatic generation of .d.ts files.
    • Multiple Formats: Support for esm, cjs, iife, and umd output formats.
    • Asset Support: Processing of .json and .wasm files.
    • Optimizations: Built-in support for tree shaking, minification, and source maps.
  3. Core features of tsdown

    main

    tsdown provides several automated features tailored for library development:

    • Simplified Configuration: Sensible defaults with minimal boilerplate.
    • Automatic Externalization: Automatically treats dependencies, peerDependencies, and optionalDependencies from your package.json as external.
    • TypeScript Support: Auto-generates .d.ts declaration files.
    • Output Formats: Supports esm, cjs, iife, and umd.
    • Asset Support: Bundles .json, .wasm, and CSS files.
    • Package Integration: Generates the exports field for package.json.
    • Advanced Bundling: Includes tree shaking, minification, source maps, and a CSS preprocessing pipeline.
    • Executable Bundling: Supports Single Executable Applications (SEA).
  4. Understand tsdown smart defaults

    main

    tsdown automatically infers configuration from your package.json and tsconfig.json. Key automatic behaviors include:

    • Dependencies: dependencies, peerDependencies, and optionalDependencies are externalized (not bundled). devDependencies are bundled if imported.
    • Type Declarations: If types or typings is present in package.json, .d.ts generation is enabled. If isolatedDeclarations is in tsconfig.json, it uses the fast oxc-transform path.
    • Module System: If type: "module" is in package.json, it uses .js extensions for ESM output (unless fixedExtension is disabled). If platform: "node" (default), it enables fixedExtension (.mjs/.cjs).
    • Entry Point: If no entry is specified, it defaults to src/index.ts.
    • Target: Compilation target is inferred from engines.node in package.json.
    • Package Exports: If exports: true is set, it automatically generates the exports field in package.json.
  5. Features of @tsdown/css

    main

    The @tsdown/css package, powered by Lightning CSS, provides the following capabilities for tsdown users:

    • CSS extraction and bundling: Automatically handles CSS files during the build process.
    • CSS @import inlining: Uses Lightning CSS bundleAsync to inline imports.
    • Syntax lowering and autoprefixing: Ensures compatibility across different browsers.
    • Minification: Reduces CSS file size for production.
    • Code splitting: Optimizes CSS delivery.
    • Source map support: Enables easier debugging of CSS.
    • Preprocessor support: Handles Sass, Less, and Stylus.
    • PostCSS integration: Allows for extensible CSS transformations.
  6. Explore new features in tsdown

    main

    Compared to tsup, tsdown provides several new capabilities:

    • nodeProtocol: Control Node.js built-in module imports (true adds node: prefix, 'strip' removes it, false keeps as-is).
    • workspace: Build multiple packages in a monorepo (e.g., workspace: 'packages/*').
    • exports: Auto-generate the exports field in package.json via exports: true.
    • publint / attw: Validate packages for common issues.
    • exe: Bundle as a Node.js standalone executable via exe: true.
    • devtools: Enable Vite DevTools for bundle analysis via devtools: true.
    • hooks: Lifecycle hooks: build:prepare, build:before, and build:done.
    • css: Full CSS pipeline (preprocessors, Lightning CSS, PostCSS, etc.).
    • globImport: Support for import.meta.glob (Vite-style).
  7. Understand default dependency behavior in tsdown

    main

    By default, tsdown handles dependencies based on their classification in package.json:

    • dependencies, peerDependencies, and optionalDependencies: These are treated as external and are not bundled. They are expected to be provided by the consumer's environment.
    • devDependencies and Phantom Dependencies (packages in node_modules not in package.json): These are bundled only if they are actually imported or required by your source code.
  8. Distribution strategy for Svelte libraries

    main

    When distributing Svelte component libraries built with tsdown, follow these best practices:

    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.
  9. Optimize dts generation performance

    main

    The speed of .d.ts generation depends on your tsconfig.json settings:

    • High Performance: Enable isolatedDeclarations: true in your tsconfig.json. This allows tsdown to use oxc-transform, which is extremely fast.
    • Standard Performance: If isolatedDeclarations is not enabled, tsdown falls back to the standard TypeScript compiler, which is slower.
    // Recommended for speed
    {
      "compilerOptions": {
        "isolatedDeclarations": true
      }
    }