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.
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.

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.
- Inventory components and count inline style usages (at GitHub, about 7,760 uses of the
sxprop) to estimate the actual effort. - For each component, create a variant with CSS Modules and protect it with a feature flag.
- Check visual parity with regression tests and deploy in phases: first the team, then internal staff, and finally all users.
- Maintain a transition layer so that old code continues to work with already migrated components (GitHub used the
@primer/styled-reactpackage). - Migrate dispersed usages with a codemod and, if it helps, with the VS Code plugin that GitHub mentions.
- 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.

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



Comments (0)
No comments yet. You can be the first.
Write a comment