fix: bundle ESM-only deps into CJS output with Rollup#1409
Merged
remarkablemark merged 1 commit intoremarkablemark:masterfrom Apr 7, 2026
Merged
fix: bundle ESM-only deps into CJS output with Rollup#1409remarkablemark merged 1 commit intoremarkablemark:masterfrom
remarkablemark merged 1 commit intoremarkablemark:masterfrom
Conversation
The CJS build (`lib/`) was compiled with plain `tsc --module commonjs`, which converts `import` statements to `require()` calls. Since v7, the dependencies (domhandler@6, htmlparser2@12, etc.) are ESM-only (`"type": "module"`), so `require()` fails with `ERR_REQUIRE_ESM` on Node.js < 22 (which lacks native `require(esm)` support). This switches the CJS build from plain `tsc` to Rollup (which already handles the ESM and UMD builds), bundling the ESM-only dependencies into the CJS output. Declaration files are still generated by `tsc`.
d9097e5 to
901f1b4
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1409 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 120 120
Branches 30 30
=========================================
Hits 120 120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
remarkablemark
approved these changes
Apr 7, 2026
Owner
remarkablemark
left a comment
There was a problem hiding this comment.
Thanks so much for the fix @athenacfr!
Owner
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.
What is the motivation for this pull request?
The CJS build (
lib/) shipped in v7.0.0 is broken — itrequire()s dependencies that are now ESM-only.The problem
In v7.0.0, both direct dependencies were upgraded:
domhandlerv5 → v6 (now ESM-only,"type": "module")htmlparser2v10 → v12 (now ESM-only, also pulls ESM-onlydomutils@4,domelementtype@3,entities@8)The CJS build is still compiled with plain
tsc --module commonjs, which convertsimporttorequire(). Since these dependencies no longer have CJS entry points, this fails:Prior to v7, this worked because
domhandler@5andhtmlparser2@10shipped CJS. The package still advertises CJS support via"require": "./lib/index.js"in its exports map.Affected
Not affected
require(esm)enabled by default)The fix
Switch the CJS build from plain
tscto Rollup (which already handles the ESM and UMD builds). Rollup's@rollup/plugin-node-resolveand@rollup/plugin-commonjsplugins bundle the ESM-only dependencies into the CJS output, eliminating the barerequire()calls.Changes:
scripts/build.sh --cjs: generates declaration files withtsc --emitDeclarationOnly, then bundles CJS with Rolluprollup.config.mjs: adds CJS server and client configs (same pattern as the existing ESM configs)All existing tests pass. No API changes.
Alternative: drop CJS support
If the upgrade to ESM-only dependencies was intentional and CJS support is no longer a goal, this PR can be closed in favor of removing CJS entirely:
"require"conditions from the"exports"map inpackage.json"main": "./lib/index.js"build:cjs)"type": "module"topackage.jsonThe current state — advertising CJS via the exports map but shipping a broken CJS build — should be resolved either way.