Skip to content

Fix CJS build to actually emit CommonJS#4083

Merged
DmitrySharabin merged 2 commits into
v2from
fix/cjs-build
Jun 2, 2026
Merged

Fix CJS build to actually emit CommonJS#4083
DmitrySharabin merged 2 commits into
v2from
fix/cjs-build

Conversation

@DmitrySharabin
Copy link
Copy Markdown
Member

@DmitrySharabin DmitrySharabin commented Jun 2, 2026

Summary

  • The CJS bundle (dist/cjs/) was byte-for-byte identical to the ESM bundle because the Rollup config never specified format: 'cjs' — both bundles used the default 'es' format
  • This caused require("prismjs") to throw SyntaxError on Node 18–21 (can't require ESM), and return an unexpected ESM namespace object on Node 22+ (need .default to get the Prism instance)
  • Add format: 'cjs' to the CJS output options so Rollup emits require()/exports syntax
  • Add format: 'es' to the ESM output options for clarity
  • Write dist/cjs/package.json with {"type":"commonjs"} after the build — needed because the root "type": "module" would otherwise make Node parse the CJS .js files as ESM. Node uses the nearest parent package.json to determine the module system
  • Add cjsExportPlugin so require("prismjs") returns the Prism instance directly with named exports (Prism, Token, loadLanguages) as properties — no .default needed.

Test plan

  • npm run build completes without errors
  • npm test passes (all 7 suites)
  • npm pack → install in a fresh project → require("prismjs").highlight(...) works (no .default)
  • Named exports accessible: require("prismjs").Token, .Prism, .loadLanguages
  • dist/cjs/index.js contains require()/exports (not import/export)
  • dist/cjs/package.json exists with {"type":"commonjs"}
  • ESM path (import Prism from "prismjs") still works

🤖 Generated with Claude Code

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>
@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 2, 2026

Deploy Preview for dev-prismjs-com ready!

Name Link
🔨 Latest commit 033ac24
🔍 Latest deploy log https://app.netlify.com/projects/dev-prismjs-com/deploys/6a1f4b35885b6900084c9ab6
😎 Deploy Preview https://deploy-preview-4083--dev-prismjs-com.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

No JS Changes

Generated by 🚫 dangerJS against 033ac24

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>
@DmitrySharabin DmitrySharabin merged commit e394eea into v2 Jun 2, 2026
13 of 14 checks passed
@DmitrySharabin DmitrySharabin deleted the fix/cjs-build branch June 2, 2026 21:53
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.

2 participants