Fix TypeScript CJS Default Exports Declarations
This utility addresses a common issue in TypeScript declaration generation, specifically when dealing with default exports within CommonJS modules. It provides a programmatic API and a Rollup plugin to correctly transform `d.mts` files into CommonJS-compatible `d.ts` and `d.cts` files, ensuring interoperability. The current stable version is 1.0.1, with recent releases indicating an active maintenance cadence primarily focused on bug fixes and compatibility. Key differentiators include its direct integration via a Rollup plugin, making it suitable for build tools like `unbuild` (which leverages it internally since v3.5.0), and its specific focus on the nuanced problem of default exports in CJS TypeScript declarations where other bundlers or compilers might produce incorrect outputs. It provides a targeted solution for a specific TypeScript build problem, rather than being a general-purpose bundler.
Common errors
-
Cannot find name 'FixDtsDefaultCjsExportsPlugin'.
cause Incorrect import path for the Rollup plugin.fixEnsure you are importing the plugin from the correct subpath: `import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'`. -
Error: The plugin 'unbuild-fix-cjs-export-type' was already used.
cause You might be trying to add `fix-dts-default-cjs-exports` plugin to `unbuild` which already has an internal, potentially conflicting, plugin for the same purpose (especially in older `unbuild` versions).fixIf using an `unbuild` version older than 3.5.0, you may need to filter out `unbuild`'s internal plugin within the `rollup:dts:options` hook before pushing the `fix-dts-default-cjs-exports` plugin, as commented in the quickstart example. -
TypeScript declarations are incorrect for default exports in CommonJS output.
cause The `fix-dts-default-cjs-exports` plugin is not correctly configured or not being applied in your build process, or your build tool is not supported.fixVerify that the plugin is correctly integrated as a Rollup plugin, especially within `unbuild`'s `rollup:dts:options` hook with `rollup.emitCJS` enabled. If using `tsup` or `pkgroll`, consider a post-build programmatic fix.
Warnings
- breaking Version 1.0.0 introduced breaking changes related to the inclusion of Node.js 10 types. Users upgrading from pre-1.0.0 versions should verify their build environment's Node.js compatibility.
- gotcha When using the Rollup plugin with `unbuild`, you *must* register the plugin directly when `rollup.emitCJS = true` is enabled. Failing to do so can lead to incorrect transformations or the plugin not being applied effectively.
- gotcha The package's Rollup plugin is intended to fix default CJS exports. It does not control file generation itself. Misconfiguration of your build tool's declaration options (e.g., `unbuild`'s `declaration` option) can still lead to unexpected output files.
- gotcha This package is not directly compatible with build tools like `tsup` or `pkgroll` as they currently do not expose hooks to replace or augment their internal Rollup configurations. Direct integration is limited to tools that allow Rollup plugin injection.
- gotcha Version 1.0.1 added a `// @ts-ignore` annotation to default exports when mixed exports are present. While this fixes a specific bug, developers should be aware that `ts-ignore` masks potential TypeScript issues and should be understood in the context of the CJS/ESM interop problem this library solves.
Install
-
npm install fix-dts-default-cjs-exports -
yarn add fix-dts-default-cjs-exports -
pnpm add fix-dts-default-cjs-exports
Imports
- fixDtsDefaultCjsExports
const { fixDtsDefaultCjsExports } = require('fix-dts-default-cjs-exports')import { fixDtsDefaultCjsExports } from 'fix-dts-default-cjs-exports' - FixDtsDefaultCjsExportsPlugin
import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports'import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup' - PluginOptions
import type { PluginOptions } from 'fix-dts-default-cjs-exports/rollup'
Quickstart
import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup';
import { defineBuildConfig } from 'unbuild';
export default defineBuildConfig({
entries: ['./src/index'],
declaration: true,
clean: true,
rollup: { emitCJS: true },
hooks: {
'rollup:dts:options': (ctx, options) => {
// Optional: Uncomment to remove unbuild's internal plugin if it conflicts or you need full control
// options.plugins = options.plugins.filter((p) => {
// if (!p || typeof p === 'string' || Array.isArray(p) || !('name' in p))
// return true;
// return p.name !== 'unbuild-fix-cjs-export-type';
// });
options.plugins.push(FixDtsDefaultCjsExportsPlugin({
warn: message => ctx.warnings.add(message)
}));
}
}
});