Skip to content

Commit abb793e

Browse files
authored
fix(css): rewrite urls in OnceExit-injected content (#22983)
1 parent 04f345b commit abb793e

6 files changed

Lines changed: 52 additions & 2 deletions

File tree

‎packages/vite/src/node/plugins/css.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ const UrlRewritePostcssPlugin: PostCSS.PluginCreator<{
20952095

20962096
return {
20972097
postcssPlugin: 'vite-url-rewrite',
2098-
Once(root) {
2098+
OnceExit(root) {
20992099
const promises: Promise<void>[] = []
21002100
root.walkDecls((declaration) => {
21012101
const importer = declaration.source?.input.file
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1+
import { expect, test } from 'vitest'
2+
import { getBg, isBuild, page, viteTestUrl } from '~utils'
13
import './tests'
4+
5+
// not included in tests.ts because the lightningcss variant does not use
6+
// the postcss pipeline
7+
test('postcss plugin that injects url() at OnceExit', async () => {
8+
await page.goto(viteTestUrl)
9+
const imported = await page.$('.inject-url-once-exit')
10+
// url should be rebased against the injected source file
11+
expect(await getBg(imported)).toMatch(
12+
isBuild ? /base64/ : '/injected-source/injected-bg.png',
13+
)
14+
})

‎playground/css/index.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ <h1>CSS</h1>
2424
<span class="nesting">PostCSS nesting plugin: this should be pink</span>
2525
</p>
2626
<p class="inject-url">PostCSS plugin: this should have a background image</p>
27+
<p class="inject-url-once-exit">
28+
PostCSS plugin injecting at OnceExit: this should have a background image
29+
</p>
2730

2831
<p class="sass">SASS: This should be orange</p>
2932
<p class="sass-at-import">
69 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
@inject-url;
2+
@inject-url-once-exit;

‎playground/css/postcss.config.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { normalizePath } from 'vite'
55
import postcssNested from 'postcss-nested'
66

77
export default {
8-
plugins: [postcssNested, testDirDep, testSourceInput, testInjectUrl],
8+
plugins: [
9+
postcssNested,
10+
testDirDep,
11+
testSourceInput,
12+
testInjectUrl,
13+
testInjectUrlOnceExit,
14+
],
915
}
1016

1117
/**
@@ -83,3 +89,30 @@ function testInjectUrl() {
8389
}
8490
}
8591
testInjectUrl.postcss = true
92+
93+
/**
94+
* A plugin for testing url() rewriting in content injected at OnceExit with
95+
* its own source file (like postcss-modules inlining composed files)
96+
*/
97+
function testInjectUrlOnceExit() {
98+
return {
99+
postcssPlugin: 'inject-url-once-exit',
100+
OnceExit(root, { postcss }) {
101+
root.walkAtRules('inject-url-once-exit', (atRule) => {
102+
atRule.remove()
103+
root.prepend(
104+
postcss.parse(
105+
'.inject-url-once-exit { background: url(./injected-bg.png) }',
106+
{
107+
from: path.join(
108+
import.meta.dirname,
109+
'injected-source/injected.css',
110+
),
111+
},
112+
),
113+
)
114+
})
115+
},
116+
}
117+
}
118+
testInjectUrlOnceExit.postcss = true

0 commit comments

Comments
 (0)