{"id":12229,"library":"typescript-declaration-webpack-plugin","title":"TypeScript Declaration Webpack Plugin","description":"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.","status":"active","version":"0.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/chitter99/typescript-declaration-webpack-plugin","tags":["javascript","webpack","typescript","bundler","declaration"],"install":[{"cmd":"npm install typescript-declaration-webpack-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-declaration-webpack-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-declaration-webpack-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This is a Webpack plugin and requires Webpack to function.","package":"webpack","optional":false},{"reason":"The plugin processes TypeScript declaration files and relies on TypeScript's compilation output.","package":"typescript","optional":false}],"imports":[{"note":"Since v0.3.0, the plugin class is also exported as a default for ESM compatibility. For CommonJS, `require('pkg')` directly returns the plugin constructor.","wrong":"const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin').default;","symbol":"TypescriptDeclarationPlugin","correct":"import TypescriptDeclarationPlugin from 'typescript-declaration-webpack-plugin';"},{"note":"Named exports for the plugin class were introduced in v0.3.0 for better ESM interoperability. Use this for explicit named imports.","wrong":"import TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');","symbol":"TypescriptDeclarationPlugin (named)","correct":"import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';"},{"note":"This is the standard CommonJS `require` pattern for Node.js environments and older Webpack configurations, as shown in the package's README. Attempts to use ESM `import` syntax in a CommonJS context will lead to errors.","wrong":"import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';","symbol":"CommonJS require","correct":"const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');"}],"quickstart":{"code":"const path = require('path');\nconst TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');\n\nmodule.exports = {\n  mode: 'production',\n  entry: './src/index.ts',\n  output: {\n    filename: 'bundle.js',\n    path: path.resolve(__dirname, 'dist'),\n    libraryTarget: 'umd',\n    library: 'MyLibrary',\n  },\n  module: {\n    rules: [\n      {\n        test: /\\.tsx?$/,\n        use: 'ts-loader',\n        exclude: /node_modules/,\n      },\n    ],\n  },\n  resolve: {\n    extensions: ['.tsx', '.ts', '.js'],\n  },\n  plugins: [\n    new TypescriptDeclarationPlugin({\n      out: 'index.d.ts', // Name of the bundled declaration file\n      removeMergedDeclarations: true, // Remove individual .d.ts files after merging\n      removeComments: true, // Remove comments from the final .d.ts file\n    }),\n  ],\n  externals: { // Example for common library scenarios\n    react: 'React',\n    'react-dom': 'ReactDOM',\n  },\n};","lang":"typescript","description":"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."},"warnings":[{"fix":"Add `\"declaration\": true` to your `compilerOptions` in `tsconfig.json`.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Review your Webpack configuration and `tsconfig.json` after upgrading to these versions, and test your build output thoroughly. Consult the project's GitHub issues for more context if problems arise.","message":"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.","severity":"breaking","affected_versions":">=0.2.0 <0.3.0"},{"fix":"For ESM projects, use `import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';` in your `webpack.config.js` (if it supports ESM). For CommonJS, stick to `const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');`.","message":"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.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"Ensure your Webpack setup includes a TypeScript loader configured to emit declaration files, and that your `tsconfig.json` specifies `\"declaration\": true`.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Run `npm install --save-dev typescript-declaration-webpack-plugin` or `yarn add --dev typescript-declaration-webpack-plugin`.","cause":"The package `typescript-declaration-webpack-plugin` was not installed or is not correctly resolved in your project.","error":"Error: Plugin 'TypescriptDeclarationPlugin' not found"},{"fix":"Verify `\"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.","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.","error":"TS2307: Cannot find module '...' or its corresponding type declarations."},{"fix":"Check 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.","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.","error":"Webpack compilation fails, but no specific error from typescript-declaration-webpack-plugin is shown."}],"ecosystem":"npm"}