Webpack 4 Declaration Bundler Plugin
This library, `declaration-bundler-webpack4-plugin`, bundles individual TypeScript declaration files (`.d.ts`) generated during the Webpack build process into a single, combined declaration file. It achieves this by re-composing the declarations as if all classes and interfaces were defined as internal modules within a specified `moduleName`. This makes the plugin particularly relevant for niche scenarios where classes and interfaces are deliberately exposed to a global module space, allowing for runtime access patterns that mimic internal TypeScript modules, often mapping prefixed URIs (like `foaf:Image`) back to module paths (e.g., `my.modules.rdfs.Image`). The current version is 1.0.6-beta.2. Given its beta status, the last commit dating back to early 2020, and its explicit tie to Webpack 4 (which is no longer the current major version of Webpack), its release cadence is effectively stalled, indicating it is no longer actively maintained. Its key differentiator is this unique, somewhat opinionated, approach to declaration bundling for a very specific internal module emulation pattern, which the documentation explicitly states makes it unsuitable for standard internal or external module usage. It primarily functions as an extension of `ts-loader`, requiring `ts-loader` to generate the initial individual declaration files for each source file, which this plugin then processes into a single output.
Common errors
-
Module not found: Error: Can't resolve 'declaration-bundler-webpack-plugin' in '...'
cause The plugin package is not installed as a dependency in your project.fixRun `npm install declaration-bundler-webpack-plugin --save-dev` or `yarn add declaration-bundler-webpack-plugin --dev`. -
TypeError: DeclarationBundlerPlugin is not a constructor
cause Incorrect import syntax for a CommonJS module, or issues with how `require` resolves the module, leading to attempting to call `new` on a non-constructor.fixEnsure you are using `const DeclarationBundlerPlugin = require('declaration-bundler-webpack-plugin');` as the plugin exports as a CommonJS module and is not designed for ES module `import` syntax. -
ERROR in DeclarationBundlerPlugin: No declaration files found to bundle. Please ensure 'ts-loader' is configured to generate '.d.ts' files.
cause `ts-loader` is not configured to generate individual TypeScript declaration files (`.d.ts`), or `declaration: true` is missing in your `tsconfig.json`.fixVerify that `ts-loader` is properly set up in your Webpack configuration and that `declaration: true` is enabled within your `tsconfig.json` file to produce the necessary `.d.ts` artifacts.
Warnings
- gotcha This plugin is designed for a very specific use case involving exposing external modules as internal TypeScript modules in a global namespace. It is explicitly stated in the documentation that it is not useful if your project adheres to standard internal or external module patterns.
- breaking This plugin is explicitly for Webpack 4. It is highly unlikely to be compatible with Webpack 5 or newer versions due to significant breaking changes in Webpack's API and plugin architecture.
- gotcha The plugin requires `ts-loader` to be configured with `declaration: true` in your `tsconfig.json` (or equivalent) to generate individual `.d.ts` files for each source file, which it then bundles. Without these individual declaration files, the plugin will have no declarations to process.
- deprecated The package is in beta (1.0.6-beta.2) and has seen no updates since early 2020. Its original upstream project (`smol/declaration-bundler-webpack-plugin`) is archived. This indicates the plugin is abandoned and not actively maintained, posing a risk for future compatibility, security, and bug fixes.
Install
-
npm install declaration-bundler-webpack4-plugin -
yarn add declaration-bundler-webpack4-plugin -
pnpm add declaration-bundler-webpack4-plugin
Imports
- DeclarationBundlerPlugin
import DeclarationBundlerPlugin from 'declaration-bundler-webpack-plugin'; import { DeclarationBundlerPlugin } from 'declaration-bundler-webpack-plugin';const DeclarationBundlerPlugin = require('declaration-bundler-webpack-plugin');
Quickstart
const path = require('path');
const DeclarationBundlerPlugin = require('declaration-bundler-webpack-plugin');
module.exports = {
entry: './src/init.ts', // Assumes entry files like src/init.ts, src/foo.ts, src/foo2.ts as per README
output: {
filename: './builds/bundle.js',
path: path.resolve(__dirname, 'dist') // Using path.resolve for output directory
},
resolve: {
extensions: ['.ts', '.tsx', '.js'] // Modern usage, simplified from README for clarity
},
module: {
rules: [ // 'loaders' is deprecated, 'rules' is current for Webpack 4+
{ test: /\.ts(x?)$/, loader: 'ts-loader' }
]
},
watch: false, // Set to false for a typical build, README shows true
plugins: [
new DeclarationBundlerPlugin({
moduleName:'some.path.moduleName',
out:'./builds/bundle.d.ts'
})
]
};