Repository navigation
Description
What version of Tailwind CSS are you using?
v4.3.3 (tailwindcss and @tailwindcss/postcss). Also reproduces with 0.0.0-insiders.1a38f84.
What build tool (or framework if it abstracts the build tool) are you using?
Vite 8.3.4, with @tailwindcss/postcss through postcss.config.mjs
What version of Node.js are you using?
v22.23.3
What browser are you using?
N/A (build output)
What operating system are you using?
Linux
Reproduction URL
Describe your issue
A package ships a font stylesheet with relative URLs, and the app imports it from its Tailwind entry CSS:
/* example-font/fonts.css */
@font-face {
font-family: "Example Sans";
src: url("./fonts/ExampleSans.woff2") format("woff2");
}/* src/index.css */
@import "tailwindcss";
@import "example-font/fonts.css";// postcss.config.mjs
export default { plugins: { "@tailwindcss/postcss": {} } };vite (dev) serves the font correctly. vite build does not emit it: the CSS keeps url(./fonts/ExampleSans.woff2), which 404s at runtime, and the only trace is a warning:
./fonts/ExampleSans.woff2 referenced in ./fonts/ExampleSans.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime
Expected: the font is emitted to dist/assets and the URL rewritten. That is what happens with @tailwindcss/vite, in dev, with NODE_ENV=development vite build, and with optimize: false (all four are in the repro). transformAssetUrls: false makes no difference. It is not specific to packages either: @import "./fonts/local.css" with a url("./font.woff2") next to it fails the same way.
This is the symptom of #16636 / #16700. #16636 was closed by #16965 (URL rewriting for stylesheets that Tailwind inlines itself, tested with postcss-cli), #16700 after #18321 added transformAssetUrls. Neither covers Vite: the original repro from #16636, updated to 4.3.3 and Vite 8.3.4, still prints all eight warnings on vite build and emits the fonts with NODE_ENV=development.
Why it happens
- Vite prepends its own
postcss-importto the PostCSS plugin list, so the@imports are already inlined when@tailwindcss/postcssruns. Tailwind's own@importhandling (and with ittransformAssetUrls) never sees them. Vite also appends its URL-rewrite plugin, which resolves eachurl()against the declaration'ssource.input.file. Before Tailwind runs, that is still the imported file (example-font/fonts.css). - Without optimization,
@tailwindcss/postcssrebuilds the PostCSS tree withcssAstToPostCssAst(postcss, tailwindCssAst, root.source), which keeps each node's original source. That is why dev works. optimizedefaults toNODE_ENV === 'production', whichvite buildsets. The optimized path serializes the AST, runs Lightning CSS, and reparses the result withpostcss.parse(optimized.code, result.opts). Every node now hassrc/index.cssas its input file.- Vite looks for
./fonts/ExampleSans.woff2next tosrc/index.css, finds nothing, and leaves the URL unchanged.
A PostCSS plugin that logs decl.source.input.file for the src declaration, placed before and after Tailwind, shows it:
# NODE_ENV=production (default for vite build)
before ./example-font/fonts.css url("./fonts/ExampleSans.woff2") format("woff2")
after ./src/index.css url(./fonts/ExampleSans.woff2)format("woff2")
# NODE_ENV=development
before ./example-font/fonts.css url("./fonts/ExampleSans.woff2") format("woff2")
after ./example-font/fonts.css url("./fonts/ExampleSans.woff2") format("woff2")
Workaround
"@tailwindcss/postcss": { optimize: false } (Vite minifies the CSS anyway), or importing the font CSS from JavaScript instead of from the Tailwind entry. Switching to @tailwindcss/vite also works, but is not always an option (see #16636, where Sass was the reason).
Possible fixes
- In the optimize path, rebase relative
url()in declarations whose source file differs from the root's input file before handing the CSS to Lightning CSS, as is already done for stylesheets Tailwind inlines itself. - Or carry the source mapping through Lightning CSS (
optimize()already accepts an input map) and restoresourceon the reparsed nodes.
Activity
Rewrite URLs of stylesheets inlined before `@tailwindcss/postcss` in …
Rewrite `url(…)`s of stylesheets inlined before `@tailwindcss/postcss…