Skip to content

Commit 752f5ec

Browse files
quantizorclaude
andauthored
fix: resolve React is not defined ReferenceError in CJS server build (#5668)
* fix: resolve React is not defined ReferenceError in CJS server build (#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]> * chore: clarify changeset rules in AGENTS.md Co-Authored-By: Claude Sonnet 4.6 <[email protected]> --------- Co-authored-by: Claude Sonnet 4.6 <[email protected]>
1 parent 197b64a commit 752f5ec

5 files changed

Lines changed: 23 additions & 13 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'styled-components': patch
3+
---
4+
5+
fix: resolve "React is not defined" ReferenceError introduced in 6.3.10 when loading the CJS build in Node.js

‎AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Mandates
1515

1616
Agent rules
1717

18-
- Create changesets for public-facing changes
18+
- Create changesets only for user-visible changes (bug fixes, features, breaking changes). Skip internal refactors, build tooling, and test changes.
19+
- Changeset descriptions: user impact only, no implementation details
1920
- Do not edit the CHANGELOG.md file, it is auto-generated by changesets
2021
- Always run the formatter (`pnpm --filter styled-components prettier`) before committing code changes
2122

‎packages/styled-components/rollup.config.mjs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ const standaloneBaseConfig = {
141141
plugins: configBase.plugins.concat(
142142
replace({
143143
__SERVER__: JSON.stringify(false),
144-
__IS_RSC__: JSON.stringify(false),
144+
}),
145+
replace({
146+
delimiters: ['', ''],
147+
"typeof React.createContext === 'undefined'": JSON.stringify(false),
145148
})
146149
),
147150
treeshake: {
@@ -181,7 +184,6 @@ const serverConfig = {
181184
plugins: configBase.plugins.concat(
182185
replace({
183186
__SERVER__: JSON.stringify(true),
184-
__IS_RSC__: "typeof React.createContext === 'undefined'",
185187
}),
186188
minifierPlugin
187189
),
@@ -196,7 +198,10 @@ const browserConfig = {
196198
plugins: configBase.plugins.concat(
197199
replace({
198200
__SERVER__: JSON.stringify(false),
199-
__IS_RSC__: JSON.stringify(false),
201+
}),
202+
replace({
203+
delimiters: ['', ''],
204+
"typeof React.createContext === 'undefined'": JSON.stringify(false),
200205
}),
201206
minifierPlugin
202207
),
@@ -217,7 +222,8 @@ const nativeConfig = {
217222
nativeTypescriptPlugin,
218223
...commonPlugins,
219224
replace({
220-
__IS_RSC__: JSON.stringify(false),
225+
delimiters: ['', ''],
226+
"typeof React.createContext === 'undefined'": JSON.stringify(false),
221227
}),
222228
minifierPlugin
223229
],

‎packages/styled-components/src/constants.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
declare let SC_DISABLE_SPEEDY: boolean | null | undefined;
22
declare let __VERSION__: string;
3-
declare const __IS_RSC__: boolean;
43

54
import React from 'react';
65

@@ -18,12 +17,13 @@ export const SPLITTER = '/*!sc*/\n';
1817
export const IS_BROWSER = typeof window !== 'undefined' && typeof document !== 'undefined';
1918

2019
/**
21-
* Detect if we're running in a React Server Component environment.
22-
* RSC environments lack createContext, making this a reliable indicator.
23-
* __IS_RSC__ is replaced at build time: `false` for browser/standalone builds,
24-
* runtime check for server builds.
20+
* True when running in a React Server Component environment (createContext
21+
* is unavailable). In browser / standalone / native builds the entire
22+
* expression is replaced with the literal `false` via rollup-plugin-replace
23+
* with empty delimiters (exact string match), enabling rollup constant
24+
* inlining and terser dead-code elimination for all RSC branches.
2525
*/
26-
export const IS_RSC: boolean = __IS_RSC__;
26+
export const IS_RSC: boolean = typeof React.createContext === 'undefined';
2727

2828
export const DISABLE_SPEEDY = Boolean(
2929
typeof SC_DISABLE_SPEEDY === 'boolean'
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
declare var __SERVER__: boolean;
22
declare var __VERSION__: string;
3-
declare var __IS_RSC__: boolean;
43

54
global.__SERVER__ = typeof document === 'undefined';
65
global.__VERSION__ = 'JEST_MOCK_VERSION';
7-
global.__IS_RSC__ = false;

0 commit comments

Comments
 (0)