Skip to content

@tailwindcss/postcss in a Vite production build: relative url() from imported CSS is not rewritten (optimize drops source.input.file) #20570

Closed

Description

@Chaosnico9000

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

https://github.com/Chaosnico9000/tailwind-postcss-import-url-repro

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

  1. Vite prepends its own postcss-import to the PostCSS plugin list, so the @imports are already inlined when @tailwindcss/postcss runs. Tailwind's own @import handling (and with it transformAssetUrls) never sees them. Vite also appends its URL-rewrite plugin, which resolves each url() against the declaration's source.input.file. Before Tailwind runs, that is still the imported file (example-font/fonts.css).
  2. Without optimization, @tailwindcss/postcss rebuilds the PostCSS tree with cssAstToPostCssAst(postcss, tailwindCssAst, root.source), which keeps each node's original source. That is why dev works.
  3. optimize defaults to NODE_ENV === 'production', which vite build sets. The optimized path serializes the AST, runs Lightning CSS, and reparses the result with postcss.parse(optimized.code, result.opts). Every node now has src/index.css as its input file.
  4. Vite looks for ./fonts/ExampleSans.woff2 next to src/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 restore source on the reparsed nodes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions