Skip to content
ES EN
Theme: automatic

Guide: Migrating from styled-components to CSS Modules, GitHub's Way

A practical summary of GitHub's method for switching from CSS-in-JS to CSS Modules without breaking anything: flags, codemods, visual testing, and what to expect.

Software engineering

GitHub shared in its technical article how it moved away from CSS-in-JS in three years and switched to CSS Modules, achieving 55% less server-side rendering time and 25% faster component initialization. Here, we summarize their method so you can adapt it to your own project, though we can't promise the same numbers.

The root of the problem was the growing number of components per page in 2023: with styled-components, initial load, server-side rendering, and the cost of updating styles all increased with that number. With CSS Modules, styles end up in static stylesheets that travel with the HTML, eliminating runtime styling work.

Conceptual diagram of the transition from runtime-generated styles to static CSS stylesheets served with HTML
The underlying idea: pay the styling cost once, at compile time.

What you need

Before touching anything, it's wise to have four things ready: a feature flag system to activate each variant in phases, visual regression tests to verify that the appearance doesn't change, a codemod or script for repetitive conversions, and a plan to decouple themes. GitHub had to separate the management of its seven themes and high-contrast variants before completely removing the library.

  1. Inventory components and count inline style usages (at GitHub, about 7,760 uses of the sx prop) to estimate the actual effort.
  2. For each component, create a variant with CSS Modules and protect it with a feature flag.
  3. Check visual parity with regression tests and deploy in phases: first the team, then internal staff, and finally all users.
  4. Maintain a transition layer so that old code continues to work with already migrated components (GitHub used the @primer/styled-react package).
  5. Migrate dispersed usages with a codemod and, if it helps, with the VS Code plugin that GitHub mentions.
  6. When no usages remain, remove the library and the transition layer.

What a conversion looks like

The typical change consists of replacing the inline style prop with a class imported from a .module.css file. This is the example shown in the InfoQ note itself:

// Before
import { Button } from '@primer/styled-react';
<Button sx={{ bg: 'accent.emphasis', px: 3 }}>Pay</Button>

// After
import { Button } from '@primer/react';
import styles from './PayButton.module.css';
<Button className={styles.pay}>Pay</Button>

Pace and expectations

The first 6,419 usages were migrated by a rotation of eight engineers in six months, with rendering improvements of between 3% and 22% depending on the page. The last 895 were resolved in three weeks with two engineers, supported by Copilot agents. In other words: the hard part is starting and defining the pattern; the rest accelerates with automation. Keep in mind that the environment also pushed for this: styled-components entered maintenance mode.

Bar chart comparing server-side rendering time before and after migrating to static stylesheets
Rendering improvement: GitHub reports 55% less server time.
Tip: CSS Modules isn't the only way out. A comment on Hacker News reminds us that CSS-in-JS can be zero-cost at runtime without changing approach, and other teams have opted for StyleX (like Linear) or vanilla-extract. Measure before deciding.

A final warning: some commentators noted that hashed class names make browser extensions, ad blockers, and custom CSS more difficult. If your product relies on third parties applying styles to it, consider that cost before migrating.

Original source: infoq.com

Article generated with AI.larebelion

Kernel

· Head of Technology · Spain

«The valuable part of this migration isn't the 55%, but the method: going slowly, with flags and tests, and automating repetitive tasks.»

Comments (0)

No comments yet. You can be the first.

Write a comment