Skip to content

turbopack support - #1639

Merged
askoufis merged 74 commits into
vanilla-extract-css:masterfrom
RJWadley:turbo-loader
Mar 15, 2026
Merged

askoufis merged 74 commits into
vanilla-extract-css:masterfrom
RJWadley:turbo-loader

Conversation

@RJWadley

@RJWadley RJWadley commented Oct 8, 2025 •

Copy link
Copy Markdown
Contributor

adds turbopack support to @vanilla-extract/next-plugin

turbopack doesn't have any sort of external API yet, so in order to evaluate the contents of our *.css.ts files we need to compile it separately using @vanilla-extract/compiler. To avoid import resolution mismatches, we defer module resolution to turbopack via its webpack-compatible api.

next/font doesn't work out of the box because turbopack has custom logic for it, so we transform any modules that use it in a css context to contain static references to the generated font family, style, and weight. next/image gets a similar treatment. most other next modules won't work in css.ts files for now.

fixes #1367

@changeset-bot

changeset-bot Bot commented Oct 8, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bf149ba

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@vanilla-extract/next-plugin Minor
@vanilla-extract/compiler Minor
@vanilla-extract/turbopack-plugin Patch
@vanilla-extract/vite-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@csorfab

csorfab commented Oct 19, 2025 •

Copy link
Copy Markdown

@mattcompiles @askoufis Can anyone take a look at this? This is a pretty important feature for next.js users. Or should we consider the project dead? Last PR was merged in early August, basically no activity from maintainers since then. Not meaning to bash anyone, I know it's thankless work maintaining open source software, but it would be nice to know what we can expect from this project. Thanks in advance!

@askoufis

Copy link
Copy Markdown
Contributor

Or should we consider the project dead? Last PR was merged in early August, basically no activity from maintainers since then.

A PR was merged in September. Reviews take time and ongoing work is often slow. All current maintainers have professional commitments and/or families.

I try to look at PRs when I have spare time/motivation, which is often quite sporadic. I'm currently on holiday, but I intend to make some headway on open PRs and repo maintenance in when I get back in early November.

@csorfab

csorfab commented Oct 19, 2025

Copy link
Copy Markdown

Thanks for your reply! I was a bit too dramatic, sorry about that. Enjoy your holiday, and let me know if I can help with anything to get this PR going.

@RJWadley
RJWadley marked this pull request as draft October 23, 2025 04:49
@callum-gander

Copy link
Copy Markdown

I see there have been some recent commits pushed up to this PR. Are there any bits that you need help with that could be provided to speed up getting this merged in?

Appreciate that it's been a holiday period, but a lot of apps have performance issues with Next which has various potential solutions but the main recommendation seems to boil down to "just use Turbopack" vercel/next.js#48748. Obviously, the lack of support for Turbopack with Vanilla is effectively blocking us from getting those gains, which would be greatly appreciated ahead of Black Friday.

It would be great if there were a list of tasks that I or people within my company could help out with to speed this up, whether that's commits or just QAing. Cheers and thanks for the work on this so far!

@RJWadley

RJWadley commented Nov 17, 2025 •

Copy link
Copy Markdown
Contributor Author

If you're interested in trying this out, I've been using a preinstall script to download and install this branch:

"preinstall": "pnpm dlx gitpick https://github.com/RJWadley/vanilla-extract/tree/turbo-loader/ /tmp/vanilla-temp -f && cd /tmp/vanilla-temp && pnpm i && pnpm build"

You can install that temporary build with a link:

"@vanilla-extract/next-plugin": "link:/tmp/vanilla-temp/packages/next-plugin",

Although, you probably want to pin to a specific commit instead of the branch for the sake of stability:

"preinstall": "pnpm dlx gitpick RJWadley/vanilla-extract -b f5163f1 /tmp/vanilla-temp -f && cd /tmp/vanilla-temp && pnpm i && pnpm build"

Feedback or issues appreciated! I haven't seen many stats yet on speed or size of builds (compared to webpack) so if you have those I'd love to see some details.


The biggest issue with this at the moment is next specific modules not playing nicely. Since we must process styles outside of next.js there's not much that can be done. Think next/font, next/image, etc. Next has decent webpack loader support, but there are a few things missing that make it much harder (and in some cases impossible) for us to preprocess styles.

I'm gonna do a quick cleanup pass this week to prep this for merge, it's basically ready at this point but is still a bit messy while I'm experimenting with some stuff.

TLDR:

  • if your styles don't use next modules at all everything will work fine
  • next/font partially works
  • next/image imports don't work at all
  • other next modules may or may not work

@callum-gander

callum-gander commented Nov 18, 2025 •

Copy link
Copy Markdown

Hmmm, strange, I followed your steps and got this error

@frontend/app:dev:    ▲ Next.js 15.5.0
@frontend/app:dev:    - Local:        http://localhost:3000
@frontend/app:dev:    - Network:      http://10.100.22.2:3000
@frontend/app:dev:    - Environments: .env
@frontend/app:dev:    - Experiments (use with caution):
@frontend/app:dev:      ⨯ reactCompiler
@frontend/app:dev:      · optimizePackageImports
@frontend/app:dev: 
@frontend/app:dev:  ✓ Starting...
@frontend/app:dev: Warning: total number of custom routes exceeds 1000, this can reduce performance. Route counts:
@frontend/app:dev: headers: 5
@frontend/app:dev: rewrites: 2
@frontend/app:dev: redirects: 1246
@frontend/app:dev: See more info: https://nextjs.org/docs/messages/max-custom-routes-reached
@frontend/app:dev: Error: > Couldn't find a `pages` directory. Please create one under the project root
@frontend/app:dev:     at Object.webpack (../../../../../../../../private/tmp/vanilla-temp/packages/next-plugin/dist/vanilla-extract-next-plugin.cjs.dev.js:130:47)

That seemed to point at this specific line in the dist

 const findPagesDirResult = findPagesDir.findPagesDir(dir, ((_resolvedNextConfig$e = resolvedNextConfig.experimental) === null || _resolvedNextConfig$e === void 0 ? void 0 : _resolvedNextConfig$e.appDir) ?? false);

It's probably worth saying that we don't have a pages dir at all, we're fully on app dir. To be fair as well, I'm not sure that this would work for us anyway as we make heavy use of next/image, next/font, as well as other next modules.

When you say it's basically ready, do you mean basically ready minus support for next modules due to dependency on things turbopack isn't yet providing to help you preprocess styles?

If you have a solution to the above error, more than happy to give it a whirl

@RJWadley

RJWadley commented Nov 18, 2025 •

Copy link
Copy Markdown
Contributor Author

The error looks like resolution error in the webpack plugin setup. Should've seen that coming.
Switch back to the release version of next-plugin, then try configuring the loader manually:

"@vanilla-extract/turbopack-plugin": "link:/tmp/vanilla-temp/packages/turbopack-plugin",
  turbopack: {
    rules: {
      '**/*.css.{ts,tsx,js,jsx}': {
        loaders: [
          {
            loader: '@vanilla-extract/turbopack-plugin',
            options: { nextEnv: {} }, // include env vars set by next.config if any
          },
        ],
      },
    },
  },

As far as next/image support, you're free to style next images as this doesn't route through the plugin:

// page.ts
<Image className={myVanillaClass} />

The specific scenario that doesn't work is importing an image into a css.ts file and using it to generate styles:

// myStyles.css.ts
import coolThing from "./my-cool-image.png"

export const background = style({
  backgroundImage: `url(${coolThing.src})` // fails! use public dir instead
})

This is unlikely to change until we get better tools from turbopack.

For next/font, support is better. Anything under the style property is supported:

// myStyles.css.ts
import myFont from "./typography"

export const background = style({
  fontFamily: myFont.style.fontFamily
})

The specific scenario that doesn't work is extending font classNames in your styles:

// myStyles.css.ts
import myFont from "./typography"

export const background = style(myFont.className) // fails! set font family directly instead

This is also unlikely to change until we get better tools from turbopack.

TLDR using next modules is fine, but using next modules in your styles themselves is likely to fail unless I've specifically worked around it.

@Pnlvfx

Pnlvfx commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

The error looks like resolution error in the webpack plugin setup. Should've seen that coming. Switch back to the release version of next-plugin, then try configuring the loader manually:

"@vanilla-extract/turbopack-plugin": "link:/tmp/vanilla-temp/packages/turbopack-plugin",
  turbopack: {
    rules: {
      '**/*.css.{ts,tsx,js,jsx}': {
        loaders: [
          {
            loader: '@vanilla-extract/turbopack-plugin',
            options: { nextEnv: {} }, // include env vars set by next.config if any
          },
        ],
      },
    },
  },

As far as next/image support, you're free to style next images as this doesn't route through the plugin:

// page.ts
<Image className={myVanillaClass} />

The specific scenario that doesn't work is importing an image into a css.ts file and using it to generate styles:

// myStyles.css.ts
import coolThing from "./my-cool-image.png"

export const background = style({
  backgroundImage: `url(${coolThing.src})` // fails! use public dir instead
})

This is unlikely to change until we get better tools from turbopack.

For next/font, support is better. Anything under the style property is supported:

// myStyles.css.ts
import myFont from "./typography"

export const background = style({
  fontFamily: myFont.style.fontFamily
})

The specific scenario that doesn't work is extending font classNames in your styles:

// myStyles.css.ts
import myFont from "./typography"

export const background = style(myFont.className) // fails! set font family directly instead

This is also unlikely to change until we get better tools from turbopack.

TLDR using next modules is fine, but using next modules in your styles themselves is likely to fail unless I've specifically worked around it.

For me is fine, thanks, I also had next font issues with webpack so I don't mind this approach,

thank you very much for your work,

I'll try this ASAP

@RJWadley

Copy link
Copy Markdown
Contributor Author

Here is a minimal reproduction on the try/outputFileTracingRoot branch:
zeckaissue/vanilla-extract-turbopack-test-yarn@try/outputFileTracingRoot

@zecka I wasn't able to reproduce the issue on this repository when I cloned it. I would presume the issue is caused by the way things are linked on your filesystem.

Just guessing here, but one potential issue that I have seen before is that you may need to configure turbopack.root as turbopack is a bit more picky with how it resolves modules than webpack is. Try setting it to match your outputFileTracingRoot configuration.

Since you're using yarn link, you may also need to set the root higher (potentially as high as the filesystem root) to get turbopack to resolve the modules.

here's the relevant bit of documentation for turbopack.root

omd0 pushed a commit to Ruya-WEB/vanilla-extract that referenced this pull request Feb 24, 2026
Implements Turbopack compatibility based on PR vanilla-extract-css#1639 by @RJWadley:

- packages/compiler: add `splitCssPerRule` option, `invalidateAllModules()`
  method, and update `cssImportSpecifier` to accept CSS content + return
  Promise<string>
- packages/css: add `vanilla.virtual.css` noop file for virtual CSS imports,
  update package.json exports and files list
- packages/next-plugin: auto-detect Next >= 16 and configure Turbopack rules;
  add `turbopackMode` ('auto'|'on'|'off') and `turbopackGlob` options;
  add semver + @vanilla-extract/turbopack-plugin dependencies
- packages/turbopack-plugin: new package — Turbopack webpack loader that
  compiles *.css.ts files via @vanilla-extract/compiler, encodes CSS into
  virtual query-param imports on vanilla.virtual.css, and handles next/font
  stubs via SWC transform

https://claude.ai/code/session_0145VazPBZC9Q6nKK8gfjrXf
@zecka

zecka commented Feb 24, 2026

Copy link
Copy Markdown

Here is a minimal reproduction on the try/outputFileTracingRoot branch:
zeckaissue/vanilla-extract-turbopack-test-yarn@try/outputFileTracingRoot

@zecka I wasn't able to reproduce the issue on this repository when I cloned it. I would presume the issue is caused by the way things are linked on your filesystem.

You’re right, it’s probably related to my local system configuration.
I’m not able to reproduce the issue on Codespaces, for example.
However, I can still reproduce it on my Mac, even with a fresh clone of the repository.
I’ll drop it for now, this is likely too much of an edge case to consider.

@askoufis askoufis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've got a few questions and suggestions but overall this looks good. I'll do another review once the feedback has been addressed and I get more time to run the fixtures locally.

Comment thread package.json Resolved
Comment thread packages/turbopack-plugin/package.json Resolved
Comment thread packages/compiler/src/compiler.ts Resolved
Comment thread packages/next-plugin/src/index.ts Resolved
Comment thread packages/css/package.json Resolved
Comment thread packages/turbopack-plugin/src/index.ts Resolved
Comment thread site/docs/integrations/next.md Resolved
@RJWadley

Copy link
Copy Markdown
Contributor Author

@askoufis thanks for the review! addressed feedback and replied to comments, I will leave resolving them up to you at your satisfaction.

if (
type === 'next-12-pages-router' ||
type === 'next-13-app-router' ||
type === 'next-16-app-pages-router'

@askoufis askoufis Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably worth addressing in a separate PR, but this change alone isn't enough to run the next 16 fixture.

next.config.js is hardcoded as the config file because next 12 and 13 don't support TypeScript config, but next 16 does.

But even when I hardcode .ts config, the next 16 fixture starts but navigating to the sprinkles page results in Styles were unable to be assigned to a file errors.

Another thing I noticed when running the built next 16 app was that navigating to a fixture page then going back and navigating to another page accumulated stylesheets from both pages. You can keep doing this for all the pages in the app, resulting in some broken styles.

The most obvious way to see this to go to the duplication test page, then back, and then the sprinkles back and then back again. You end up with body styles from both pages.

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.

I actually had lots of issues trying to use the start-fixture CLI with next. Since I don't think it's really representative of the way folks run next in reality, I didn't invest much time in trying to fix it and opted instead to use the official tooling. See tests/servers.ts for more details. I actually quite liked doing it this way, and am considering reworking the fixture system in a follow-up to make testing VE more consistent across all the supported bundlers.

With respect to the global styles "leaking" across pages, this is currently expected behavior per the next docs and the react docs. React makes no attempt to clean up stylesheets when components that use them unmount. I have invested a lot of time and energy into trying to fix that in upstream react, but it is actually quite complex to do universally with good performance. I imagine it won't be fixed until some youtuber makes a clickbait video with a title like "I made react's styles 40 billion times faster" at which point the facebook geniuses will take notice and write some really good code to fix it for real

I wouldn't mind attempting to fix this for vanilla extract specifically, but it definitely belongs in a future PR (if at all).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fair enough. I think it makes more sense to handle Next.js separately from the rest of the bundler integrations, so happy for more effort to be put into that in the future.

I do want to make the fixture CLI much better for all bundlers at some point.

Re: react - I'm not sure how much framework-specific stuff we want to put into Vanilla Extract, if any. If there's a need for it then maybe it's worth working on in the future.

@askoufis askoufis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Made a few tweaks to the changesets and docs, and had to explicitly turn on turbopack in the next 16 fixture after the default changed.

I'm happy with where this has landed. Thanks @RJWadley for all your hard work on this.

@askoufis
askoufis merged commit c0e2812 into vanilla-extract-css:master Mar 15, 2026
9 of 10 checks passed
@askoufis askoufis mentioned this pull request Mar 15, 2026
@askoufis

Copy link
Copy Markdown
Contributor

Turbopack support has been released in v2.5.0 of the next plugin.

@plexey

plexey commented Mar 16, 2026 •

Copy link
Copy Markdown

Thank you so much for taking the time to add Turbopack support @askoufis @RJWadley & others.

Among other benefits, this work has significantly improved our build times in CI from 6 minutes down to 2 minutes, which is a huge win.

Your hard work doesn't go unnoticed and is super appreciated ❤️

@nandahibatullah

Copy link
Copy Markdown

Echoing @plexey's appreciation here @RJWadley and @askoufis + everyone else involved.

Thank you for all the time you put into this. I happen to work with @plexey and we are enjoying the improved build times - so huge for us. Mega effort 🔥

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.

Cannot build a next app with Turbopack (Styles were unable to be assigned to a file)