Skip to content

fix(commonjs): support default imports with Node16 and NodeNext - #2076

Open
imssurya wants to merge 1 commit into
rollup:masterfrom
imssurya:fix/commonjs-nodenext-types
Open

imssurya wants to merge 1 commit into
rollup:masterfrom
imssurya:fix/commonjs-nodenext-types

Conversation

@imssurya

@imssurya imssurya commented Dec 5, 2026

Copy link
Copy Markdown

Rollup Plugin Name: @rollup/plugin-commonjs

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

#1860 (this fixes only commonjs; same approach as #2028 for virtual)

Description

With moduleResolution: node16 or nodenext, an ES module consumer gets the CommonJS-format types/index.d.ts for the import condition, which actually resolves to dist/es/index.js. So the documented usage doesn't type-check, even though it runs fine:

// rollup.config.mts
import commonjs from '@rollup/plugin-commonjs';

commonjs();
// TS2349: This expression is not callable.

The workaround TypeScript suggests, commonjs.default(), type-checks but throws TypeError: commonjs.default is not a function at runtime.

This does the same as #2028 did for @rollup/plugin-virtual: a new types/index.d.mts reuses the existing function type and re-exports the option types, and exports gets per-condition types for import and require. The runtime entries, the CJS .d.ts, the top-level types and the default fallback don't change.

test/types.test.js (copied from virtual) type-checks ESM, CJS and legacy fixtures under Node16 and NodeNext. The two ESM cases fail without the change; all 5 pass with it. The one difference from virtual's test is that there's no explicit lib list, because this package uses TypeScript 4.8, which has no esnext.disposable lib.

Checked: pnpm build, pnpm test (304), pnpm test:earliest, pnpm test:ts and pnpm lint (no new warnings) pass. attw on the packed tarball reports no problems. A real rollup.config.mts using commonjs() type-checks and builds with TypeScript 5.9 under nodenext, bundler and node10.

node-resolve, json, replace, babel, terser, inject, typescript and url have the same problem. I can send the same change for them, one PR each, if this approach is fine.

@imssurya
imssurya requested a review from shellscape as a code owner December 5, 2026 20:14
@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge.

Reviews (1) · Last reviewed commit: "fix(commonjs): support default imports w..." · Reviewed by Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant