style9

CSS-in-JS compiler inspired by Meta's stylex

README

style9


CSS-in-JS compiler inspired by Facebook's stylex, with near-zero runtime, atomic CSS extraction and TypeScript support. Framework agnostic.

Basic usage


For a complete walkthrough of the API, see Usage guide.

  1. ```javascript
  2. import style9 from 'style9';

  3. const styles = style9.create({
  4.   blue: {
  5.     color: 'blue',
  6.   },
  7.   red: {
  8.     color: 'red'
  9.   }
  10. });

  11. document.body.className = styles('blue', isRed && 'red');
  12. ```

For the above input, the compiler will generate the following output:

  1. ```javascript
  2. /* JavaScript */
  3. document.body.className = isRed ? 'cRCRUH ' : 'hxxstI ';

  4. /* CSS */
  5. .hxxstI { color: blue }
  6. .cRCRUH { color: red }
  7. ```

Installation


  1. ```sh
  2. # Yarn
  3. yarn add style9

  4. # npm
  5. npm install style9
  6. ```

Compiler setup - required


The following is the minimally required Webpack setup for extracting styles to a CSS file. For Webpack options and Rollup, Next.js, Gatsby,Vite, and Babel plugins, see Bundler plugins.

  1. ```javascript
  2. const Style9Plugin = require('style9/webpack');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');

  4. module.exports = {
  5.   // Collect all styles in a single file - required
  6.   optimization: {
  7.     splitChunks: {
  8.       cacheGroups: {
  9.         styles: {
  10.           name: 'styles',
  11.           type: 'css/mini-extract',
  12.           // For webpack@4 remove type and uncomment the line below
  13.           // test: /\.css$/,
  14.           chunks: 'all',
  15.           enforce: true,
  16.         }
  17.       }
  18.     }
  19.   },
  20.   module: {
  21.     rules: [
  22.       {
  23.         test: /\.(tsx|ts|js|mjs|jsx)$/,
  24.         use: Style9Plugin.loader,
  25.       },
  26.       {
  27.         test: /\.css$/i,
  28.         use: [MiniCssExtractPlugin.loader, 'css-loader']
  29.       }
  30.     ]
  31.   },
  32.   plugins: [
  33.     new Style9Plugin(),
  34.     new MiniCssExtractPlugin()
  35.   ]
  36. };
  37. ```

Have a question?


Look at the FAQ, search the repo, or ask in discussions.

[stylex]: https://www.youtube.com/watch?v=9JZHodNR184
[search]: https://github.com/johanholmerin/style9/search
[discussions]: https://github.com/johanholmerin/style9/discussions