Skip to content

feat: add example-nextjs-source with split-layer StyleX plugin - #1421

Merged
cixzhang merged 4 commits into
mainfrom
navi/feat/example-nextjs-source
Apr 16, 2026
Merged

cixzhang merged 4 commits into
mainfrom
navi/feat/example-nextjs-source

Conversation

@cixzhang

@cixzhang cixzhang commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

New source build example for serious consumers who want an optimized production build. Compiles @xds/core from raw TypeScript + StyleX source alongside product code in a single build pass — fully tree-shaken, zero unused CSS.

Split-layer PostCSS plugin

The key innovation is stylex-split-layers.js — a custom PostCSS plugin that wraps the StyleX compilation and partitions the output by source file path:

  • Library rules (from node_modules/@xds/) → @layer xds-base
  • Product rules (from src/) → @layer product

Combined with reset.css and theme.css, the full cascade is:

reset < xds-base < xds-theme < product

Product StyleX styles always override XDS component defaults and theme overrides without !important or specificity hacks.

Layer demo

Includes a visual demo showing the three-layer cascade on buttons:

  1. Default XDS buttons (xds-base layer)
  2. Theme-styled secondary button (xds-theme layer)
  3. Product overrides: pill shape, full-width, green background (product layer)
Screenshot 2026-04-16 at 10 48 58 AM

Example apps matrix

Example Build CSS approach
example-nextjs Dist Plain CSS imports
example-nextjs-tailwind Dist Tailwind v4
example-nextjs-stylex Dist StyleX for product only
example-nextjs-source Source StyleX compiles everything

New source build example that compiles @xds/core from raw TypeScript +
StyleX source alongside product code. A custom PostCSS plugin
(stylex-split-layers.js) partitions compiled StyleX rules by file path
and wraps them in separate CSS layers:

  reset < xds-base (library) < xds-theme < product (app)

This ensures product StyleX styles always override XDS component
defaults without !important or specificity hacks.

Includes a layer demo showing the three-layer cascade on buttons:
base default → theme override → product override.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 16, 2026
@github-actions

github-actions Bot commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 15.8KB 24.7KB 3.8KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

Move the custom PostCSS plugin from a local file in the example app
to a proper package at packages/postcss-plugin. This is a dev
dependency that source-build consumers install alongside @xds/core.

The example app's postcss.config.js now references '@xds/postcss-plugin'
instead of './stylex-split-layers'.
Comment on lines +194 to +203
const libraryCss = libraryRules.length
? stylexBabelPlugin.processStylexRules(libraryRules, {
useLayers: true,
})
: '';
const productCss = productRules.length
? stylexBabelPlugin.processStylexRules(productRules, {
useLayers: true,
})
: '';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mellyeliu -- is it going to be okay for us to use stylex's babel plugin this way?

Reduce config to two required options:
- appDir: where your app source lives (default: 'src')
- babelPlugins: your StyleX babel plugins

The plugin handles XDS-specific defaults internally:
- Auto-includes node_modules/@xds/**/*.{ts,tsx}
- Knows its own library pattern for layer splitting
- Configures TypeScript/JSX parsing
- Uses @stylexjs/stylex as the import source

Before:
  include: ['src/**/*', 'node_modules/@xds/core/**/*'],
  babelConfig: { babelrc: false, parserOpts: {...}, plugins: ... },
  libraryPattern: 'node_modules/@xds/',

After:
  appDir: 'src',
  babelPlugins: babelConfig.plugins,
@cixzhang
cixzhang merged commit 6e694c1 into main Apr 16, 2026
16 checks passed
@cixzhang
cixzhang deleted the navi/feat/example-nextjs-source branch April 18, 2026 02:58
cixzhang added a commit that referenced this pull request Apr 26, 2026
* feat: add example-nextjs-source with split-layer StyleX plugin

New source build example that compiles @xds/core from raw TypeScript +
StyleX source alongside product code. A custom PostCSS plugin
(stylex-split-layers.js) partitions compiled StyleX rules by file path
and wraps them in separate CSS layers:

  reset < xds-base (library) < xds-theme < product (app)

This ensures product StyleX styles always override XDS component
defaults without !important or specificity hacks.

Includes a layer demo showing the three-layer cascade on buttons:
base default → theme override → product override.

* docs: explain why layers.css is a separate file

* refactor: extract stylex-split-layers into @xds/postcss-plugin package

Move the custom PostCSS plugin from a local file in the example app
to a proper package at packages/postcss-plugin. This is a dev
dependency that source-build consumers install alongside @xds/core.

The example app's postcss.config.js now references '@xds/postcss-plugin'
instead of './stylex-split-layers'.

* refactor: simplify @xds/postcss-plugin API

Reduce config to two required options:
- appDir: where your app source lives (default: 'src')
- babelPlugins: your StyleX babel plugins

The plugin handles XDS-specific defaults internally:
- Auto-includes node_modules/@xds/**/*.{ts,tsx}
- Knows its own library pattern for layer splitting
- Configures TypeScript/JSX parsing
- Uses @stylexjs/stylex as the import source

Before:
  include: ['src/**/*', 'node_modules/@xds/core/**/*'],
  babelConfig: { babelrc: false, parserOpts: {...}, plugins: ... },
  libraryPattern: 'node_modules/@xds/',

After:
  appDir: 'src',
  babelPlugins: babelConfig.plugins,
cixzhang added a commit that referenced this pull request Jun 21, 2026
* feat: add example-nextjs-source with split-layer StyleX plugin

New source build example that compiles @xds/core from raw TypeScript +
StyleX source alongside product code. A custom PostCSS plugin
(stylex-split-layers.js) partitions compiled StyleX rules by file path
and wraps them in separate CSS layers:

  reset < xds-base (library) < xds-theme < product (app)

This ensures product StyleX styles always override XDS component
defaults without !important or specificity hacks.

Includes a layer demo showing the three-layer cascade on buttons:
base default → theme override → product override.

* docs: explain why layers.css is a separate file

* refactor: extract stylex-split-layers into @xds/postcss-plugin package

Move the custom PostCSS plugin from a local file in the example app
to a proper package at packages/postcss-plugin. This is a dev
dependency that source-build consumers install alongside @xds/core.

The example app's postcss.config.js now references '@xds/postcss-plugin'
instead of './stylex-split-layers'.

* refactor: simplify @xds/postcss-plugin API

Reduce config to two required options:
- appDir: where your app source lives (default: 'src')
- babelPlugins: your StyleX babel plugins

The plugin handles XDS-specific defaults internally:
- Auto-includes node_modules/@xds/**/*.{ts,tsx}
- Knows its own library pattern for layer splitting
- Configures TypeScript/JSX parsing
- Uses @stylexjs/stylex as the import source

Before:
  include: ['src/**/*', 'node_modules/@xds/core/**/*'],
  babelConfig: { babelrc: false, parserOpts: {...}, plugins: ... },
  libraryPattern: 'node_modules/@xds/',

After:
  appDir: 'src',
  babelPlugins: babelConfig.plugins,
cixzhang added a commit that referenced this pull request Jun 21, 2026
* feat: add example-nextjs-source with split-layer StyleX plugin

New source build example that compiles @xds/core from raw TypeScript +
StyleX source alongside product code. A custom PostCSS plugin
(stylex-split-layers.js) partitions compiled StyleX rules by file path
and wraps them in separate CSS layers:

  reset < xds-base (library) < xds-theme < product (app)

This ensures product StyleX styles always override XDS component
defaults without !important or specificity hacks.

Includes a layer demo showing the three-layer cascade on buttons:
base default → theme override → product override.

* docs: explain why layers.css is a separate file

* refactor: extract stylex-split-layers into @xds/postcss-plugin package

Move the custom PostCSS plugin from a local file in the example app
to a proper package at packages/postcss-plugin. This is a dev
dependency that source-build consumers install alongside @xds/core.

The example app's postcss.config.js now references '@xds/postcss-plugin'
instead of './stylex-split-layers'.

* refactor: simplify @xds/postcss-plugin API

Reduce config to two required options:
- appDir: where your app source lives (default: 'src')
- babelPlugins: your StyleX babel plugins

The plugin handles XDS-specific defaults internally:
- Auto-includes node_modules/@xds/**/*.{ts,tsx}
- Knows its own library pattern for layer splitting
- Configures TypeScript/JSX parsing
- Uses @stylexjs/stylex as the import source

Before:
  include: ['src/**/*', 'node_modules/@xds/core/**/*'],
  babelConfig: { babelrc: false, parserOpts: {...}, plugins: ... },
  libraryPattern: 'node_modules/@xds/',

After:
  appDir: 'src',
  babelPlugins: babelConfig.plugins,
cixzhang pushed a commit that referenced this pull request Oct 21, 2026
…ibrary pass (#7774)

<!-- template: bug-fix.md -->

## User impact

Every documented Next.js source build pays for an unused Babel run over every installed Astryx module. `@astryxdesign/build`'s PostCSS plugin compiles Astryx source to collect its StyleX rules with `babelrc: false` but not `configFile: false`, so Babel also loads the app's root `babel.config.js`: in the documented setup, `next/babel` plus Astryx's StyleX plugin. The CSS pass then runs preset-env, Next's plugins and a second StyleX instance over package source, only to read StyleX metadata. On a clean install of the documented example that roughly doubles the pass, for byte-identical CSS.

## Problem and solution fit

- Why it happens: `babelrc: false` only disables `.babelrc` files; Babel still loads a root config file unless told `configFile: false`.
- Why it harms: the extra transforms produce nothing the pass uses, and they scale with the Astryx packages installed, not used.
- Why that matters: the app cannot keep its root config out of the plugin's pass without dropping it from its own compile.

## Expected behavior and authority

No record governs the plugin's build cost. `spec:AST-080` FR7 (current) governs its output, which stays byte-identical. The pass already opts out of the app's Babel configuration with `babelrc: false` (since #1421); `configFile: false` extends that to the root config file Babel also loads. #5464 shows the same leak failing a consumer's StyleX pass.

## Smallest restoration

- The library pass sets `configFile: false`; it already passes its own parser options and plugins.
- The app's own files still compile with its config, which may carry syntax plugins they need.

## Evidence

- Reproduction before the change: on a clean registry install of the documented example, the app's PostCSS config takes a median 37.4 s with the README's browserslist and 67.3 s with none (5 fresh-process runs each, alternating order; the no-browserslist case has #7767 applied, since main does not build without one).
- Result after the change: 20.3 s and 21.4 s, with byte-identical CSS across all 20 runs.
- Representative unchanged path: the app's own files still compile with its config, which the test asserts.
- Regression test or other mutation-sensitive proof: `postcss.test.mjs` runs the documented config from an app whose `babel.config.js` records each file it is applied to. On main it records the Astryx module; here only the app's.

## Scope

- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

- `pnpm exec vitest run --project node packages/build`: 6 files, 79 tests pass; `postcss.test.mjs` fails on main.
- CSS-pass runs, before and after: README browserslist 35.7/41.1/36.7/37.4/38.1 s and 19.9/24.8/20.3/20.0/22.8 s; none 67.3/75.8/67.8/65.9/52.9 s and 19.9/21.4/24.9/24.7/19.7 s.
- `pnpm -F @astryxdesign/core typecheck`, Prettier, `pnpm check:repo`, changeset coverage: pass (ESLint ignores `packages/build/src`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant