TypeScript Declaration Webpack Plugin

0.3.0 · active · verified Sun Apr 19

The `typescript-declaration-webpack-plugin` is a Webpack plugin designed to consolidate all TypeScript declaration files (`.d.ts`) generated by the TypeScript compiler and its loaders during a Webpack build into a single, unified `.d.ts` file. This functionality is particularly beneficial for library authors who distribute their compiled JavaScript alongside their type definitions, providing a single, coherent type entry point for consumers. The plugin intelligently sorts and deduplicates module imports within the bundled declaration file. The current stable version is `0.3.0`, which introduced named exports for the plugin class and configuration, alongside enhanced declaration emission. While the project doesn't follow a strict release cadence, it shows active development with recent patches addressing critical issues and adding new features like comment removal from declarations.

Common errors

Warnings

Install

Imports

Quickstart

This Webpack configuration demonstrates how to set up `typescript-declaration-webpack-plugin` to bundle a TypeScript library, using `ts-loader` for compilation and emitting a single `index.d.ts` file alongside the bundled JavaScript output.

const path = require('path');
const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');

module.exports = {
  mode: 'production',
  entry: './src/index.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'umd',
    library: 'MyLibrary',
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  plugins: [
    new TypescriptDeclarationPlugin({
      out: 'index.d.ts', // Name of the bundled declaration file
      removeMergedDeclarations: true, // Remove individual .d.ts files after merging
      removeComments: true, // Remove comments from the final .d.ts file
    }),
  ],
  externals: { // Example for common library scenarios
    react: 'React',
    'react-dom': 'ReactDOM',
  },
};

view raw JSON →