Fix CJS build to actually emit CommonJS#4083
Merged
Merged
Conversation
The CJS bundle was identical to the ESM bundle because the Rollup config
did not specify `format: 'cjs'`. This caused `require("prismjs")` to
throw a SyntaxError on Node 18–21, and return an unexpected ESM namespace
object on Node 22+.
- Add `format: 'cjs'` to the CJS output options
- Add `format: 'es'` to the ESM output options for clarity
- Write `dist/cjs/package.json` with `{"type":"commonjs"}` so Node
parses the CJS files correctly despite the root `"type": "module"`
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
✅ Deploy Preview for dev-prismjs-com ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Add a Rollup plugin that appends a one-liner to CJS chunks with mixed
default + named exports, making module.exports the default with named
exports as properties (the Axios pattern). Without this, CJS consumers
would need require("prismjs").default.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
34b10b0 to
033ac24
Compare
LeaVerou
approved these changes
Jun 2, 2026
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.
Summary
dist/cjs/) was byte-for-byte identical to the ESM bundle because the Rollup config never specifiedformat: 'cjs'— both bundles used the default'es'formatrequire("prismjs")to throwSyntaxErroron Node 18–21 (can'trequireESM), and return an unexpected ESM namespace object on Node 22+ (need.defaultto get the Prism instance)format: 'cjs'to the CJS output options so Rollup emitsrequire()/exportssyntaxformat: 'es'to the ESM output options for claritydist/cjs/package.jsonwith{"type":"commonjs"}after the build — needed because the root"type": "module"would otherwise make Node parse the CJS.jsfiles as ESM. Node uses the nearest parentpackage.jsonto determine the module systemcjsExportPluginsorequire("prismjs")returns the Prism instance directly with named exports (Prism,Token,loadLanguages) as properties — no.defaultneeded.Test plan
npm run buildcompletes without errorsnpm testpasses (all 7 suites)npm pack→ install in a fresh project →require("prismjs").highlight(...)works (no.default)require("prismjs").Token,.Prism,.loadLanguagesdist/cjs/index.jscontainsrequire()/exports(notimport/export)dist/cjs/package.jsonexists with{"type":"commonjs"}import Prism from "prismjs") still works🤖 Generated with Claude Code