mcpField
T
tsdown

tsdown

github.com/antfu開発6.0k14.0k 回インストール6 日前に更新
リポジトリ

tsdown について

tsdownを使うTypeScript・JavaScriptライブラリ開発者向けのドキュメントスキルです。複数のモジュール形式、型宣言、依存関係の処理、tsupからの移行を含むビルド設定を扱います。

主な機能

  1. 01複数の出力形式でビルド
  2. 02TypeScriptの型宣言を生成
  3. 03依存関係のバンドルと外部化を設定
  4. 04ツリーシェイキングと圧縮
  5. 05tsupからの移行ガイド

活用シーン

  1. 01型宣言付きのnpmライブラリをビルド
  2. 02異なるプラットフォーム向けのビルド対象を設定
  3. 03公開前にCIでパッケージを検証

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/antfu/skills --skill tsdown

インストール後、Claude Code で/tsdownを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

LICENSE.md
README.md
references/
advanced-benchmark.md
advanced-ci.md
advanced-hooks.md
advanced-plugins.md
advanced-programmatic.md
advanced-rolldown-options.md
guide-getting-started.md
guide-introduction.md
guide-migrate-from-tsup.md
option-cjs-default.md
option-cleaning.md
option-config-file.md
option-css.md
option-dependencies.md
option-dts.md
option-entry.md
option-exe.md
option-lint.md
option-log-level.md
option-minification.md
option-output-directory.md
option-output-format.md
option-package-exports.md
option-platform.md
option-root.md
option-shims.md
option-sourcemap.md
option-target.md

詳細

tsdown - エレガントなライブラリバンドラー

Rolldown と Oxc を基盤とする、TypeScript/JavaScript ライブラリ向けの超高速バンドラーです。

実行環境の要件

tsdown の実行には Node.js 22.18.0 以降が必要です(ビルド時のみ)。ただし、target オプションを使って、バンドル出力の対象をはるかに古い Node.js バージョンに設定できます。そのため、tsdown でビルドしたライブラリは、実行時に Node.js 22 以降に限定されるわけではありません。

パッケージで Node.js 18/20 をサポートする必要がある場合:

  • CI では Node.js 22 以降を使ってビルドします(例:target: 'node18' または target: 'node20' を設定します)。
  • サポート対象とする古い Node.js バージョンで、ビルド済みの出力(またはパッケージ化した tarball)をテストします。たとえば、公開済みパッケージのテストを Node.js 18/20/22 で実行するマトリックスジョブを使用します。

使用する場面

  • npm 向けの TypeScript/JavaScript ライブラリのビルド
  • TypeScript 宣言ファイル(.d.ts)の生成
  • 複数の形式(ESM、CJS、IIFE、UMD)へのバンドル
  • ツリーシェイキングとミニファイによるバンドルの最適化
  • 最小限の変更で tsup から移行
  • React、Vue、Solid、Svelte コンポーネントライブラリのビルド

クイックスタート

# Install
pnpm add -D tsdown

# Basic usage
npx tsdown

# With config file
npx tsdown --config tsdown.config.ts

# Watch mode
npx tsdown --watch

# Migrate from tsup
npx tsdown-migrate

基本設定

import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['./src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
  clean: true,
})

主なリファレンス

トピック説明リファレンス
はじめにインストール、最初のバンドル、CLI の基本guide-getting-started
設定ファイル設定ファイルの形式、複数の設定、ワークスペースoption-config-file
CLI リファレンスすべての CLI コマンドとオプションreference-cli
tsup からの移行移行ガイドと互換性に関する注意事項guide-migrate-from-tsup
プラグインRolldown、Rollup、Unplugin のサポートadvanced-plugins

オプションの対応表を含む包括的な移行支援については、専用の tsdown-migrate スキルをインストールしてください:npx skills add rolldown/tsdown --skill tsdown-migrate | フック | 独自のロジックを実装するライフサイクルフック | advanced-hooks | | プログラム API | Node.js スクリプトからのビルド | advanced-programmatic | | Rolldown オプション | オプションを Rolldown に直接渡す | advanced-rolldown-options | | CI 環境 | CI の検出、'ci-only'/'local-only' の値 | advanced-ci |

ビルドオプション

オプション使用例リファレンス
エントリーポイントentry: ['src/*.ts', '!**/*.test.ts']option-entry
出力形式format: ['esm', 'cjs', 'iife', 'umd']option-output-format
出力ディレクトリoutDir: 'dist'、outExtensionsoption-output-directory
型宣言dts: true、dts: { sourcemap, compilerOptions, vue }option-dts
対象環境target: 'es2020'、target: 'esnext'option-target
プラットフォームplatform: 'node'、platform: 'browser'option-platform
ツリーシェイキングtreeshake: true、カスタムオプションoption-tree-shaking
ミニファイminify: true、minify: 'dce-only'option-minification
ソースマップsourcemap: true、'inline'、'hidden'option-sourcemap
ウォッチモードwatch: true、ウォッチオプションoption-watch-mode
クリーンアップclean: true、クリーンアップパターンoption-cleaning
ログレベルlogLevel: 'silent'、failOnWarn: falseoption-log-level

依存関係の処理

機能使用方法参照
バンドルしないdeps: { neverBundle: ['react', /^@myorg\//] }option-dependencies
常にバンドルするdeps: { alwaysBundle: ['dep-to-bundle'] }option-dependencies
指定したものだけバンドルするdeps: { onlyBundle: ['cac', 'bumpp'] } - 許可リストoption-dependencies
node_modules をスキップするdeps: { skipNodeModulesBundle: true }option-dependencies
自動的に外部化する依存関係/ピア依存関係/オプション依存関係を自動的に外部化しますoption-dependencies

出力の拡張

機能使用方法参照
シムshims: true - ESM/CJS 互換性を追加しますoption-shims
CJS のデフォルトcjsDefault: true(デフォルト)/falseoption-cjs-default
パッケージのエクスポートexports: true - exports フィールドを生成しますoption-package-exports
CSS の処理[experimental] css: { ... } — プリプロセッサー、Lightning CSS、PostCSS、CSS モジュール、コード分割を含む完全なパイプラインです。@tsdown/css が必要ですoption-css
CSS モジュールcss: { modules: { localsConvention: 'camelCase' } } — .module.css ファイルのスコープ付きクラス名ですoption-css
CSS の注入css: { inject: true } — JS 出力で CSS インポートを保持しますoption-css
アンバンドルモードunbundle: true - ディレクトリ構造を保持しますoption-unbundle
ルートディレクトリroot: 'src' - 出力ディレクトリのマッピングを制御しますoption-root
実行可能ファイル[experimental] exe: true - スタンドアロン実行可能ファイルとしてバンドルします。@tsdown/exe によりクロスプラットフォームに対応しますoption-exe
パッケージの検証publint: true、attw: true - パッケージを検証しますoption-lint

フレームワークとランタイムのサポート

フレームワークガイド参照
ReactJSX トランスフォーム、React Compilerrecipe-react
VueSFC サポート、JSXrecipe-vue
SolidSolidJS JSX トランスフォームrecipe-solid
SvelteSvelte コンポーネントライブラリ(ソース配布を推奨)recipe-svelte
WASMrolldown-plugin-wasm による WebAssembly モジュールrecipe-wasm

よく使われるパターン

基本的なライブラリのバンドル
export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
  clean: true,
})
複数のエントリーポイント
export default defineConfig({
  entry: {
    index: 'src/index.ts',
    utils: 'src/utils.ts',
    cli: 'src/cli.ts',
  },
  format: ['esm', 'cjs'],
  dts: true,
})
ブラウザー向けライブラリ(IIFE/UMD)
export default defineConfig({
  entry: ['src/index.ts'],
  format: ['iife'],
  globalName: 'MyLib',
  platform: 'browser',
  minify: true,
})
React コンポーネントライブラリ
export default defineConfig({
  entry: ['src/index.tsx'],
  format: ['esm', 'cjs'],
  dts: true,
  deps: {
    neverBundle: ['react', 'react-dom'],
  },
  inputOptions: {
    jsx: { runtime: 'automatic' },
  },
})
ディレクトリ構造の保持
export default defineConfig({
  entry: ['src/**/*.ts', '!**/*.test.ts'],
  unbundle: true, // Preserve file structure
  format: ['esm'],
  dts: true,
})
CI を考慮した設定
export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
  failOnWarn: 'ci-only',  // opt-in: fail on warnings in CI
  publint: 'ci-only',
  attw: 'ci-only',
})
WASM のサポート
import { wasm } from 'rolldown-plugin-wasm'
import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['src/index.ts'],
  plugins: [wasm()],
})
CSS と Sass を使うライブラリ
export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
  target: 'chrome100',
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "src/styles/variables" as *;`,
      },
    },
  },
})
スタンドアロン実行可能ファイル
export default defineConfig({
  entry: ['src/cli.ts'],
  exe: true,
})
クロスプラットフォーム実行可能ファイル(@tsdown/exe が必要)
export default defineConfig({
  entry: ['src/cli.ts'],
  exe: {
    targets: [
      { platform: 'linux', arch: 'x64', nodeVersion: '25.7.0' },
      { platform: 'darwin', arch: 'arm64', nodeVersion: '25.7.0' },
      { platform: 'win', arch: 'x64', nodeVersion: '25.7.0' },
    ],
  },
})
フックを使った高度な設定
export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
  hooks: {
    'build:before': async (context) => {
      console.log('Building...')
    },
    'build:done': async (context) => {
      console.log('Build complete!')
    },
  },
})

設定機能

複数の設定

複数のビルド設定を使用するには、配列をエクスポートします。

export default defineConfig([
  {
    entry: ['src/index.ts'],
    format: ['esm', 'cjs'],
    dts: true,
  },
  {
    entry: ['src/cli.ts'],
    format: ['esm'],
    platform: 'node',
  },
])
条件付き設定

動的な設定には関数を使用します。

export default defineConfig((options) => {
  const isDev = options.watch
  return {
    entry: ['src/index.ts'],
    format: ['esm', 'cjs'],
    minify: !isDev,
    sourcemap: isDev,
  }
})
ワークスペース/モノレポ

複数のパッケージをビルドするには、glob パターンを使用します。

export default defineConfig({
  workspace: 'packages/*',
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,
})

CLI クイックリファレンス

# Basic commands
tsdown                          # Build once
tsdown --watch                  # Watch mode
tsdown --config custom.ts       # Custom config
npx tsdown-migrate              # Migrate from tsup

# Output options
tsdown --format esm,cjs        # Multiple formats
tsdown -d lib                  # Custom output directory (--out-dir)
tsdown --minify                # Enable minification
tsdown --dts                   # Generate declarations
tsdown --exe                   # Bundle as standalone executable
tsdown --unbundle              # Bundleless mode

# Entry options
tsdown src/index.ts            # Single entry
tsdown src/*.ts                # Glob patterns
tsdown src/a.ts src/b.ts       # Multiple entries

# Workspace / Monorepo
tsdown -W                      # Enable workspace mode
tsdown -W -F my-package        # Filter specific package
tsdown --filter /^pkg-/        # Filter by regex

# Development
tsdown --watch                 # Watch mode
tsdown --sourcemap             # Generate source maps
tsdown --clean                 # Clean output directory
tsdown --from-vite             # Reuse Vite config
tsdown --tsconfig tsconfig.build.json  # Custom tsconfig

ベストプラクティス

  1. TypeScript ライブラリでは、必ず型宣言を生成します。

    { dts: true }
    
  2. 不要なコードのバンドルを避けるため、依存関係を外部化します。

    { deps: { neverBundle: [/^react/, /^@myorg\//] } }
    
  3. バンドルサイズを最適化するため、ツリーシェイキングを使用します。

    { treeshake: true }
    
  4. 本番ビルドでは、圧縮を有効にします。

    { minify: true }
    
  5. ESM/CJS との互換性を高めるため、シムを追加します。

    { shims: true }  // Adds __dirname, __filename, etc.
    
  6. package.json の exports を自動生成します。

    { exports: true }  // Creates proper exports field
    
  7. 開発中は、ウォッチモードを使用します。

    tsdown --watch
    
  8. ファイル数の多いユーティリティでは、構造を維持します。

    { unbundle: true }  // Keep directory structure
    
  9. 公開前に、CI でパッケージを検証します。

    { publint: 'ci-only', attw: 'ci-only' }
    

リソース

コメント