-
|
I work at a company, Canva, which has a very large CSS modules code base (10k+ CSS Modules files containing 46k+ rule selectors). We are investigating moving from CSS Modules to StyleX. One thing that is currently unclear to me is how we can safely migrate from CSS Modules to StyleX (as a "big bang" migration isn't feasible given the size of the code base). One idea we've had is updating our build pipeline to have the CSS Modules styles built into a lower priority CSS layer. This would effectively ensure that StyleX styles take precedence ahead of CSS Module styles and would allow us to safely perform a top down migration where we can safely migrate React components which do NOT have a If Meta or others are willing to share how they migrated from their old CSS system to StyleX we'd love to hear it |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Meta used something very similar to CSS modules before StyleX and we were able to create a script/codemod that was able to automatically convert ~80% of cases. I think something like that is likely the most effective path forward. I'll try to explain the details I think of, but feel free to ask any follow-up questions. CSS Layers is a good starting point.If you configure your tooling to wrap all your existing styles in a CSS layer, and allow stylex styles to "win", that is a good start to have determinism during the migration. Migrate "large chunks" of files at a timeGenerally speaking if an HTML element is all StyleX or all CSS modules, there should be no style conflict. And so you can start doing codemods one feature or part of the app at a time. Bail out on any complex selectorsYou likely have some usage of descendent and sibling selectors in your CSS files. Configure your codemod to bail out of transformation for any such class. Although Hopefully the vast majority of your styles do not rely on such complex selectors and should work fine. Consider using deprecated APIs to bridge the gapDealing with non-stylex classNamesIf you run into many issues with an automatic codemod, you can consider using some deprecated patterns: You can choose to use OR You can create a helper function to convert arbitrary classNames into something that const toStyleXStyles = (classes: string) => ({
[classes]: classes,
$$css: true
} as any as stylex.StaticStyles);Dealing with StyleX styles being passed to components that don't expect themOne of the core issues with incremental migration is that there may be cases where you start passing stylex styles (which are objects) to a component that still expects strings passed in as You can work around this issue with a small helper as well: const stringy = (styles: stylex.StyleXStyles): stylex.StyleXStyles & string => {
Object.defineProperty(styles, 'toString', {
value: () => stylex.props(styles).className,
writable: false,
enumerable: false,
configurable: false,
});
return styles;
}Now, you can do something like Legacy Media and pseduosStyleX API expects "conditions" like media queries and pseudo classes (like color: {
default: 'black',
':hover': 'red',
}This is more similar to inline CSS than how CSS is often written. So, if you find that you're hitting too many style regressions during the automatic migration, you can use the legacy pattern. StyleX will compile it but give you an ESlint error. color: 'black'
// DEPRECATED PATTERN
':hover': {
color: 'red',
}This pattern may help bridge over situations where you have many classes being passed into components where styles for different viewport sizes are not co-located. The basic pattern for the codemod script would be to
|
Beta Was this translation helpful? Give feedback.
Meta used something very similar to CSS modules before StyleX and we were able to create a script/codemod that was able to automatically convert ~80% of cases.
I think something like that is likely the most effective path forward. I'll try to explain the details I think of, but feel free to ask any follow-up questions.
CSS Layers is a good starting point.
If you configure your tooling to wrap all your existing styles in a CSS layer, and allow stylex styles to "win", that is a good start to have determinism during the migration.
Migrate "large chunks" of files at a time
Generally speaking if an HTML element is all StyleX or all CSS modules, there should be no style conflict. And so you can…