Skip to content

fix: resolve React is not defined ReferenceError in CJS server build - #5668

Merged
quantizor merged 2 commits into
mainfrom
5667
Feb 19, 2026
Merged

quantizor merged 2 commits into
mainfrom
5667

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

TypeScript's import-elision removed import React from 'react' in constants.ts because React was never referenced in any TypeScript expression — only in the rollup replace plugin's injected string typeof React.createContext === 'undefined' which ran after TS.

Fix: write IS_RSC as a direct TS expression so the import is preserved. Browser/standalone/native builds replace the whole expression with the literal false via rollup replace, keeping dead-code elimination intact.

Fixes #5667

@changeset-bot

changeset-bot Bot commented Feb 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f57bf8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI 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.

Pull request overview

This PR fixes a critical "React is not defined" ReferenceError that occurred in CJS server builds introduced in version 6.3.10. The issue stemmed from TypeScript's import-elision removing the import React from 'react' statement because React was only referenced in a string injected by rollup's replace plugin after TypeScript compilation, not in any actual TypeScript expression.

Changes:

  • Replaced the __IS_RSC__ build-time constant with a direct TypeScript expression typeof React.createContext === 'undefined' to prevent import elision
  • Updated rollup configurations to replace the entire expression string with false in browser/standalone/native builds
  • Removed __IS_RSC__ declarations from test globals since it's no longer a build-time injected constant

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
packages/styled-components/src/constants.ts Changed IS_RSC from using __IS_RSC__ constant to direct expression checking React.createContext, ensuring React import is preserved
packages/styled-components/rollup.config.mjs Updated all build configs (standalone, browser, native) to replace the expression string instead of __IS_RSC__ constant; server config correctly omits replacement for runtime check
packages/styled-components/src/test/globals.ts Removed __IS_RSC__ global declarations no longer needed since IS_RSC is now a direct expression
.changeset/fix-rsc-react-not-defined.md Added changeset documenting the fix and explaining the root cause

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

quantizor and others added 2 commits February 19, 2026 15:32
…5667)

TypeScript's import-elision removed `import React from 'react'` in
constants.ts because React was never referenced in any TypeScript
expression — only in the rollup replace string injected after TS ran.

Write IS_RSC as a direct TS expression so the import is preserved.
Use rollup-plugin-replace with `delimiters: ['', '']` (exact match) for
browser/standalone/native builds to substitute the literal `false`,
keeping dead-code elimination intact. Server build gets the correct
runtime check at no extra cost.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@quantizor
quantizor merged commit 752f5ec into main Feb 19, 2026
5 checks passed
@quantizor
quantizor deleted the 5667 branch February 19, 2026 20:38
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.

Error in version 6.3.10: ReferenceError: React is not defined

2 participants