[unplugin] Vite dev on Windows: runtime/CSS URLs built with path.join reach the browser as "\\virtual:…" and never load
Describe the issue
On Windows, @stylexjs/unplugin/vite in dev (devMode: 'full') injects its runtime script and stylesheet with URLs built by Node's path.join, which produces backslashes:
// packages/@stylexjs/unplugin/src/vite.js (transformIndexHtml), still on main
src: path.join(base, '/@id/virtual:stylex:runtime'), // win32 -> "\\@id\\virtual:stylex:runtime"
href: path.join(base, DEV_CSS_PATH), // win32 -> "\\virtual:stylex.css"Vite then serializes tag attributes with JSON.stringify, so each backslash is doubled in the HTML the browser receives:
<script type="module" src="\\@id\\virtual:stylex:runtime"></script>
<link rel="stylesheet" href="\\virtual:stylex.css">A browser treats a leading \\ as //, i.e. a protocol-relative URL to another host:
\\virtual:stylex.css→//virtual:stylex.css→ hostvirtual, portstylex.css→ invalid URL, never requested\\@id\\virtual:stylex:runtime→http://id//virtual:stylex:runtime→ a request to hostid
So on Windows no StyleX CSS ever reaches the page in dev. In our app (with more on the page) the first load did not complete at all; in the minimal repro below the page loads but is unstyled.
The same path.join(base, 'virtual:stylex:css-module') is used as a module-graph id in handleHotUpdate, which on Windows can never match either.
Expected behavior
Dev URLs are URL paths, not file paths: /@id/virtual:stylex:runtime and /virtual:stylex.css (joined with path.posix.join, or plain string concatenation with the Vite base) on every OS.
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, Chromium (Playwright 1.62.1).
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.js: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">.- Start
viteon Windows with the dependency cache already warm (or with the workaround from #1912, the cold-start deadlock), thencurl http://localhost:5173/. The injected tags are the backslashed ones above. - Open the page:
#hellocomputesrgb(0, 0, 0). With the URLs fixed (workaround below) it computesrgb(102, 51, 153).
Test case
The repro above is complete (four files, no other dependencies). Measured on our machine:
| variant | injected href | #hello color |
|---|---|---|
| plugin as shipped | \\virtual:stylex.css |
rgb(0, 0, 0) |
| with the workaround | /virtual:stylex.css |
rgb(102, 51, 153) |
Additional comments
Workaround we run (a post transformIndexHtml that puts the slashes back):
export function stylexDevUrlsOnWindows(): Plugin {
return {
name: 'stylex-dev-urls-on-windows',
apply: 'serve',
enforce: 'post',
transformIndexHtml: {
order: 'post',
handler: (html) =>
html.replace(/(src|href)="\\+(@id\\+)?virtual:stylex([^"]*)"/g, (_all, attr, idPart, rest) =>
`${attr}="/${idPart ? '@id/' : ''}virtual:stylex${rest}"`),
},
};
}Suggested fix: use path.posix.join (or string concatenation that respects base) for the three dev URLs/ids in vite.js. Related but different: #1347 (dev URLs ignoring a non-root base).
Source: facebook/stylex