Repository navigation
Load dist/cjs as CommonJS, and smoke-test the packed entry points in CI - #125
Open
roger-datocms wants to merge 4 commits into
Open
roger-datocms wants to merge 4 commits into
roger-datocms wants to merge 4 commits into
Conversation
The root "type": "module" made Node parse dist/cjs as ESM, so every `require` entry point threw. A nested dist/cjs/package.json with "type": "commonjs" fixes it. The new CI job packs the library and loads every entry point with `require` and `import`. Fixes datocms#124 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
(Human here)
This is actually just a one-line fix, a nested
package.jsonwith:"type": "commonjs"for the CJS build.The rest is just test stuff and a Node script to copy the package.json into the build after Typescript compilation. This repo doesn't have
tsupetc. so we have to do it manually.(Claude here [Opus 5.5])
Fixes #124.
Since v4.1.1, the root
"type": "module"has made Node parse the CommonJS build indist/cjsas ESM, sorequire("react-datocms")and everyrequireof a subpath throwReferenceError: exports is not defined in ES module scope.importand bundled apps were never affected.Changes
tsc:cjs,scripts/copy-cjs-package-json.mjscopiesscripts/cjs-package.json({"type": "commonjs"}) todist/cjs/package.json. Node uses the nearestpackage.jsonto decide a.jsfile's format, so that folder loads as CommonJS again. This is the same marker tshy writes into its CommonJS build.npm run test:package(scripts/test-package.mjs): builds, packs the tarball, installs it into a throwaway consumer, and loads everyexportsentry point withrequireandimport. It reads the entry points frompackage.json, so new subpaths are covered automatically.packageCI job runs it on the.nvmrcNode version. The existing job only runsnpm testagainstsrc, which is how this shipped unnoticed.Before / after
node scripts/test-package.mjson Node 24.12.0:Before (marker file removed):
After:
npm test: 6 suites, 54 tests, 42 snapshots pass.Checked for regressions
Comparing packed tarballs with and without the fix: the
importpath is unchanged. Thedist/esmanddist/typesfolders are identical, and built output is byte-identical for Next.js 16 (Turbopack and webpack), Vite (examples/), webpack, esbuild and rollup. TypeScript resolution results are the same under node16, nodenext, bundler and node10.Known limits (not regressions)
requireof the root and/video-playerstill fails, because@mux/mux-player-react/lazyonly ships ESM and those versions can'trequire()ESM. Every supported Node line handles it.module: node16/nodenextstill get TS1479, because the types only live in the ESM-flaggeddist/types.TODO later
enginesfrom>=8.0.0to what actually works (e.g.>=20.19), in the next major..cjsfiles with tsup/tsdown (as@datocms/content-linkdoes) or ship ESM only, now that every supported Node canrequire()ESM.🤖 Generated with Claude Code