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.
npm install fix-dts-default-cjs-exportsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate the Rollup plugin with `unbuild` to ensure correct TypeScript declaration output for default CommonJS exports. This configuration is for `unbuild` versions prior to v3.5.0 or when needing custom plugin options.
Ensure your project's `tsconfig.json` and build environment are compatible with the Node.js 10 type inclusions, or adjust `target` and `lib` settings as necessary.
Ensure `rollup.emitCJS: true` is set in your `unbuild` configuration and the `FixDtsDefaultCjsExportsPlugin` is explicitly added to `options.plugins` within the `rollup:dts:options` hook, as shown in the quickstart.
Consult the `declaration` option in your build tool's documentation (e.g., `unbuild`'s configuration) to ensure correct file output paths and types (`d.ts`, `d.cts`, `d.mts`).
For `tsup` or `pkgroll`, you may need to use a post-build script to apply the `fixDtsDefaultCjsExports` API programmatically, or wait for these tools to introduce plugin hooks.
No direct fix needed, but be aware of the annotation. If encountering unexpected TypeScript errors after upgrading, review how mixed exports are handled in your declaration files.
Ensure you are importing the plugin from the correct subpath: `import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'`.If 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.
Verify 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.