Skip to content

fix(native): replace postcss, optimize render path (#5705) - #5706

Merged
quantizor merged 5 commits into
mainfrom
fix/react-native-improvements
Mar 28, 2026
Merged

quantizor merged 5 commits into
mainfrom
fix/react-native-improvements

Conversation

@quantizor

@quantizor quantizor commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #5705 — postcss's nanoid dependency 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

  • Replaced postcss with parseCSSDeclarations — a ~80-line parser that handles quotes, nested parentheses, CSS comments, backslash escapes, stray semicolons, and data URIs
  • Quote-aware comment stripping (stripComments) preserves /* */ inside quoted strings and url() arguments
  • Invalid CSS (unclosed quotes, unclosed parentheses) is dropped with a dev warning, and subsequent valid declarations are recovered
  • Warns on selectors and at-rules leaking into the native path (parity with postcss behavior)
  • Removes postcss + nanoid + source-map-js from production dependencies

Render path optimization

  • Cache-first render pattern in StyledNativeComponent: skips resolveContext + generateStyleObject entirely when props and theme haven't changed
  • Cache keyed on props + theme (inputs) instead of derived context — matches the web StyledComponent pattern
  • Eliminated double object spread: removed separate resolvedAttrs allocation, buildPropsForElement iterates context directly
  • toStyleSheet() now shares the cssToStyleObject cache with InlineStyle.generateStyleObject

Modernization

  • Updated native component alias list: removed 5 components no longer in React Native (DatePickerIOS, DrawerLayoutAndroid, ProgressBarAndroid, ProgressViewIOS, Slider), added 4 missing ones (InputAccessoryView, StatusBar, TouchableNativeFeedback, TouchableWithoutFeedback)
  • native/ is now a pnpm workspace member with its own dev dependencies
  • Added bench:native script; bench now runs both web and native benchmarks

Benchmarks vs 6.3.12

Parser: 4-6x faster

Benchmark 6.3.12 (postcss) This PR Speedup
simple (3 decls) 311ms 49ms 6.3x
medium (10 decls) 850ms 172ms 4.9x
complex (20 decls) 1,547ms 375ms 4.1x
with comments 698ms 152ms 4.6x

Render: 2.6-3.2x faster re-renders

Benchmark 6.3.12 This PR Speedup
50 static children, parent re-render 620ms 238ms 2.6x
50 dynamic children, parent re-render 779ms 246ms 3.2x
First render (single component) ~67ms ~67ms ~1x
Cache-miss cycling (50 children, 30 colors) ~317ms ~328ms ~1x

First renders and cache-miss scenarios are ~1x because css-to-react-native's transformDeclPairs is now the bottleneck, not parsing.

Tests

  • 137 parser unit tests covering all RN CSS patterns (colors, transforms, filters, shadows, gradients, custom properties, data URIs) and adversarial inputs (broken interpolations, double semicolons, unterminated quotes, unclosed parens, backslash escapes, unicode, null bytes, nested comments)
  • Native render benchmarks (single component, parent re-render, cycling colors, siblings at scale)
  • styled-components-native benchmark implementation exercising the exact SC/native + RNW import path from v6.3.* causing crashes on react-native-web - nanoid is not a function #5705

@changeset-bot

changeset-bot Bot commented Mar 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 34427c1

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

@quantizor
quantizor force-pushed the fix/react-native-improvements branch from 8f940d0 to f4e3809 Compare March 28, 2026 02:26
@quantizor
quantizor requested a review from Copilot March 28, 2026 02:26
@quantizor quantizor changed the title fix(native): replace postcss, modernize RN path fix(native): replace postcss, optimize render path (#5705) Mar 28, 2026

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 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 postcss parsing in the native inline-style pipeline with a lightweight declaration parser + shared cssToStyleObject() cache.
  • Modernized StyledNativeComponent render 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

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

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.

Comment thread packages/styled-components/package.json Resolved
…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.
@quantizor
quantizor merged commit 89d4c40 into main Mar 28, 2026
5 checks passed
@quantizor
quantizor deleted the fix/react-native-improvements branch March 28, 2026 02:55
@quantizor quantizor mentioned this pull request Mar 28, 2026
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.

v6.3.* causing crashes on react-native-web - nanoid is not a function

2 participants