TypeScript Declaration Webpack Plugin
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
-
Error: Plugin 'TypescriptDeclarationPlugin' not found
cause The package `typescript-declaration-webpack-plugin` was not installed or is not correctly resolved in your project.fixRun `npm install --save-dev typescript-declaration-webpack-plugin` or `yarn add --dev typescript-declaration-webpack-plugin`. -
TS2307: Cannot find module '...' or its corresponding type declarations.
cause TypeScript is unable to locate declaration files for an imported module, often because `declaration: true` is missing in `tsconfig.json` or the build process didn't generate them correctly before the plugin runs.fixVerify `"declaration": true` is present in `compilerOptions` of your `tsconfig.json`. Also, ensure `ts-loader` or similar is correctly configured in your Webpack rules to process TypeScript files and produce declarations. -
Webpack compilation fails, but no specific error from typescript-declaration-webpack-plugin is shown.
cause The plugin might not be receiving any declaration files from the upstream TypeScript compilation, leading to an empty or non-functional output without explicit errors from the plugin itself.fixCheck your `tsconfig.json` for `"declaration": true`. Ensure your `ts-loader` setup is generating `.d.ts` files in your build output before the plugin attempts to process them. You might temporarily disable `removeMergedDeclarations: true` to inspect intermediate `.d.ts` files.
Warnings
- gotcha The plugin relies on TypeScript generating declaration files. Ensure `compilerOptions.declaration` is set to `true` in your `tsconfig.json` file. Without this, the plugin will have no `.d.ts` files to merge.
- breaking Versions `0.2.0` and `0.2.2` included fixes for "two serious issues." While specific breaking changes are not detailed, users upgrading from `v0.1.x` or earlier may encounter different behaviors or require adjustments due to these critical bug fixes and plugin reworks.
- gotcha When using ESM in your project (`type: module` in `package.json`), ensure your Webpack configuration is also set up for ESM or use dynamic `import()` for the plugin. Older Webpack configs often use CommonJS `require()`, which might conflict with newer module systems.
- gotcha The plugin is designed to work with declaration files generated by a TypeScript loader (e.g., `ts-loader`). If you are using a different setup for TypeScript compilation (e.g., `babel-loader` without `declaration: true` in `tsconfig.json`), the plugin might not find any declaration files to process.
Install
-
npm install typescript-declaration-webpack-plugin -
yarn add typescript-declaration-webpack-plugin -
pnpm add typescript-declaration-webpack-plugin
Imports
- TypescriptDeclarationPlugin
const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin').default;import TypescriptDeclarationPlugin from 'typescript-declaration-webpack-plugin';
- TypescriptDeclarationPlugin (named)
import TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin'; - CommonJS require
import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');
Quickstart
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',
},
};