使用BundleDeclarationsWebpackPlugin合并TS文件时遇TypeError:不是构造函数,求排查配置
问题分析与解决
出现TypeError: BundleDeclarationsWebpackPlugin is not a constructor的核心原因是插件引入方式错误:
bundle-declarations-webpack-plugin 以ES模块规范编写,用CommonJS的require()引入时,默认导出会被包裹在.default属性里,你当前的代码直接引入了整个模块对象,而非插件的构造函数本身。
修复步骤
修改插件引入代码,添加.default:
const BundleDeclarationsWebpackPlugin = require("bundle-declarations-webpack-plugin").default;
修改后的完整配置片段:
const path = require('path'); const nodeExternals = require('webpack-node-externals'); const BundleDeclarationsWebpackPlugin = require("bundle-declarations-webpack-plugin").default; // 此处添加.default module.exports = { entry: './src/index.ts', mode: 'production', output: { ... }, devtool: 'source-map', module: { ... }, plugins: [ new BundleDeclarationsWebpackPlugin({ entry: './dist/types/index.d.ts', outFile: './dist/index.d.ts', }), ], resolve: { extensions: ['.tsx', '.ts', '.js'] } };
额外检查点
- 确保
bundle-declarations-webpack-plugin版本与你的webpack版本兼容(比如webpack 5需搭配插件最新版本) - 确认
entry配置的.d.ts文件路径正确,webpack执行时该文件已通过tsc编译生成
内容的提问来源于stack exchange,提问作者Cyril
相关产品推荐
相关产品推荐

