[unplugin] Vite dev: import walk calls this.load() on dependencies (react), deadlocking the cold-start dependency optimizer
Describe the issue
In Vite dev, the unplugin's transform walks every import of a file that uses StyleX and calls this.load() on each one, react included:
// packages/@stylexjs/unplugin/src/core.js, transform(), still on main
if (ctx && ctx.meta && ctx.meta.watchMode && typeof ctx.parse === 'function') {
const ast = ctx.parse(result.code);
for (const stmt of ast.body) {
if (stmt.type === 'ImportDeclaration') {
const resolved = await ctx.resolve(stmt.source.value, id);
if (resolved && !resolved.external) {
const loaded = await ctx.load(resolved); // <- also for 'react', 'react-dom/client', ...
...On a cold start (no dependency cache, or vite --force), loading a pre-bundled dependency waits for Vite's dependency optimizer. The optimizer holds those requests until the initial static-import crawl is done, and the crawl waits for this transform to return. The transform waits for load(). Nothing moves: the first request for the entry module never gets a response, and the page never loads. Nothing is logged.
With a warm dependency cache the same project loads normally, which makes this look random: it only bites after a lockfile change, a cache wipe, or --force.
Expected behavior
The first page load on a cold start completes. Either the walk skips bare/dependency imports (anything resolved into node_modules or the optimizer's .vite/deps), or it doesn't await load() for them, or it doesn't run under vite serve at all. Every StyleX file reports its own rules when Vite transforms it anyway.
Steps to reproduce
Windows 11 Pro (10.0.26200), Node 20.19.0, Vite 5.4.21, React 18.3.1, @stylexjs/unplugin / @stylexjs/stylex / @stylexjs/babel-plugin 0.19.1. We only tested on Windows, but the mechanism doesn't look Windows-specific: it's the Vite optimizer's cold-start ordering.
vite.config.mjs:import { defineConfig } from 'vite'; import stylex from '@stylexjs/unplugin/vite'; export default defineConfig({ plugins: [stylex({ dev: true, devMode: 'full', runtimeInjection: false, useCSSLayers: true })], });src/main.jsimportsreact,react-dom/clientand@stylexjs/stylexand callsstylex.create:plus animport { createElement } from 'react'; import { createRoot } from 'react-dom/client'; import * as stylex from '@stylexjs/stylex'; const styles = stylex.create({ hello: { color: 'rebeccapurple' } }); createRoot(document.getElementById('root')).render( createElement('h1', { ...stylex.props(styles.hello), id: 'hello' }, 'hello'), );index.htmlwith<div id="root">and<script type="module" src="/src/main.js">.vite --force, thencurl -m 15 http://localhost:5173/src/main.js.
Test case
Measured with the repro above:
| start | GET /src/main.js |
|---|---|
vite --force (cold), plugin as shipped |
no response after 15 s (curl timeout); the browser's load never fires |
vite (warm cache), plugin as shipped |
200 in 0.05 s |
vite --force (cold), import walk disabled (workaround below) |
200 in 0.02 s; page renders with the StyleX colour |
Additional comments
Workaround we run: give the hook a context that reports watchMode: false, which skips the walk:
export function withoutStylexImportWalk(plugins: Plugin | Plugin[]): Plugin[] {
return (Array.isArray(plugins) ? plugins : [plugins]).map((plugin) => {
const hook = plugin.transform;
if (typeof hook !== 'function') return plugin;
return {
...plugin,
transform(this: ThisParameterType<typeof hook>, ...args: Parameters<typeof hook>) {
const context = new Proxy(this, {
get: (target, key) => (key === 'meta' ? { ...target.meta, watchMode: false } : Reflect.get(target, key)),
});
return hook.apply(context, args);
},
};
});
}
// plugins: [...withoutStylexImportWalk(stylex({ ... }))]With it, our app's dev server serves every StyleX rule: we compared the computed style of every element on 93 screens against a pre-StyleX build and found no missing rules. The walk looks meant for Rollup watch mode (cached modules not being re-transformed), and there shouldTransformCachedModule already covers it.
Source: facebook/stylex