镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Enable URL rewriting for PostCSS - #16965

Merged
philipp-spiess merged 8 commits into
mainfrom
fix/vite-postcss-rebase
Mar 7, 2025
Merged

philipp-spiess merged 8 commits into
mainfrom
fix/vite-postcss-rebase

Conversation

@philipp-spiess

@philipp-spiess philipp-spiess commented Mar 5, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #16636

This PR enables URL rebasing for PostCSS. Furthermore it fixes an issue where transitive imports rebased against the importer CSS file instead of the input CSS file. While fixing this we noticed that this is also broken in Vite right now and that our integration test swallowed that when testing because it did not import any Tailwind CSS code and thus was not considered a Tailwind file.

Test plan

@philipp-spiess
philipp-spiess requested a review from a team as a code owner March 5, 2025 16:05
@philipp-spiess
philipp-spiess marked this pull request as draft March 5, 2025 16:05
@philipp-spiess
philipp-spiess force-pushed the fix/vite-postcss-rebase branch from abaa114 to 89671f0 Compare March 6, 2025 13:31
@philipp-spiess
philipp-spiess force-pushed the fix/vite-postcss-rebase branch from 89671f0 to f8ebd54 Compare March 6, 2025 13:39
@philipp-spiess philipp-spiess changed the title WIP Enable URL rewriting for PostCSS Mar 6, 2025
@philipp-spiess
philipp-spiess force-pushed the fix/vite-postcss-rebase branch from eef25ae to e989430 Compare March 6, 2025 13:44
@philipp-spiess
philipp-spiess marked this pull request as ready for review March 6, 2025 13:49
Comment thread CHANGELOG.md Resolved
Comment thread CHANGELOG.md Resolved
@philipp-spiess
philipp-spiess merged commit 225f323 into main Mar 7, 2025
@philipp-spiess
philipp-spiess deleted the fix/vite-postcss-rebase branch March 7, 2025 11:18
RobinMalfait added a commit that referenced this pull request Oct 14, 2026
…` in optimized builds (#20576)

## Summary

Fixes #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
#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
#16636 and
#16700 reported.
#16965 fixed URL
rewriting only for the stylesheets that we inline ourselves.
`transformAssetUrls`
(#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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@tailwindcss/postcss fails to resolve font URLs in external packages with Vite

3 participants