Add CommonJS support via dual packaging#26
Conversation
621245a to
2b4000b
Compare
There was a problem hiding this comment.
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.cjscontaining the TinyQueue implementation with CommonJS exports - Converted
index.jsto a thin ESM wrapper that re-exports fromindex.cjs - Updated
package.jsonwithexportsfield for conditional import/require resolution and updatedmainto point toindex.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.
2b4000b to
4eaf6d2
Compare
There was a problem hiding this comment.
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.
4eaf6d2 to
f28f93e
Compare
There was a problem hiding this comment.
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.
|
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. |
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
index.cjsas the single source of truth for the TinyQueue implementation, withindex.jsre-exporting it via ESM to avoid duplicating codeexportsfield inpackage.jsonwithtypes,import, andrequireconditions for proper TypeScript and dual-module resolutionmainto./index.cjsfor older Node.js compatibilityindex.jsandindex.cjsin thefilesarray so both entry points are published.cjsfilescreateRequireto verify CJSrequire()resolves throughpackage.jsonrequireusage exampleTest plan
npm test)require()test passes viacreateRequireresolving through package rootrequire('tinyqueue')works in a CJS projectimport TinyQueue from 'tinyqueue'works in an ESM projectindex.cjsis linted alongside.jsfiles