tsupの使い方メモ
tsupって何
少ない設定でTypeScriptコードをバンドルしてくれるツール。
内部でesbuildを使用している。
tsupを使うと何が嬉しいの
tsupの使い方
pnpm add -D tsuptsup src/index.tsこれだけでdist/index.jsが生成される。
tsupの設定の仕方
以下のtsup.config.tsを作成する。
import { defineConfig } from 'tsup'
export default defineConfig({  entry: ['src/index.ts']})すると、さっきのtsup src/index.tsは単にtsupと打つだけで同じ設定でバンドルを実行できる。
型定義ファイル(*.d.ts)も同時に出力したい
tsup --dtsのように--dtsオプションを付けるか、tsup.config.tsにdts: trueを追加する。
import { defineConfig } from 'tsup'
export default defineConfig({  entry: ['src/index.ts'],  dts: true})ESM/CJSの両方に対応するときのtsupやpackage.jsonの書き方
import { defineConfig } from 'tsup'
export default defineConfig({  format: ['esm', 'cjs'],  entry: ['src/index.ts'],  sourcemap: true,  dts: true}){  // ...  "main": "./dist/index.cjs",  "module": "./dist/index.js",  "types": "./dist/index.d.ts",  "exports": {    ".": {      "import": {        "types": "./dist/index.d.ts",        "import": "./dist/index.js"      },      "require": {        "types": "./dist/index.d.cts",        "require": "./dist/index.cjs"      }    }  }  // ...}参考


