Skip to content

Add CJS + ESM support to the TypeScript and npm package - #438

Merged
Dexus merged 3 commits into
masterfrom
ts-dual-module
Nov 5, 2026
Merged

Dexus merged 3 commits into
masterfrom
ts-dual-module

Conversation

@Dexus

@Dexus Dexus commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Add CJS + ESM support to the TypeScript package

Builds on the merged lib/→TypeScript conversion (#436). Adds dual CommonJS/ESM entry points while keeping one shared runtime instance for both module modes.

What

Commit Change
4b20e69 index.mjs — hand-written ESM wrapper: export default pem plus all 19 named re-exports (createCertificate, createCSR, CA, convert, promisified, …) pulled from the CJS bundle
b0578de package.json — exports map (types → types/pem.d.ts, import → index.mjs, require → dist/index.js), two-pattern ./lib/* deep-import continuation, files + lint glob updated; README ## ESM section
dd818d8 test/esm.spec.js — proves import('pem') and require('pem') return the same module instance (reference identity, not a copy), the full named surface, and a PEM↔DER roundtrip through the ESM entry

Why a wrapper instead of a second build

lib/pem.ts is deliberately script-form (no import/export statements) so the emitted bundle has no live __esModule marker — that's what makes import pem from 'pem' interop work against the CJS bundle. Compiling a real ESM build would change that invariant and split the pem↔ca lazy circular dependency across two module registries. The wrapper keeps both modes on one cached module instance: config() state, the CA class, and openssl settings are identical whether you require or import.

Package resolution after this change

// CommonJS — unchanged
const pem = require('pem')            // → dist/index.js
// ESM — new
import pem from 'pem'                 // → index.mjs (default + named exports)
import { createCertificate } from 'pem'
// Deep imports keep working
import helper from 'pem/lib/helper.js' // and require('pem/lib/helper')

The exports map adds one inherent side effect worth noting: previously reachable but undocumented paths (pem/dist/index.js, pem/types/*) are no longer resolvable on modern Node. Documented deep imports (pem/lib/*) continue to work — verified in probes and from an installed tarball.

Verification

  • Whole-task gate, 14/14 PASS: lint (0 errors), coverage — 135 passing (2 new ESM interop tests), tsd, ncc build reproduces dist/ byte-identically, API-surface equality vs the pre-conversion JS (all 6 modules structurally identical), ESM surface (default identity + 19 named re-exports), deep-import resolution probes, real npm pack → bare-directory install → require + import probes, and frozenness asserts (lib/, types/, yarn.lock untouched in this range).
  • Whole-branch independent review: CLEAN, 0 findings (reviewer re-ran all instruments, verified README examples against the actual surface, and deep-imported pem/lib/ca from a freshly installed tarball).
  • lib/ and types/ are byte-identical to the feat: convert lib/ JavaScript to TypeScript (behavior-identical) #436 tip; diff is exactly index.mjs, package.json, test/esm.spec.js, README.md.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T22:22:30.279971Z dd818d8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Dexus
Dexus merged commit de1826d into master Nov 5, 2026
203 checks passed
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