Registry / devops / typescript-declaration-webpack-plugin

typescript-declaration-webpack-plugin

JSON →
library0.3.0jsnpmunverified

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.

npm install typescript-declaration-webpack-plugin
INSTALL
IMPORT
SIG · TYPESCRIPT-DECLARA
T
typescript-declaration-webpack-plugin
devopsjavascriptv0.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TypescriptDeclarationPlugin
import TypescriptDeclarationPlugin from 'typescript-declaration-webpack-plugin';
const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin').default;
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.
TypescriptDeclarationPlugin (named)
import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';
import TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');
Named exports for the plugin class were introduced in v0.3.0 for better ESM interoperability. Use this for explicit named imports.
CommonJS require
const TypescriptDeclarationPlugin = require('typescript-declaration-webpack-plugin');
import { TypescriptDeclarationPlugin } from 'typescript-declaration-webpack-plugin';
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.

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', }, };
Debug
Known issues
gotchaThe 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.
fix
Add `"declaration": true` to your `compilerOptions` in `tsconfig.json`.
affects: >=0.1.0
breakingVersions `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.
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.
affects: >=0.2.0 <0.3.0
gotchaWhen 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.
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');`.
affects: >=0.3.0
gotchaThe 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.
fix
Ensure your Webpack setup includes a TypeScript loader configured to emit declaration files, and that your `tsconfig.json` specifies `"declaration": true`.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Plugin 'TypescriptDeclarationPlugin' not found
The package `typescript-declaration-webpack-plugin` was not installed or is not correctly resolved in your project.
fix
Run `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.
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.
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.
Webpack compilation fails, but no specific error from typescript-declaration-webpack-plugin is shown.
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.
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.
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies
webpackrequiredThis is a Webpack plugin and requires Webpack to function.
typescriptrequiredThe plugin processes TypeScript declaration files and relies on TypeScript's compilation output.
Agent activity
4 hits · last 30 days
node
4
Resources