## Summary Fixes https://github.com/tailwindlabs/tailwindcss/issues/20570 Vite adds its own `postcss-import` before the PostCSS plugins of the user. Thus, when `@tailwindcss/postcss` runs, the `@import`-ed stylesheets are already inlined. Their relative `url(…)`s are still relative to their own file. At the end, Vite resolves each `url(…)` against the `source.input.file` of its declaration. Without optimization, we keep the `source` of each node (since https://github.com/tailwindlabs/tailwindcss/pull/17775), so this works. With optimization, we parse the output of Lightning CSS again, and every node gets the input file as its `source`. `optimize` is on by default when `NODE_ENV` is `production`, and `vite build` sets it. Vite then looks for the asset next to the input file, does not find it, and keeps the URL as it is: ``` ./fonts/ExampleSans.woff2 referenced in ./fonts/ExampleSans.woff2 didn't resolve at build time, it will remain unchanged to be resolved at runtime ``` This is the problem that https://github.com/tailwindlabs/tailwindcss/issues/16636 and https://github.com/tailwindlabs/tailwindcss/issues/16700 reported. https://github.com/tailwindlabs/tailwindcss/pull/16965 fixed URL rewriting only for the stylesheets that we inline ourselves. `transformAssetUrls` (https://github.com/tailwindlabs/tailwindcss/pull/18321) has no effect here. ## Solution When we optimize, we now rewrite the relative `url(…)`s of declarations that come from another file, so that they are relative to the input file. This is the same rewrite that we do for the stylesheets that we inline ourselves. After the rewrite, the URLs match the `source` that the optimized nodes get. - We rewrite the AST that we get from PostCSS, before we compile it. The stylesheets that we load ourselves are not in that AST yet, so we do not rewrite their URLs two times. - Without optimization, nothing changes, because the nodes keep their `source`. - `transformAssetUrls: false` disables the rewrite. To share the code, `rewriteUrls` in `@tailwindcss/node` now uses a new `rewriteUrlsInAst` function. A callback gives it the base folder of each declaration. This also changes optimized builds of other setups that run `postcss-import` before `@tailwindcss/postcss`, like `postcss-cli`. Their URLs are now relative to the input file, like the URLs of the stylesheets that we inline ourselves. ## Test plan - Added unit tests in `packages/@tailwindcss-postcss/src/index.test.ts`. They run `postcss-import` before the plugin with `optimize` on, with `optimize` off, and with `transformAssetUrls: false`. The test with `optimize` on fails on `main`. - Added an integration test in `integrations/postcss/url-rewriting.test.ts`. It runs `vite build` with `@tailwindcss/postcss` in a PostCSS config. Without this change, Vite emits no fonts and the test fails. With this change, it passes. - `pnpm vitest run`: 5598 passed. - `pnpm vitest --root=./integrations run` on `postcss/url-rewriting`, `vite/url-rewriting`, `postcss/index`, `postcss/source`, `postcss/plugins`, `postcss/config`, `postcss/core-as-postcss-plugin` and `postcss/multi-root`: all passed. I did not run `postcss/next.test.ts`. - Built a Vite 8.2.0 project with a font CSS file in a package and one in a local folder. With `optimize: true`, the fonts are now emitted to `dist/assets` and the URLs are rewritten, the same as with `optimize: false`.
A utility-first CSS framework for rapidly building custom user interfaces.
Documentation
For full documentation, visit tailwindcss.com.
Community
For help, discussion about best practices, or feature ideas:
Discuss Tailwind CSS on GitHub
Contributing
If you're interested in contributing to Tailwind CSS, please read our contributing docs before submitting a pull request.
@tailwindcss/postcss plugin API
Changing where the plugin searches for source files
You can use the base option (defaults to the current working directory) to change the directory in which the plugin searches for source files:
import tailwindcss from '@tailwindcss/postcss'
export default {
plugins: [
tailwindcss({
base: path.resolve(__dirname, './path'),
}),
],
}
Enabling or disabling Lightning CSS
By default, this plugin detects whether or not the CSS is being built for production by checking the NODE_ENV environment variable. When building for production Lightning CSS will be enabled otherwise it is disabled.
If you want to always enable or disable Lightning CSS the optimize option may be used:
import tailwindcss from '@tailwindcss/postcss'
export default {
plugins: [
tailwindcss({
// Enable or disable Lightning CSS
optimize: false,
}),
],
}
It's also possible to keep Lightning CSS enabled but disable minification:
import tailwindcss from '@tailwindcss/postcss'
export default {
plugins: [
tailwindcss({
optimize: { minify: false },
}),
],
}
Enabling or disabling url(…) rewriting
Our PostCSS plugin can rewrite url(…)s for you since it also handles @import (no postcss-import is needed). This feature is enabled by default.
In some situations the bundler or framework you're using may provide this feature itself. In this case you can set transformAssetUrls to false to disable this feature:
import tailwindcss from '@tailwindcss/postcss'
export default {
plugins: [
tailwindcss({
// Disable `url(…)` rewriting
transformAssetUrls: false,
// Enable `url(…)` rewriting (the default)
transformAssetUrls: true,
}),
],
}