Skip to content

fix(rsbuild): load StyleX styles via extracted stylex.css - #511

Merged
chenjiahan merged 4 commits into
rstackjs:mainfrom
heyq02:fix/rsbuild-stylex-css-link
Oct 12, 2026
Merged

chenjiahan merged 4 commits into
rstackjs:mainfrom
heyq02:fix/rsbuild-stylex-css-link

Conversation

@heyq02

@heyq02 heyq02 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Disable StyleX runtimeInjection and link the emitted stylex.css from public/index.html in rsbuild/stylex.
  • Update the example README to document why the CSS link is required for production builds.

Fixes #356

Test plan

  • pnpm --filter @rsbuild-example/stylex build succeeds
  • dist/stylex.css is emitted and dist/index.html contains <link rel="stylesheet" href="/stylex.css" />
  • pnpm --filter @rsbuild-example/stylex preview — StyleX styles render (stylex.css loads, h1 font-size: 3.6rem / font-weight: 700, flex column layout)
  • pnpm --filter @rsbuild-example/stylex dev — same StyleX styles render via /stylex.css

Disable StyleX runtimeInjection and link the emitted stylex.css from
the HTML template so production builds render styles correctly.

Fixes rstackjs#356

Co-authored-by: Cursor <cursoragent@cursor.com>

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the fix! Hardcoding /stylex.css bypasses Rsbuild’s CSS asset handling, including content-hashed filenames and inlineStyles.

Disabling runtime injection in dev also breaks style HMR: class names update, but the stylesheet only updates after a page refresh.

Could we try the official @stylexjs/unplugin instead? It may be a better fit.

heyq02 and others added 3 commits October 12, 2026 19:06
Use the official StyleX unplugin so generated CSS is appended to
Rsbuild's content-hashed CssExtract asset instead of a hardcoded
/stylex.css link. Prefer stylex.webpack() because CSS injection is
attached via the webpack-compatible hook.

Addresses review feedback on rstackjs#511 / rstackjs#356.

Co-authored-by: Cursor <cursoragent@cursor.com>
@heyq02

heyq02 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — addressed in a621f95.

Changes

  • Replaced unplugin-stylex with official @stylexjs/unplugin
  • Removed hardcoded /stylex.css HTML link
  • StyleX CSS now appends into Rsbuild’s content-hashed CSS asset (static/css/index.<hash>.css)

Note

Used stylex.webpack() instead of stylex.rspack(): the unplugin’s CSS injection currently attaches through the webpack-compatible hook; with stylex.rspack() only the transform ran and CSS was not emitted. Transform + injection both work via stylex.webpack() under tools.rspack.plugins.

Verified with pnpm build / pnpm preview.

@heyq02
heyq02 requested a review from chenjiahan October 12, 2026 11:30
@chenjiahan
chenjiahan merged commit e1880ee into rstackjs:main Oct 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StyleX example doesn't setup styles correctly

2 participants