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

@timfish timfish 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. */
export function isCjs(): boolean {
  /*! rollup-include-cjs-only */
  return true;
  /*! rollup-include-cjs-only-end */

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

let hasWarnedAboutNodeVersion: boolean | undefined;

/**
 * Check if the current Node.js version supports module.register
 */
export function supportsEsmLoaderHooks(): boolean {
  if (isCjs()) {
    return false;
  }
  // ...

The CJS output becomes:

function isCjs() {
  return true;
}
function supportsEsmLoaderHooks() {
  {
    return false;
  }

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!

@timfish timfish changed the title build: Bundle time ESM/CJS code build: Build time ESM/CJS specific code Jun 23, 2026
@timfish timfish requested a review from isaacs June 23, 2026 22:19
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.47 kB - -
@sentry/browser - with treeshaking flags 25.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 flags 74.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/react 29.27 kB - -
@sentry/react (incl. Tracing) 48.28 kB - -
@sentry/vue 32.62 kB - -
@sentry/vue (incl. Tracing) 47.83 kB - -
@sentry/svelte 27.5 kB - -
CDN Bundle 29.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 - uncompressed 88.95 kB - -
CDN Bundle (incl. Tracing) - uncompressed 145.02 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.65 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 148.99 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.62 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.03 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 267.99 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 277.73 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 281.68 kB - -
@sentry/nextjs (client) 50.67 kB - -
@sentry/sveltekit (client) 46.37 kB - -
@sentry/core/server 76.32 kB - -
@sentry/core/browser 63.48 kB - -
@sentry/node-core 61.43 kB -0.32% -195 B 🔽
@sentry/node 123.37 kB -0.2% -240 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.95 kB - -
@sentry/node/light 50.34 kB -0.43% -214 B 🔽
@sentry/node - without tracing 73.49 kB -0.28% -204 B 🔽
@sentry/aws-serverless 84.68 kB -0.25% -212 B 🔽
@sentry/cloudflare (withSentry) - minified 175.71 kB - -
@sentry/cloudflare (withSentry) 437.11 kB - -

View base workflow run

@timfish timfish changed the title build: Build time ESM/CJS specific code build: add rollup plugin for compile-time ESM/CJS code branching Jun 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 owner June 24, 2026 09:06
@timfish timfish requested review from JPeer264 and mydea and removed request for a team June 24, 2026 09:06

@cursor cursor Bot 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 Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ddd1e16. Configure here.

Copy link
Copy Markdown
Collaborator Author

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 Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ddd1e16. Configure here.

Copy link
Copy Markdown
Collaborator Author

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 develop Jun 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 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.

3 participants