Repository navigation
fix(native): replace postcss, optimize render path (#5705) - #5706
Conversation
🦋 Changeset detectedLatest commit: 34427c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
8f940d0 to
f4e3809
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses React Native / react-native-web stability and performance in styled-components by removing postcss from the native inline-style path, introducing a custom CSS declaration parser with shared caching, and updating RN component alias coverage.
Changes:
- Replaced
postcssparsing in the native inline-style pipeline with a lightweight declaration parser + sharedcssToStyleObject()cache. - Modernized
StyledNativeComponentrender hot-path with a cache-first approach to skip context/style generation on cache hits. - Updated native component aliases and added extensive parser tests + new bench targets (web + native).
Reviewed changes
Copilot reviewed 15 out of 17 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pnpm-workspace.yaml | Adds packages/styled-components/native as a workspace package. |
| pnpm-lock.yaml | Updates lockfile to reflect dependency graph changes (postcss removal, native workspace package deps). |
| packages/styled-components/src/native/index.ts | Switches toStyleSheet() to use shared cssToStyleObject() cache and updates RN alias list. |
| packages/styled-components/src/models/test/InlineStyle.test.ts | Adds comprehensive unit tests for the new CSS declaration parser. |
| packages/styled-components/src/models/StyledNativeComponent.ts | Refactors native render path with memoized/cache-first behavior and helper extraction. |
| packages/styled-components/src/models/InlineStyle.ts | Removes postcss usage; adds comment stripping + declaration parsing + shared cache helper. |
| packages/styled-components/src/bench/web.test.js | Adds integrated web benchmark suite for stress/perf tracking. |
| packages/styled-components/src/bench/native.test.tsx | Adds native parser/cache and render benchmarks. |
| packages/styled-components/package.json | Removes postcss; splits benchmark scripts into web + native. |
| packages/styled-components/native/package.json | Adds dev deps for RN typing/runtime in the native subpath package. |
| packages/styled-components/jest.config.bench-native.js | Introduces a RN-preset Jest config for native benchmarks. |
| packages/benchmarks/src/implementations/styled-components-native/index.js | Adds a native implementation entry for the benchmarks package. |
| packages/benchmarks/src/implementations/styled-components-native/Provider.js | Adds provider wrapper for the native benchmarks implementation. |
| packages/benchmarks/src/implementations/styled-components-native/Dot.js | Adds native-styled Dot component for benchmarks. |
| packages/benchmarks/src/implementations/styled-components-native/Box.js | Adds native-styled Box component for benchmarks. |
| AGENTS.md | Documents new benchmark commands (web + native). |
| .changeset/react-native-improvements.md | Adds a patch changeset describing RN improvements and dependency removals. |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Replace postcss with lightweight CSS declaration parser (fixes nanoid crash in Expo/Metro react-native-web). Quote-aware comment stripping, backslash escapes, invalid CSS recovery. 4-6x faster than postcss. - Cache-first render in StyledNativeComponent: skip resolveContext + generateStyleObject on cache hit (2.6-3.2x faster parent re-renders). - Extract cssToStyleObject as shared cached function for InlineStyle and toStyleSheet. Warn on selectors/at-rules (parity with postcss). - Update native aliases: remove 5 dead, add 4 missing RN components. - Remove postcss + nanoid + source-map-js from production dependencies. - 137 parser tests including adversarial inputs from council review.
- Add native.test.tsx: parser throughput, cache hit/miss, full component render (single, parent re-render, cycling colors, siblings at scale) - Add styled-components/native + RNW benchmark implementation - bench:native script via react-native jest preset - bench now runs both web + native; bench:web for web only - Rename micro.test.js to web.test.js
f4e3809 to
e76e8e6
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 23 out of 25 changed files in this pull request and generated 3 comments.
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…rser
Multi-declaration selector/at-rule blocks (e.g. `.foo { color: red;
font-size: 12px; }`) previously only caught the first declaration—
subsequent ones inside the block were emitted as valid. Now tracks
brace depth to skip the entire block and recover after the closing `}`.
Also restricts bench:web config to web*.test.* to prevent native
benchmarks from running under the jsdom preset.
Summary
Fixes #5705 —
postcss'snanoiddependency crashes at import time in Expo/Metro (react-native-web), preventing all styling from working.This PR replaces postcss with a purpose-built CSS declaration parser, optimizes the native render path, and modernizes the React Native integration.
Parser replacement
postcsswithparseCSSDeclarations— a ~80-line parser that handles quotes, nested parentheses, CSS comments, backslash escapes, stray semicolons, and data URIsstripComments) preserves/* */inside quoted strings andurl()argumentspostcss+nanoid+source-map-jsfrom production dependenciesRender path optimization
StyledNativeComponent: skipsresolveContext+generateStyleObjectentirely when props and theme haven't changedprops+theme(inputs) instead of derivedcontext— matches the webStyledComponentpatternresolvedAttrsallocation,buildPropsForElementiterates context directlytoStyleSheet()now shares thecssToStyleObjectcache withInlineStyle.generateStyleObjectModernization
native/is now a pnpm workspace member with its own dev dependenciesbench:nativescript;benchnow runs both web and native benchmarksBenchmarks vs 6.3.12
Parser: 4-6x faster
Render: 2.6-3.2x faster re-renders
First renders and cache-miss scenarios are ~1x because
css-to-react-native'stransformDeclPairsis now the bottleneck, not parsing.Tests
styled-components-nativebenchmark implementation exercising the exact SC/native + RNW import path from v6.3.* causing crashes on react-native-web - nanoid is not a function #5705