Repository navigation
Ship the dual ESM/CJS entry and the CommonJS types (regressions 1 and 6) - #35
Open
codeKonami wants to merge 10 commits into
Open
codeKonami wants to merge 10 commits into
codeKonami wants to merge 10 commits into
Conversation
…mpat/ - PokerHand.name is 'PokerHand' through require() and import, as in 1.0.1. Nothing pinned it since the builds stopped being minified. - An ES module that also require()s the package can compare hands from both: 1.0.1 had a single CommonJS copy, a dual package loads two classes. - new in 1.1.0: destructured require() keeps working, and .PokerHand and .default are the class itself. All pass against 1.0.1 (COMPAT_TARGET=reference). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A second tsdown entry, src/index.cjs.ts, has a single default export, so tsdown's cjsDefault writes dist/index.cjs as `module.exports = PokerHand` and dist/index.d.cts as `export = PokerHand` (#11). The named exports stay reachable from require(): PokerHand, default, HandRank and CompareResult are static properties of the class, and a `declare namespace PokerHand` merged with the class carries them, and Card, Rank and Suit, into index.d.cts. Oxc emits that namespace as is. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`import PokerHand from 'poker-hand-evaluator'` gave the class in 1.0.1, through Node's CommonJS interop, and failed since the ESM build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A dual package loads two PokerHand classes when a program both imports
and requires it, and `instanceof` rejected a hand from the other copy
with "Incorrect hand to compare with". 1.0.1 had one CommonJS copy, so
that mixed use worked there.
compareWith() now checks Symbol.for('poker-hand-evaluator.PokerHand'),
set on the prototype and not enumerable (#12). Any copy of the package
is accepted, which #14 validated as a relaxation. Anything else still
throws the same Error, and Object.create(PokerHand.prototype) still
throws "Incorrect hand".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
compat/types.test.ts compiles three small projects with the repo's TypeScript and skipLibCheck off: node16 from CommonJS, node16 from an ES module, and bundler (module: preserve). They use the class as a value and a type, HandRank and CompareResult, Card/Rank/Suit, through `import PokerHand = require()`, `import PokerHand from` and named imports, and `const r: HandRank = 'FLUSH'` (#30). Skipped against 1.0.1, which shipped no types. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each is now an object `as const` plus a union type of its values under the same name (#30). Member names don't change (HandRank.Flush, CompareResult.Win), and a literal is accepted where a HandRank is expected: `const r: HandRank = 'FLUSH'` compiles. The enums were never published, so nothing breaks; CompareResult loses the reverse mapping of a numeric enum. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- exports: `types` comes first in the `import` and `require` branches, each pointing at its own declaration file (index.d.mts, index.d.cts), as #11 validated. - types at the root: index.d.cts, the pair of `main`, for TypeScript's node10 resolution, which ignores exports. - "./package.json" stays reachable, as it was in 1.0.1. - engines: node >=6 (#15). publint --strict and attw --pack now pass: no problems for node10, node16 from CJS, node16 from ESM and bundler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
"type": "module" becomes "type": "commonjs", the scope 1.0.1 had by default. The library files keep their explicit .mjs/.cjs extensions and don't change. What changes is every .js file in the package, which is now CommonJS: - dist/pokerhand.min.js (#19) and the 1.0.1 deep paths (index.js, src/**/*.js, #29) have fixed .js names. They must load with require() on every Node, and exist as plain files for Node < 12.7, which ignores exports. Under "type": "module", Node >= 12.17 reads them as ES modules (require() gives {} or ERR_REQUIRE_ESM), so each would need a .cjs twin or a nested package.json. A nested package.json also overrides the root "sideEffects" for webpack. - publint suggests the explicit value over an absent field. The "module" field goes: webpack 4 ignores exports and prefers "module" to "main" even for require(), so require('poker-hand-evaluator') got the ES module namespace and `new PokerHand()` threw "PokerHand is not a constructor" (measured with webpack 4.47). 1.0.1 had no "module" field. Bundlers that read exports are unaffected. The tsdown and Vite configs become .mts, so that they stay ES modules without a package-wide "type": "module". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
npm copies the root package's engines into the lockfile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 26, 2026
There was a problem hiding this comment.
🟡 Changes recommended
Two critical active findings block verification: the renamed Vitest ES-module config uses undefined __dirname, and src/poker-hand.ts fails TypeScript compilation because POKER_HAND is inferred as a non-unique symbol.
1 open issue
What changed in this PR
This PR delivers the dual ESM/CommonJS package surface for poker-hand-evaluator, preserving the legacy CommonJS entry behavior while adding public TypeScript declarations and cross-entry hand recognition.
Changes:
- Replaces the single tsdown build with separate ESM and CommonJS entries, including
.d.mtsand.d.ctsdeclarations. - Changes
HandRankandCompareResultto literal-valuedas constobjects with matching union types, and adds static exports toPokerHand. - Updates package exports, CommonJS scoping, Node engine metadata, compatibility tests, and mixed ESM/CJS behavior.
| File | Description |
|---|---|
| vite.config.mts | Updates the tooling configuration reference to the renamed tsdown configuration. |
| tsdown.config.ts | Removes the former single-entry build configuration. |
| tsdown.config.mts | Defines separate ESM and CommonJS builds with declaration generation. |
| src/types.ts | Replaces enums with const objects and literal-union types. |
| src/poker-hand.ts | Adds cross-copy hand recognition, namespace declarations, and static class exports. |
| src/index.ts | Adds the ESM default and named exports. |
| src/index.cjs.ts | Adds the CommonJS default entry. |
| package.json | Updates module type, conditional exports, declaration paths, engines, and compatibility scripts. |
| package-lock.json | Records the package Node engine constraint. |
| compat/vitest.config.mts | Provides the renamed compatibility test configuration. |
| compat/types.test.ts | Validates the published declarations across CommonJS, ESM, and bundler consumer projects. |
| compat/require-api.test.ts | Covers CommonJS class identity, naming, and static exports. |
| compat/README.md | Documents the expanded compatibility test coverage. |
| compat/esm-import.test.ts | Covers ESM default imports, named exports, and mixed-entry comparisons. |
Review effort: Lite
| const POKER_HAND = Symbol.for('poker-hand-evaluator.PokerHand'); | ||
|
|
||
| function isPokerHand(value: unknown): value is PokerHand { | ||
| return value != null && (value as { [POKER_HAND]?: unknown })[POKER_HAND] === true; |
This was referenced Nov 26, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Part of the 1.1.0 map (Carte : publier poker-hand-evaluator 1.1.0 rétrocompatible avec la 1.0.1), ticket Livrer la double entrée ESM/CJS et les types (régressions 1 et 6). Stacked on #34. It applies Double entrée ESM/CJS, Dual package hazard, Cibles d'exécution and Types publics. The UMD (#19), the deep-path shims (#29), CI (#21) and the tables (#33) are left to their own tickets.
Summary
Two tsdown entries, because a CommonJS file can only be
module.exports = PokerHandwhen its entry has nothing but a default export:src/poker-hand.tsgets the merged namespace and the static properties thatexport =needs. Oxc copies the namespace into both declaration files as written, so #11's.d.cts(validated with tsc 5.9) comes out the same with Oxc:compareWith()checks a marker instead ofinstanceof(#12, a Relaxation per #14):The marker is a non-enumerable property of the prototype, so
Object.keys()of an instance is stillhand,score,rank.HandRankandCompareResultbecomeas constobjects, each with a literal union of the same name (#30). The member names don't change.package.json:Why
"type": "module"goesThe package becomes CommonJS-scoped again, which is what 1.0.1 got by default. publint suggests writing
"commonjs"explicitly. The library files keep their.mjsand.cjsextensions, so they don't change. The change applies to every.jsfile the package ships, and two tickets need those files to be CommonJS:dist/pokerhand.min.jshas a fixed.jsname and must load withrequire(). Under"type": "module", Node ≥ 12.17 reads it as an ES module.require()then gives{}on Node 22, orERR_REQUIRE_ESMon Node 20 (measured by Double entrée ESM/CJS : comment faire que require() renvoie la classe avec des types corrects ? #11). Now it is an ordinary CommonJS file. Rétablir le build UMD minifié dist/pokerhand.min.js #19 only has to emit it and add"./dist/pokerhand.min.js"toexports. It needs no.cjstwin and no nestedpackage.json.index.js,src/pokerhand.js,src/utils/*.jsandsrc/arrays/*.jsmust exist as real files for Node < 12.7, which ignoresexportsand loads them as CommonJS anyway. Under"type": "module", newer Node would read the same files as ESM. Each shim would then need a.cjstwin mapped inexports, or a nested{ "type": "commonjs" }. The rootindex.jscan't have a nested one. Now each shim is one CommonJS.jsfile, served to every Node version.package.jsonwould also cost Générer les tables à l'import et déclarer sideEffects: false #33. webpack readssideEffectsfrom the nearestpackage.json. With webpack 4.47, a nesteddist/package.jsonthat holds only{ "type": "commonjs" }made webpack keep a module that the root"sideEffects": falsedrops.What it costs:
tsdown.config.ts,vite.config.tsandcompat/vitest.config.tsbecome.mts, so they stay ES modules. Without the rename, Node warnsMODULE_TYPELESS_PACKAGE_JSONwhen tsdown loads its config, and Vite warns about ESM syntax in a CommonJS file. Thecompatscript points at the new file name.Why
"module"goeswebpack 4 ignores
exports, and it prefersmoduleovermaineven forrequire(). Measured with webpack 4.47 on the packed tarball:1.0.1 had no
modulefield, so with it the Contrat v1 would break for webpack 4 users. Bundlers that readexportsare not affected by the removal.Commits
compat/tests: the class name, mixedimport/require(), and named exports on the class. All of them pass against 1.0.1.require()returns the class.Symbol.formarker.compat/types.test.ts: TypeScript consumer projects.HandRankandCompareResultasas constobjects.exports,typesandengines, withtypesfirst."type": "commonjs", nomodulefield, and the configs renamed to.mts.compat/README.mdtable.enginesinpackage-lock.json.Evidence
Before (
npm ci && npm run build && npm run compaton Update the tooling (tsdown, Vite 8, Vitest 5, TypeScript 7) and add Biome lint + format #34's branch): 34 failed, 48 passed (82).After: 31 failed, 59 passed (90), on Node 22.23.4, 24.4.0 and 26.9.0. No test went from green to red.
The new tests were red at the right step. The mixed
import/require()test failed oninstanceofuntil the marker commit. The three type projects failed withTS2322: Type '"FLUSH"' is not assignable to type 'HandRank'until theas constcommit.COMPAT_TARGET=reference npm run compat: 80 passed, 2 skipped before. 83 passed, 7 skipped after. The class-name and mixedimport/require()tests run against 1.0.1 and pass. The five "new in 1.1.0" tests are skipped.npm run compat:lint(regression 6): red → green.Consumer type check. Double entrée ESM/CJS : comment faire que require() renvoie la classe avec des types corrects ? #11's bench was re-run on the Oxc declarations, outside the repo, with
skipLibCheck: false. Every project usesimport PokerHand = require()where the mode allows it,import PokerHand from,import { PokerHand, HandRank },const r: HandRank = 'FLUSH',Card/Rank/SuitandCompareResult. Each also has@ts-expect-errorlines, so types that collapsed toanywould fail.The node16 CJS/ESM and bundler projects, compiled with the repo's TypeScript 7.0.2, are now
compat/types.test.ts.ES2015.
es-check es6 --checkFeaturespasses ondist/index.cjsand, with--module, ondist/index.mjs. A probe that usesObject.valuesfails it.Old Node. Official darwin-x64 binaries, checked against
SHASUMS256.txt, were run on the packed tarball. The script ranrequire(),.name, the static properties,compareWith, the 1.0.1 error message andrequire('poker-hand-evaluator/package.json'). It ranimporttoo, from 12.22 on.npm test: 45 passed.npm run typecheckandnpm run lintare clean.npm run benchruns, and the Vite config ofnpm run devloads.Emitted files:
Merge Danger
Door: two-way until 1.1.0 is published, then one-way for the public surface.
Nothing is published here, and a revert restores the old build. Once 1.1.0 is out, several things become part of what 1.x must keep: the four static properties,
defaulton both entries, theas consttypes (going back to enums would break code that writes literals, #30), and theSymbol.forkey (#12).Blast Radius: packaging
PokerHand,default,HandRank,CompareResult). In 1.0.1,Object.keys(PokerHand)was[]. They stay enumerable on purpose. Olderimport * asinterop helpers copy only enumerable keys: TypeScript 4.7's__importStarwalks them withfor…in, while 5.9 usesObject.getOwnPropertyNames.importandrequire().compareWithaccepts hands from either copy now, but===between the two classes stays false.compat/vitest.config.mts.compat:lintis green, so CI can gate on it. es-check isn't a dependency yet."type": "commonjs"lets them ship plain.jsCommonJS files. Each path must also be listed inexports, becauseexportsnow closes every path except.and./package.json.tsdown.config.tsorvite.config.tsneed to follow the rename.🤖 Generated with Claude Code