Skip to content

build: add rollup plugin for compile-time ESM/CJS code branching - #21715

Merged
timfish merged 5 commits into
developfrom
timfish/build/bundle-time-cjs-esm
Jun 24, 2026
Merged

build: add rollup plugin for compile-time ESM/CJS code branching#21715
timfish merged 5 commits into
developfrom
timfish/build/bundle-time-cjs-esm

Conversation

@timfish

@timfishtimfish commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

This PR adds a rollup plugin that strips comment-annotated blocks at build time, letting us write ESM- and CJS-specific code in a single source file without runtime format checks.

That means this for this file:

/** Detect CommonJS. */exportfunctionisCjs(): boolean{/*! rollup-include-cjs-only */returntrue;/*! rollup-include-cjs-only-end *//*! rollup-include-esm-only */returnfalse;/*! rollup-include-esm-only-end */}lethasWarnedAboutNodeVersion: boolean|undefined;/** * Check if the current Node.js version supports module.register */exportfunctionsupportsEsmLoaderHooks(): boolean{if(isCjs()){returnfalse;}// ...

The CJS output becomes:

functionisCjs(){returntrue;}functionsupportsEsmLoaderHooks(){{returnfalse;}

This allows us to include or exclude ESM or CJS specific code without resorting to checking globals at runtime. It changes a runtime check into a build time constant.

There is one caveat. Your code must be valid TypeScript and TypeScript doesn't understand the annotations!

@timfishtimfish changed the title build: Bundle time ESM/CJS codebuild: Build time ESM/CJS specific codeJun 23, 2026
@timfish
timfish requested a review from isaacsJune 23, 2026 22:19
@github-actions

github-actionsBot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize% ChangeChange
@sentry/browser27.47 kB--
@sentry/browser - with treeshaking flags25.91 kB--
@sentry/browser (incl. Tracing)45.96 kB--
@sentry/browser (incl. Tracing + Span Streaming)47.72 kB--
@sentry/browser (incl. Tracing, Profiling)50.75 kB--
@sentry/browser (incl. Tracing, Replay)85.21 kB--
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags74.81 kB--
@sentry/browser (incl. Tracing, Replay with Canvas)89.91 kB--
@sentry/browser (incl. Tracing, Replay, Feedback)102.57 kB--
@sentry/browser (incl. Feedback)44.66 kB--
@sentry/browser (incl. sendFeedback)32.26 kB--
@sentry/browser (incl. FeedbackAsync)37.4 kB--
@sentry/browser (incl. Metrics)28.54 kB--
@sentry/browser (incl. Logs)28.78 kB--
@sentry/browser (incl. Metrics & Logs)29.47 kB--
@sentry/react29.27 kB--
@sentry/react (incl. Tracing)48.28 kB--
@sentry/vue32.62 kB--
@sentry/vue (incl. Tracing)47.83 kB--
@sentry/svelte27.5 kB--
CDN Bundle29.88 kB--
CDN Bundle (incl. Tracing)47.9 kB--
CDN Bundle (incl. Logs, Metrics)31.44 kB--
CDN Bundle (incl. Tracing, Logs, Metrics)49.24 kB--
CDN Bundle (incl. Replay, Logs, Metrics)70.77 kB--
CDN Bundle (incl. Tracing, Replay)85.41 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics)86.69 kB--
CDN Bundle (incl. Tracing, Replay, Feedback)91.2 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics)92.45 kB--
CDN Bundle - uncompressed88.95 kB--
CDN Bundle (incl. Tracing) - uncompressed145.02 kB--
CDN Bundle (incl. Logs, Metrics) - uncompressed93.65 kB--
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed148.99 kB--
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed218.62 kB--
CDN Bundle (incl. Tracing, Replay) - uncompressed264.03 kB--
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed267.99 kB--
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed277.73 kB--
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed281.68 kB--
@sentry/nextjs (client)50.67 kB--
@sentry/sveltekit (client)46.37 kB--
@sentry/core/server76.32 kB--
@sentry/core/browser63.48 kB--
@sentry/node-core61.43 kB-0.32%-195 B 🔽
@sentry/node123.37 kB-0.2%-240 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection)69.95 kB--
@sentry/node/light50.34 kB-0.43%-214 B 🔽
@sentry/node - without tracing73.49 kB-0.28%-204 B 🔽
@sentry/aws-serverless84.68 kB-0.25%-212 B 🔽
@sentry/cloudflare (withSentry) - minified175.71 kB--
@sentry/cloudflare (withSentry)437.11 kB--

View base workflow run

@timfishtimfish changed the title build: Build time ESM/CJS specific codebuild: add rollup plugin for compile-time ESM/CJS code branchingJun 24, 2026
@timfish
timfish marked this pull request as ready for review June 24, 2026 09:06
@timfish
timfish requested a review from a team as a code ownerJune 24, 2026 09:06
@timfish
timfish requested review from JPeer264 and mydea and removed request for a teamJune 24, 2026 09:06

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ddd1e16. Configure here.


/*! rollup-include-esm-only */
return false;
/*! rollup-include-esm-only-end */

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source isCjs always true

Medium Severity

Replacing runtime detection with two unconditional return statements means any execution of the TypeScript source without the Rollup strip step always hits the first return true. Unit tests and other tooling that import from src (for example Vitest in packages/node-core) no longer mirror ESM behavior for isCjs() or callers like supportsEsmLoaderHooks().

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit ddd1e16. Configure here.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the PR. These blocks are removed at build time.

name: 'remove-esm-cjs-mode-blocks',
transform(code) {
if (!code.includes('rollup-include-')) return null;
return { code: code.replace(removeBlock, '').replace(stripMarkers, ''), map: null };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESM/CJS strip runs after esbuild

Medium Severity

The new remove-esm-cjs-mode-blocks transform is not pinned before esbuild in mergePlugins, unlike remove-dev-mode-blocks. Format-specific blocks are stripped only after transpilation, so top-level imports used solely in a removed branch can remain in the module graph and ship in the wrong format bundle.

Fix in CursorFix in Web

Reviewed by Cursor Bugbot for commit ddd1e16. Configure here.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not using esbuild

@timfish
timfish merged commit e2d77d9 into developJun 24, 2026
554 of 556 checks passed
@timfish
timfish deleted the timfish/build/bundle-time-cjs-esm branch June 24, 2026 12:52
Sign up for freeto 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.

3 participants

@timfish@mydea@JPeer264