Fix TypeScript CJS Default Exports Declarations

1.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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.

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)
      }));
    }
  }
});

view raw JSON →