Webpack TypeScript Declaration Bundler
types-webpack-bundler is a specialized webpack plugin designed to combine multiple TypeScript declaration files (`.d.ts`) generated by `ts-loader` (or similar tools) into a single, consolidated declaration file. Unlike standard declaration bundling which often produces external modules, this plugin recomposes declarations to mimic an 'internal module' structure, exposing classes and interfaces to a global-like module space. This is a fork of an unmaintained original library, updated to support Webpack 5. Its primary use case is highly niche: allowing external modules to behave like internal modules for specific scenarios, such as mapping prefixed URIs to module paths. The current stable version is 1.0.2. It does not have a stated regular release cadence and is described as an 'experimental setup' by its author, suggesting infrequent updates unless a specific need arises.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'compilation')
cause The plugin is likely being initialized incorrectly or in an incompatible Webpack version/environment where the expected Webpack plugin API shape is not present.fixEnsure you are using Webpack 5 as a peer dependency. Double-check your `webpack.config.js` to confirm the plugin is instantiated correctly: `new DeclarationBundlerPlugin({...})` within the `plugins` array. -
Error: ENOENT: no such file or directory, open '.../bundle.d.ts'
cause The output directory specified for the bundled declaration file (`out` option) does not exist, and the plugin might not automatically create it.fixEnsure the directory specified in the `out` option (e.g., `./builds/`) exists before webpack runs, or configure webpack's output path to ensure it creates necessary directories. You might need to use a tool like `fs.mkdirSync` in a pre-build script or rely on webpack's output directory creation.
Warnings
- gotcha This library is explicitly described as an 'experimental setup' and is intended for a very specific and rare use case. It allows external modules to mimic internal modules by re-composing declaration files into a global-like scope. For most TypeScript projects using modern module systems (ESM or CommonJS), this approach is generally not recommended or necessary.
- gotcha The plugin expects separate declaration files to be generated by a loader (e.g., `ts-loader` with `declaration: true` in `tsconfig.json`). If the loader is not configured to emit these files, the bundler will have no input and will likely fail or produce an empty output.
- breaking This is a fork of an unmaintained original library. While it supports Webpack 5, it implies that future Webpack major versions might not be immediately supported without further updates to this project.
Install
-
npm install types-webpack-bundler -
yarn add types-webpack-bundler -
pnpm add types-webpack-bundler
Imports
- DeclarationBundlerPlugin
const DeclarationBundlerPlugin = require('types-webpack-bundler').default;import DeclarationBundlerPlugin from 'types-webpack-bundler';
- DeclarationBundlerPlugin
const DeclarationBundlerPlugin = require('types-webpack-bundler');
Quickstart
import * as path from 'path';
import DeclarationBundlerPlugin from 'types-webpack-bundler';
export default {
entry: './src/init.ts',
output: {
filename: './builds/bundle.js',
path: path.resolve(__dirname, 'dist')
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: [
{
test: /\.ts(x?)$/,
loader: 'ts-loader',
options: {
compilerOptions: { declaration: true, outDir: './builds' } // ts-loader must generate declarations
}
}
]
},
plugins: [
new DeclarationBundlerPlugin({
moduleName:'some.path.moduleName',
out:'./builds/bundle.d.ts',
excludedReferences: []
})
],
mode: 'development' // or 'production'
};