When Facebook shipped its redesign in April 2019, the rebuild leaned on a stack the company had largely open-sourced: React, GraphQL, Relay, and an internal CSS-in-JS library called stylex. Because stylex itself remains internal, its publicly shared lessons made an open-source implementation possible — style9.

What sets style9 apart

style9 delivers the usual CSS-in-JS benefits — scoped selectors, dead code elimination, deterministic resolution, and shared values between CSS and JavaScript — but two properties distinguish it from existing libraries.

Compile-time extraction, tiny runtime

Styles are authored in JavaScript but pulled out by the compiler into a plain CSS file, so no style declarations ship in the final JavaScript. Only the resulting class names remain, and a minimal runtime applies them conditionally as you normally would. The payoff is smaller bundles, lower memory usage, and faster rendering.

Extraction at compile time rules out genuinely dynamic values, which are uncommon and unharmed by being defined inline. Conditional styles are supported, as are local constants and mathematical expressions, via Babel's path.evaluate.

Atomic classes

Every declaration becomes its own single-property class. If opacity: 0 appears in several places, it lands in the generated CSS once. The stylesheet therefore grows with the number of unique declarations rather than total declarations, which can shrink output dramatically: Facebook's old homepage used 413 KB of gzipped CSS, while the redesign uses 74 KB for all pages.

Slide from Building the New Facebook with React and Relay by Frank Yan, at 13:23 showing the logarithmic scale of atomic CSS.

This approach produces non-semantic, opaque class names and ignores the cascade — treating CSS as a compilation target. That trade is intentional: questioning assumed best practices here improves both user and developer experience. The library also offers TypeScript-typed styles, unused style elimination, JavaScript variables, and support for media queries, pseudo-selectors, and keyframes.

Installation and setup

Install the package as usual:

npm install style9

Plugins exist for Rollup, Webpack, Gatsby, and Next.js, all built on a Babel plugin; usage instructions live in the repository. The Webpack plugin used below looks like this:

const Style9Plugin = require('style9/webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  module: {
    rules: [
      // This will transform the style9 calls
      {
        test: /\.(tsx|ts|js|mjs|jsx)$/,
        use: Style9Plugin.loader
      },
      // This is part of the normal Webpack CSS extraction
      {
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  plugins: [
    // This will sort and remove duplicate declarations in the final CSS file
    new Style9Plugin(),
    // This is part of the normal Webpack CSS extraction
    new MiniCssExtractPlugin()
  ]
};

Authoring styles

Styles are created with style9.create, taking objects of style declarations.

import style9 from 'style9';

const styles = style9.create({
  button: {
    padding: 0,
    color: 'rebeccapurple'
  },
  padding: {
    padding: 12
  },
  icon: {
    width: 24,
    height: 24
  }
});

Because each declaration compiles to an atomic class, shorthands that set several properties — flex: 1, background: blue — won't work. Expandable properties such as padding, margin, and overflow are automatically converted to their longhand forms. With TypeScript, unsupported properties raise an error.

Producing class names

Calling the function returned by style9.create with style keys generates a class name.

const className = styles('button');

Later keys take precedence and merge with earlier ones like Object.assign, so an element could end up with 12px padding and rebeccapurple text.

const className = styles('button', 'padding');

Conditional styles can be expressed in any of these forms:

// logical AND
styles('button', hasPadding && 'padding');
// ternary
styles('button', isGreen ? 'green' : 'red');
// object of booleans
styles({
  button: true,
  green: isGreen,
  padding: hasPadding
});

These calls disappear at compile time, replaced by direct string concatenation — the first line above becomes something like 'c1r9f2e5 ' + hasPadding ? 'cu2kwdz ' : ''. No runtime is left behind.

Extending a style object

A style object can be extended by accessing it with a property name and passing the result to style9; rightmost styles win.

const styles = style9.create({ blue: { color: 'blue; } });
const otherStyles = style9.create({ red: { color: 'red; } });

// will be red
const className = style9(styles.blue, otherStyles.red);

Here the class name can't be resolved statically: property values are replaced by classes and joined at runtime, while the properties are still added to the CSS file as before.

Why build-time extraction matters

CSS-in-JS benefits are real, but embedding styles in code carries a performance cost. Extracting values at build time gives both: styles sit alongside markup, existing JavaScript infrastructure remains usable, and the generated stylesheet stays optimal.