|
| 1 | +# Build Architecture |
| 2 | + |
| 3 | +How the package is built, which environment flags exist, and how CSS injection order is decided. |
| 4 | + |
| 5 | +## Environment flags |
| 6 | + |
| 7 | +Three separate mechanisms answer "are we on a server", and they are not interchangeable. |
| 8 | + |
| 9 | +`__SERVER__` is a build-time constant. Rollup replaces it per bundle, so a branch behind it is |
| 10 | +eliminated entirely from the browser build. It is the right gate for dead-code elimination and the |
| 11 | +wrong gate for anything needing paired cleanup: Jest resolves the server build through the `main` |
| 12 | +field in jsdom, where `__SERVER__` is `true`, so a `useLayoutEffect` gated on it is eliminated while |
| 13 | +the DOM mutations it was meant to clean up still happen. Gate cleanup on `styleSheet.server` or |
| 14 | +`IS_RSC` instead. |
| 15 | + |
| 16 | +`IS_RSC` is `typeof React.createContext === 'undefined'`, evaluated once at module level. React 19's |
| 17 | +`react-server` export condition serves a build stripped of `createContext`, so `IS_RSC` is `true` at |
| 18 | +runtime inside server components; bundlers such as Next.js and Turbopack select that condition |
| 19 | +automatically. In browser, standalone and native builds rollup replaces the expression with `false` |
| 20 | +for dead-code elimination. Because it is module-level rather than per-render, gating a hook behind it |
| 21 | +does not violate the rules of hooks. |
| 22 | + |
| 23 | +`styleSheet.server` is a runtime flag set by `ServerStyleSheet`. It exists because Turbopack resolves |
| 24 | +the `browser` entry when server-rendering client components, which makes `__SERVER__` false on the |
| 25 | +server; this flag is the fallback that still reports the truth there. |
| 26 | + |
| 27 | +Combine all three (`__SERVER__ || IS_RSC || ssc.styleSheet.server`) with one deliberate exception: |
| 28 | +`createGlobalStyle`'s server-cache cleanup drops the `IS_RSC` term, because the `IS_RSC` branch above |
| 29 | +it has already handled the render and returned, so reaching that line under RSC means there was |
| 30 | +nothing left to clean. |
| 31 | + |
| 32 | +`IS_BROWSER` is `typeof window !== 'undefined' && typeof document !== 'undefined'`. Both halves are |
| 33 | +needed: a non-browser host can define `window` without a DOM. It is a runtime check, so bundlers |
| 34 | +cannot tree-shake code behind it and it never substitutes for `__SERVER__`. |
| 35 | + |
| 36 | +`React.useRef` is `undefined` in RSC server components. Gate it behind `__SERVER__` so the call is |
| 37 | +eliminated; never write `typeof React.useRef === 'function'`, which is a runtime conditional hook. |
| 38 | + |
| 39 | +## Native entry isolation |
| 40 | + |
| 41 | +The native entry must never transitively import DOM code through a value import. Use `import type`, |
| 42 | +and branded `Symbol.for()` checks in place of `instanceof`. React Native on Hermes 0.79+ fails at |
| 43 | +module evaluation time on a bare `document` reference, so the failure is a hard crash rather than a |
| 44 | +degraded path. |
| 45 | + |
| 46 | +Verify with `grep -c 'document\.' native/dist/styled-components.native.cjs.js`, which must report 0. |
| 47 | + |
| 48 | +## Module resolution |
| 49 | + |
| 50 | +The `browser` field in `package.json` maps server bundles to their browser-specific alternatives. It |
| 51 | +is preferred over `exports`, which caused TS2742 in composite TypeScript projects. |
| 52 | + |
| 53 | +## CSS injection ordering |
| 54 | + |
| 55 | +Group IDs are allocated at call time, when `styled()`, `createGlobalStyle()` or `keyframes()` runs, |
| 56 | +and a lower ID sorts earlier in the stylesheet. |
| 57 | + |
| 58 | +Keyframes register eagerly via `getGroupForId(this.id)` in the constructor rather than through |
| 59 | +`StyleSheet.registerId`, which would pull DOM imports into native builds. |
0 commit comments