Skip to content

f-lawe/unplugin-dts-bundle-generator

Repository files navigation

DTS Bundle Generator Unplugin

Versions Downloads Licence GitHub Actions

Ever wanted to easily package your typescript library with a bundled declaration file? Integrate DTS Bundle Generator within your favourite bundler thanks to Unplugin!

(see available bundlers below)

Installation

npm i --save-dev unplugin-dts-bundle-generator
yarn add --dev unplugin-dts-bundle-generator

Usage

Only those bundlers are supported at the moment:

ESBuild

With ESBuild, add this block when calling esbuild.build():

import * as esbuild from 'esbuild';
import dtsBundleGenerator from 'unplugin-dts-bundle-generator/rollup';

await esbuild.build({
  plugins: [
    dtsBundleGenerator({
      outfile: 'my-lib.d.ts',
      output: {
        // output config
      },
      libraries: {
        // libraries config
      },
      compilation: {
        // compilation options
      }
    }),
  ],
  entryPoints: 'src/index.ts',
  outfile: 'my-lib.js',
  format: 'esm',
  bundle: true,
});


Rolldown

With Rolldown, add this block to your rolldown.config.ts:

import dtsBundleGenerator from 'unplugin-dts-bundle-generator/rolldown';

export default {
  plugins: [
    dtsBundleGenerator({
      file: 'my-lib.d.ts',
      output: {
        // output config
      },
      libraries: {
        // libraries config
      },
      compilation: {
        // compilation options
      }
    }),
  ],
  input: 'src/index.ts',
  output: {
    dir: 'dist',
    format: 'es',
  },
};


Rollup

With Rollup, add this block to your rollup.config.ts:

import dtsBundleGenerator from 'unplugin-dts-bundle-generator/rollup';

export default {
  plugins: [
    dtsBundleGenerator({
      file: 'my-lib.d.ts',
      output: {
        // output config
      },
      libraries: {
        // libraries config
      },
      compilation: {
        // compilation options
      }
    }),
  ],
  input: 'src/index.ts',
  output: {
    dir: 'dist',
    format: 'es',
  },
};


Vite

With Vite, add this block to your vite.config.ts:

import path from 'node:path';
import dtsBundleGenerator from 'unplugin-dts-bundle-generator/vite';
import { defineConfig, normalizePath } from 'vite';

export default defineConfig({
  plugins: [
    dtsBundleGenerator({
      fileName: 'my-lib.d.ts',
      output: {
        // output config
      },
      libraries: {
        // libraries config
      },
      compilation: {
        // compilation options
      }
    })
  ],
  build: {
    lib: {
      entry: normalizePath('src/index.ts'),
      formats: ['es'],
      fileName: 'my-lib.js'
    }
  }
});


⚠️ Be careful! Plugin options may vary depending on the bundler you are using.

Feel free to open a PR or an issue if you need another export and want to speed up the process, so we can work it out.

Configuration

This library handle both single and multiple entrypoints. You can use any of the output, libraries and compilation options available in the config file of DTS Bundle Generator.

About

Generate bundled dts files with your favorite bundler

Topics

Resources

License

Stars

Watchers

Forks

Contributors