Skip to content

Commit 1203f80

Browse files
authored
fix: remove DOM code from React Native build (#5677)
Replace `instanceof Keyframes` with a branded symbol check and convert value imports to `import type` in flatten.ts. This prevents rollup from bundling the entire DOM sheet stack (Sheet → Tag → dom → Rehydration) into the native build, fixing crashes on RN 0.79+ where Hermes fails at module evaluation when encountering `document` references. Closes #5610
1 parent 6ac911d commit 1203f80

4 files changed

Lines changed: 21 additions & 4 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 React Native crash caused by `document` references in the native build. The native bundle no longer includes DOM code, resolving compatibility with RN 0.79+ and Hermes.

‎packages/styled-components/src/models/Keyframes.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import StyleSheet from '../sheet';
22
import { Keyframes as KeyframesType, Stringifier } from '../types';
33
import styledError from '../utils/error';
4+
import { KEYFRAMES_SYMBOL } from '../utils/isKeyframes';
45
import { setToString } from '../utils/setToString';
56
import { mainStylis } from './StyleSheetManager';
67

78
export default class Keyframes implements KeyframesType {
9+
readonly [KEYFRAMES_SYMBOL] = true as const;
10+
811
id: string;
912
name: string;
1013
rules: string;

‎packages/styled-components/src/utils/flatten.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import Keyframes from '../models/Keyframes';
2-
import StyleSheet from '../sheet';
1+
import type StyleSheet from '../sheet';
32
import {
43
AnyComponent,
54
Dict,
@@ -14,6 +13,7 @@ import addUnitIfNeeded from './addUnitIfNeeded';
1413
import getComponentName from './getComponentName';
1514
import hyphenate from './hyphenateStyleName';
1615
import isFunction from './isFunction';
16+
import isKeyframes from './isKeyframes';
1717
import isPlainObject from './isPlainObject';
1818
import isStatelessFunction from './isStatelessFunction';
1919
import isStyledComponent from './isStyledComponent';
@@ -75,7 +75,7 @@ export default function flatten<Props extends object>(
7575
process.env.NODE_ENV !== 'production' &&
7676
typeof fnResult === 'object' &&
7777
!Array.isArray(fnResult) &&
78-
!(fnResult instanceof Keyframes) &&
78+
!isKeyframes(fnResult) &&
7979
!isPlainObject(fnResult) &&
8080
fnResult !== null
8181
) {
@@ -93,7 +93,7 @@ export default function flatten<Props extends object>(
9393
}
9494
}
9595

96-
if (chunk instanceof Keyframes) {
96+
if (isKeyframes(chunk)) {
9797
if (styleSheet) {
9898
chunk.inject(styleSheet, stylisInstance);
9999
result.push(chunk.getName(stylisInstance));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type KeyframesClass from '../models/Keyframes';
2+
3+
const KEYFRAMES_SYMBOL = Symbol.for('sc-keyframes');
4+
5+
export default function isKeyframes(value: unknown): value is KeyframesClass {
6+
return typeof value === 'object' && value !== null && KEYFRAMES_SYMBOL in value;
7+
}
8+
9+
export { KEYFRAMES_SYMBOL };

0 commit comments

Comments
 (0)