Skip to content

Comments

Add CommonJS support via dual packaging#26

Closed
attaradev wants to merge 1 commit intomourner:mainfrom
attaradev:add-cjs-support
Closed

Add CommonJS support via dual packaging#26
attaradev wants to merge 1 commit intomourner:mainfrom
attaradev:add-cjs-support

Conversation

@attaradev
Copy link

@attaradev attaradev commented Feb 16, 2026

Motivation

Many popular tools and libraries still rely on CommonJS — notably Jest, ts-node (in default mode), and older bundlers — and cannot consume ESM-only packages without workarounds. Adding CJS support makes tinyqueue usable out of the box in these environments.

Summary

  • Add index.cjs as the single source of truth for the TinyQueue implementation, with index.js re-exporting it via ESM to avoid duplicating code
  • Add exports field in package.json with types, import, and require conditions for proper TypeScript and dual-module resolution
  • Update main to ./index.cjs for older Node.js compatibility
  • Include index.js and index.cjs in the files array so both entry points are published
  • Update lint script to cover .cjs files
  • Add test using createRequire to verify CJS require() resolves through package.json
  • Update README with CommonJS require usage example

Test plan

  • All existing tests pass (npm test)
  • New CJS require() test passes via createRequire resolving through package root
  • require('tinyqueue') works in a CJS project
  • import TinyQueue from 'tinyqueue' works in an ESM project
  • index.cjs is linted alongside .js files

Copilot AI review requested due to automatic review settings February 16, 2026 16:56
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces dual package support for both CommonJS and ESM module systems to the tinyqueue library. The implementation moves the TinyQueue class to index.cjs as the single source of truth, with index.js serving as an ESM wrapper that re-exports from the CommonJS file. The package.json is updated with conditional exports to route require() calls to index.cjs and import statements to index.js.

Changes:

  • Created index.cjs containing the TinyQueue implementation with CommonJS exports
  • Converted index.js to a thin ESM wrapper that re-exports from index.cjs
  • Updated package.json with exports field for conditional import/require resolution and updated main to point to index.cjs

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

File Description
index.cjs New file containing the TinyQueue implementation with CommonJS module.exports
index.js Converted to ESM re-export wrapper that imports from index.cjs
package.json Added exports field with conditional import/require, updated main to index.cjs, added index.cjs to files array
test.js Added test that dynamically imports index.cjs to verify CJS compatibility

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Enable CommonJS consumers to require tinyqueue by adding an index.cjs
entry point with the exports field for conditional resolution. The ESM
index.js re-exports from index.cjs to keep a single source of truth.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mourner
Copy link
Owner

mourner commented Feb 16, 2026

Sorry, but I don't want to support CommonJS in principle — it's time to let it die, and the more packages stop supporting it, the faster Jest / ts-node and others will adapt. v2.0.3 is still perfectly usable if you want to use tinyqueue with CommonJS.

@mourner mourner closed this Feb 16, 2026
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.

2 participants