Skip to content

Usage ​

Dtsbuild exports a CLI and a build() API to bundle type files. Bundling .d.ts files directly works out of the box, but bundling .ts files requires either typescript to be installed or compilerOptions.isolatedDeclarations to be enabled in the tsconfig.json (recommended for faster builds).

To compile with TypeScript:

bash
$ npm install -D typescript
bash
$ yarn add -D typescript
bash
$ pnpm add -D typescript

To compile with isolated declarations, update tsconfig.json:

json
{
  "compilerOptions": {
    "isolatedDeclarations": true
  }
}

TIP

The tsconfig.json is picked up automatically if it's in the current working directory, otherwise, you can specify the path to the tsconfig.json manually with the tsconfig option.

Options ​

build() accepts a single options object with a required entryPoints field. See the API reference for all available options.

Examples ​

Basic usage ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: ['./src/index.ts', './src/utils.ts'],
})

// Outputs:
// dist/index.d.ts
// dist/utils.d.ts
// dist/chunk-1.d.ts

Empty output directory before build ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: ['./src/index.ts'],
  emptyOutDir: true,
})

Custom output file names ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: {
    main: './src/index.ts',
    utils: './src/utils.ts',
  },
})

// Outputs:
// dist/main.d.ts
// dist/utils.d.ts
// dist/chunk-1.d.ts

Custom entry and chunk names ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: {
    main: './src/index.ts',
    utils: './src/utils.ts',
  },
  entryNames: '[name]/index.[ext]',
  chunkNames: '_chunks/[name]-[count].[ext]',
})

// Outputs:
// dist/main/index.d.ts
// dist/utils/index.d.ts
// dist/_chunks/chunk-1.d.ts

Custom output base directory ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: ['./src/public/index.ts', './src/public/utils.ts'],
  outBase: './src',
})

// Outputs:
// dist/public/index.d.ts
// dist/public/utils.d.ts
// dist/chunk-1.d.ts
// (If `outBase` is unset, outputs would be e.g. `dist/index.d.ts` instead)

Bundle libraries ​

ts
import { build } from '@dtsbuild/core'

await build({
  entryPoints: ['./src/index.ts'],

  // Bundle `vite` dependency types
  external: ['!vite']

  // Bundle `@scope/**` dependency types
  external: ['!@scope/**']

  // Bundle all dependencies
  external: ['!**/**'],

  // Bundle all dependencies except `vite`
  external: ['!**/**', 'vite'],
})