Skip to content

feat: add @griffel/turbopack-loader - #1150

Closed
Aziz Gazanchiian (creage) wants to merge 1 commit into
microsoft:mainfrom
creage:feat/turbopack-loader
Closed

Aziz Gazanchiian (creage) wants to merge 1 commit into
microsoft:mainfrom
creage:feat/turbopack-loader

Conversation

@creage

@creage Aziz Gazanchiian (creage) commented Dec 2, 2026 •

Copy link
Copy Markdown

Why

Next.js 16 uses Turbopack by default, and Griffel has no ahead-of-time compilation for it. Turbopack runs webpack-style loaders but has no plugin API, so @griffel/webpack-plugin does not work there.

What

  • @griffel/turbopack-loader (new) compiles makeStyles() and makeResetStyles() calls with @griffel/transform.
  • @griffel/transform gets an extractCSS option (default: true). With false, calls are compiled to __styles() / __resetStyles() / __staticStyles() and the rules stay in the code.
  • @griffel/transform and @griffel/transform-shaker now parse JSX in .js and .mjs ES modules. Next.js projects without TypeScript keep styles and JSX in the same .js file, and these failed to parse.
  • @griffel/vite-plugin and @griffel/webpack-plugin omit extractCSS from their options (types only).
  • A docs page for Next.js and an e2e test that uses Next.js 16.

CSS extraction is not part of this PR. Turbopack strips comments that mark style buckets and does not allow sorting rules across chunks. A naive version of extraction produced a wrong cascade in the browser, for example padding-left: 1px instead of 10px. Rules are inserted at runtime instead, so the bucket order is kept.

How it works

  • Turbopack keeps loader workers alive and persists loader results between builds. The loader drops evaluated modules for every file and reports transitive dependencies, so changes in them cause a recompile.
  • Turbopack's resolver is asynchronous and the transform is synchronous. The loader uses oxc-resolver with paths from the closest tsconfig.json or jsconfig.json.
  • Files that use imported assets in styles and files that are not ES modules are passed through to the runtime.

Limitations

These are listed in the README, with workarounds where they exist:

  • Variables from env in next.config.js evaluate to undefined, so styles that use them are dropped.
  • next/font, import.meta and browser globals in evaluated code cause a build error. /* griffel-css-extraction-disable */ opts a file out.
  • turbopack.resolveAlias and turbopack.resolveExtensions are not supported.
  • Source maps do not account for the compiled calls.
  • makeStaticStyles() is not compiled by default, because @fluentui/react-components does not export __staticStyles.

Testing

  • Unit tests for the loader, the transform and the shaker, and an e2e test that covers compilation, the asset fallback and a rebuild after a dependency change.
  • On Next.js 16.4.6, markup and computed styles match the runtime build on four pages, including one with @fluentui/react-components. A build on Next.js 16.0.0 also works.
  • First call of 200 hooks with 1000 slots in Node: 28.4 ms at runtime and 2.6 ms compiled (median of 15 runs per side, without DOM insertion). Client JS of the test app grew by 1.8 kB on 690 kB.
  • On Windows, five tests in transform, css-extraction-utils, webpack-plugin and postcss-syntax fail both on this branch and on main.

Next.js 16 uses Turbopack by default, which has no ahead-of-time compilation
option for Griffel. The loader compiles makeStyles() & makeResetStyles() calls
and keeps CSS in the code, it is inserted in runtime to preserve the order of
rules.

- transform: add "extractCSS" option to keep CSS rules in the code
- transform, transform-shaker: support JSX in ".js" & ".mjs" ES modules
- vite-plugin, webpack-plugin: exclude "extractCSS" from options

Closes microsoft#730
@creage
Aziz Gazanchiian (creage) requested a review from a team as a code owner December 2, 2026 08:14
@creage
Aziz Gazanchiian (creage) deleted the feat/turbopack-loader branch December 2, 2026 08:42
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.

1 participant