next.js
90d78e99 - Turbopack: rewrite CSS url() matched by a `type: 'asset'` rule (#102635)

Commit
1 day ago
Turbopack: rewrite CSS url() matched by a `type: 'asset'` rule (#102635) ### What? With Turbopack, a CSS `url()` that matches a `turbopack.rules` entry with `type: 'asset'` was left pointing at its source path in the emitted CSS, so the image 404'd. For example: ```js // next.config.js turbopack: { rules: { '*.webp': { condition: { query: '?marker' }, type: 'asset' }, }, } ``` ```css .logo { background-image: url('../images/logo.webp?marker'); } ``` The same file imported from JS with the same query, and the same `url()` without the query, already resolved to the emitted `/_next/static/media/...` asset. Now the matched CSS `url()` resolves to that asset too. ### Why? `type: 'asset'` maps to a JS static-URL module. User rules match before the built-in rule that gives CSS `url()` references a CSS-embeddable static asset module, and the first module type that matches wins. So a CSS `url()` matched by the user rule became a module CSS can't embed, and the URL was never replaced. ### How? The fix is in rule construction rather than at module creation time. A user rule whose configured module type needs a different module type for CSS `url()` references (currently only `asset`) is split into two rules with mutually exclusive conditions: - user conditions **and** a CSS `url()` reference → loaders (if any) + the CSS-embeddable static asset module - user conditions **and not** a CSS `url()` reference → loaders (if any) + the existing asset module A reference matches exactly one of the two, so loaders still run once, including when a loader renames the module and rules are re-applied. JS imports and all other configured module types are unchanged. ### Verification - New e2e test `test/e2e/app-dir/turbopack-css-url-asset-rule`: - Before the fix, it fails for the queried CSS `url()`: the URL resolves to `/_next/static/immutable/images/logo.webp`, the source path. The two controls (the `url()` without the query and the queried JS import) pass. - After the fix, all 3 pass with `pnpm test-start-turbo` (with Cache Components on and off) and with `pnpm test-dev-turbo`. - Under webpack the suite is skipped via `@force-gate turbopack`, because webpack doesn't read `turbopack.rules`. - `pnpm test-start-turbo test/e2e/turbopack-loader-config` (existing test, confirms rule and loader behavior is unchanged) - `cargo test -p turbopack-tests --test execution -- css` (existing CSS execution tests) - Not run: the new e2e test in deploy mode. CI covers it. <!-- NEXT_JS_LLM --> <!-- fleet 10ba875b-5afb-45ca-8091-2d04d672b6ba --> Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>
Author
Parents
Loading