Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

Update Storybook setup following StyleX recommendation (#7)

Cindy Zhang5a869b59565c
diff --git a/.context/proposals/stylex-distribution.md b/.context/proposals/stylex-distribution.md
new file mode 100644
index 0000000000..7746d89b96
--- /dev/null
+++ b/.context/proposals/stylex-distribution.md
@@ -0,0 +1,79 @@
+# StyleX Distribution Pattern
+
+## Status
+
+Proposed
+
+## Context
+
+XDS is a component library that uses StyleX for styling. We needed to decide how to distribute the library to consuming applications (Next.js, Vite, etc.).
+
+The StyleX team recommended a specific pattern based on their example:
+https://github.com/facebook/stylex/tree/main/examples/example-nextjs
+
+## Proposal
+
+**Ship source code with StyleX runtime only. Consumer apps run the StyleX build plugins.**
+
+### Component Library (`@xds/core`)
+
+- Uses only `@stylexjs/stylex` as a dependency (runtime package)
+- Ships raw `.tsx` source files containing `stylex.create()` calls
+- Does NOT run `@stylexjs/babel-plugin` or `@stylexjs/postcss-plugin`
+- Essentially a "source distribution"
+
+```json
+// @xds/core/package.json
+{
+ "dependencies": {
+ "@stylexjs/stylex": "^0.17.0"
+ },
+ "exports": {
+ ".": "./src/index.ts",
+ "./Button": "./src/Button/index.ts"
+ }
+}
+```
+
+### Consuming App (Next.js, Vite, Storybook, etc.)
+
+For Next.js:
+- Runs `@stylexjs/babel-plugin` to transform StyleX calls
+- Runs `@stylexjs/postcss-plugin` to extract CSS
+
+For Vite, Webpack/Rspack, and Esbuild:
+- Use `@stylexjs/unplugin` to generate and bundle CSS
+- Processes both its own code AND `@xds/core` imports
+- Handles all CSS extraction at build time
+
+```js
+// postcss.config.mjs in consumer app
+stylexPlugin({
+ include: [
+ 'src/**/*.{ts,tsx}', // App's own code
+ 'node_modules/@xds/core/**/*', // XDS components
+ ],
+ // ...
+})
+```
+
+## Consequences
+
+### Benefits
+
+1. **No pre-compilation needed** - Library doesn't need a complex build step for styles
+2. **Consumer controls CSS output** - Apps can configure CSS layers, extraction, etc.
+3. **Tree-shaking works naturally** - Consumer's bundler handles dead code elimination
+4. **Simpler library packaging** - Just ship TypeScript source
+5. **Theme tokens resolve correctly** - CSS variables are processed in app context
+
+### Tradeoffs
+
+1. **Consumer must configure StyleX** - Apps need babel + postcss plugins set up
+2. **Include paths required** - PostCSS config must explicitly include `@xds/core` paths
+3. **Build time at consumer** - StyleX processing happens during app build, not library build
+
+## References
+
+- StyleX Next.js example: https://github.com/facebook/stylex/tree/main/examples/example-nextjs
+- StyleX Storybook example: https://github.com/facebook/stylex/tree/main/examples/example-storybook
diff --git a/apps/storybook/.babelrc.cjs b/apps/storybook/.babelrc.cjs
new file mode 100644
index 0000000000..13c7d68e46
--- /dev/null
+++ b/apps/storybook/.babelrc.cjs
@@ -0,0 +1,28 @@
+/**
+ * @file .babelrc.cjs
+ * @input Uses @babel/preset-react, @babel/preset-typescript, @stylexjs/babel-plugin
+ * @output Babel configuration for React/TypeScript with StyleX
+ * @position Build config; used by Vite and PostCSS for transpilation
+ */
+
+module.exports = {
+ presets: [
+ ['@babel/preset-react', { runtime: 'automatic' }],
+ '@babel/preset-typescript',
+ ],
+ plugins: [
+ [
+ '@stylexjs/babel-plugin',
+ {
+ dev: process.env.NODE_ENV === 'development',
+ runtimeInjection: false,
+ genConditionalClasses: true,
+ treeshakeCompensation: true,
+ unstable_moduleResolution: {
+ type: 'commonJS',
+ rootDir: __dirname,
+ },
+ },
+ ],
+ ],
+};
diff --git a/apps/storybook/package.json b/apps/storybook/package.json
index 41675abff1..ad3176ff61 100644
--- a/apps/storybook/package.json
+++ b/apps/storybook/package.json
@@ -23,6 +23,8 @@
"@storybook/react-vite": "^8.4.0",
"@storybook/test": "^8.4.0",
"@stylexjs/babel-plugin": "^0.17.4",
+ "@stylexjs/postcss-plugin": "^0.17.5",
+ "autoprefixer": "^10.4.23",
"storybook": "^8.4.0"
}
}
diff --git a/apps/storybook/postcss.config.mjs b/apps/storybook/postcss.config.mjs
new file mode 100644
index 0000000000..f2acb6da46
--- /dev/null
+++ b/apps/storybook/postcss.config.mjs
@@ -0,0 +1,30 @@
+/**
+ * @file postcss.config.mjs
+ * @input Uses postcss, @stylexjs/postcss-plugin, autoprefixer
+ * @output PostCSS configuration for StyleX processing
+ * @position Build config; used by Vite for CSS processing
+ */
+
+import autoprefixer from 'autoprefixer';
+import stylexPlugin from '@stylexjs/postcss-plugin';
+import babelrc from './.babelrc.cjs';
+
+export default {
+ plugins: [
+ stylexPlugin({
+ include: [
+ 'stories/**/*.{ts,tsx}',
+ '../../../packages/core/src/**/*.{ts,tsx}',
+ ],
+ babelConfig: {
+ babelrc: false,
+ parserOpts: {
+ plugins: ['typescript', 'jsx'],
+ },
+ ...babelrc,
+ },
+ useCSSLayers: process.env.NODE_ENV !== 'development',
+ }),
+ autoprefixer(),
+ ],
+};
diff --git a/apps/storybook/vite.config.ts b/apps/storybook/vite.config.ts
index 391701a0cc..ba7be907ec 100644
--- a/apps/storybook/vite.config.ts
+++ b/apps/storybook/vite.config.ts
@@ -1,7 +1,7 @@
/**
* @file vite.config.ts
- * @input Uses vite, @vitejs/plugin-react, path
- * @output Vite configuration with React plugin and @xds/core alias
+ * @input Uses vite, @vitejs/plugin-react, babel config
+ * @output Vite configuration with React/StyleX via Babel
* @position Build config; used by Storybook's Vite integration
*
* SYNC: When modified, update this header and /apps/storybook/README.md
@@ -9,9 +9,14 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
+import babelrc from './.babelrc.cjs';
export default defineConfig({
- plugins: [react()],
+ plugins: [
+ react({
+ babel: babelrc,
+ }),
+ ],
optimizeDeps: {
include: ['react', 'react-dom'],
},
diff --git a/yarn.lock b/yarn.lock
index a0f0c161bd..22828185f4 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1744,7 +1744,7 @@
"@stylexjs/shared" "0.11.1"
"@stylexjs/stylex" "0.11.1"
-"@stylexjs/babel-plugin@^0.17.4":
+"@stylexjs/[email protected]", "@stylexjs/babel-plugin@^0.17.4":
version "0.17.5"
resolved "https://registry.yarnpkg.com/@stylexjs/babel-plugin/-/babel-plugin-0.17.5.tgz#2161ae32ad1eb08cbe4b40985aa5ba4f6017adf7"
integrity sha512-BT+066uVkzcJSLtkGaEmFLEWxs3uC08wuOxeC8AEVyfWibz45/e1OVjDgIf1+8my0XiJXSLAlwjmzkf1/unjNA==
@@ -1772,6 +1772,18 @@
babel-plugin-syntax-hermes-parser "^0.26.0"
esbuild "^0.24.0"
+"@stylexjs/postcss-plugin@^0.17.5":
+ version "0.17.5"
+ resolved "https://registry.yarnpkg.com/@stylexjs/postcss-plugin/-/postcss-plugin-0.17.5.tgz#03ecd009409cbcb59e5083cac8a62f591ec38e16"
+ integrity sha512-GeveZXN8aDrXTSRpDM0SvCjmmY7WCfoel+lWcsf47QcJvjz5auGmh3a9LhIFLpwmAgpsVr20RyB8yxX93NaKow==
+ dependencies:
+ "@babel/core" "^7.26.8"
+ "@stylexjs/babel-plugin" "0.17.5"
+ fast-glob "^3.3.2"
+ glob-parent "^6.0.2"
+ is-glob "^4.0.3"
+ postcss "^8.4.49"
+
"@stylexjs/[email protected]":
version "0.11.1"
resolved "https://registry.yarnpkg.com/@stylexjs/shared/-/shared-0.11.1.tgz#c92160f33161f5721f5e1b4a623f99892df5d319"
@@ -2182,6 +2194,17 @@ ast-types@^0.16.1:
dependencies:
tslib "^2.0.1"
+autoprefixer@^10.4.23:
+ version "10.4.23"
+ resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-10.4.23.tgz#c6aa6db8e7376fcd900f9fd79d143ceebad8c4e6"
+ integrity sha512-YYTXSFulfwytnjAPlw8QHncHJmlvFKtczb8InXaAx9Q0LbfDnfEYDE55omerIJKihhmU61Ft+cAOSzQVaBUmeA==
+ dependencies:
+ browserslist "^4.28.1"
+ caniuse-lite "^1.0.30001760"
+ fraction.js "^5.3.4"
+ picocolors "^1.1.1"
+ postcss-value-parser "^4.2.0"
+
available-typed-arrays@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz#a5cc375d6a03c2efc87a553f3e0b1522def14846"
@@ -2254,7 +2277,7 @@ browser-assert@^1.2.1:
resolved "https://registry.yarnpkg.com/browser-assert/-/browser-assert-1.2.1.tgz#9aaa5a2a8c74685c2ae05bfe46efd606f068c200"
integrity sha512-nfulgvOR6S4gt9UKCeGJOuSGBPGiFT6oQ/2UBnvTY/5aQ1PnksW72fhZkM30DzoRRv2WpwZf1vHHEr3mtuXIWQ==
-browserslist@^4.24.0:
+browserslist@^4.24.0, browserslist@^4.28.1:
version "4.28.1"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.28.1.tgz#7f534594628c53c63101079e27e40de490456a95"
integrity sha512-ZC5Bd0LgJXgwGqUknZY/vkUQ04r8NXnJZ3yYi4vDmSiZmC/pdSN0NbNRPxZpbtO4uAfDUAFffO8IZoM3Gj8IkA==
@@ -2308,7 +2331,7 @@ callsites@^3.0.0:
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==
-caniuse-lite@^1.0.30001759:
+caniuse-lite@^1.0.30001759, caniuse-lite@^1.0.30001760:
version "1.0.30001764"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001764.tgz#03206c56469f236103b90f9ae10bcb8b9e1f6005"
integrity sha512-9JGuzl2M+vPL+pz70gtMF9sHdMFbY9FJaQBi186cHKH3pSzDvzoUJUPV6fqiKIMyXbud9ZLg4F3Yza1vJ1+93g==
@@ -2852,7 +2875,7 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==
-fast-glob@^3.2.9:
+fast-glob@^3.2.9, fast-glob@^3.3.2:
version "3.3.3"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.3.tgz#d06d585ce8dba90a16b0505c543c3ccfb3aeb818"
integrity sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg==
@@ -2952,6 +2975,11 @@ foreground-child@^3.1.0:
cross-spawn "^7.0.6"
signal-exit "^4.0.1"
+fraction.js@^5.3.4:
+ version "5.3.4"
+ resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-5.3.4.tgz#8c0fcc6a9908262df4ed197427bdeef563e0699a"
+ integrity sha512-1X1NTtiJphryn/uLQz3whtY6jK3fTqoE3ohKs0tT+Ujr1W59oopxmoEh7Lu5p6vBaPbgoM0bzveAW4Qi5RyWDQ==
+
fs-extra@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
@@ -3865,12 +3893,12 @@ postcss-load-config@^6.0.1:
dependencies:
lilconfig "^3.1.1"
-postcss-value-parser@^4.1.0:
+postcss-value-parser@^4.1.0, postcss-value-parser@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz#723c09920836ba6d3e5af019f92bc0971c02e514"
integrity sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==
-postcss@^8.4.43:
+postcss@^8.4.43, postcss@^8.4.49:
version "8.5.6"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.5.6.tgz#2825006615a619b4f62a9e7426cc120b349a8f3c"
integrity sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==