Skip to content

Commit

Permalink
fix(css): run rewrite plugin if postcss plugin exists (#19371)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Feb 6, 2025
1 parent 17b0f6e commit bcdb51a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,11 @@ async function compileCSS(

if (
urlResolver &&
// if there's an @import, we need to add this plugin
// regradless of whether it contains url() or image-set(),
// because we don't know the content referenced by @import
(needInlineImport || hasUrl)
// when a postcss plugin is used (including the internal postcss plugins),
// we need to add this plugin regardless of whether
// this file contains url() or image-set(),
// because we don't know the content injected by those plugins
(postcssPlugins.length > 0 || isModule || hasUrl)
) {
postcssPlugins.push(
UrlRewritePostcssPlugin({
Expand Down
8 changes: 8 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ test('postcss config', async () => {
await untilUpdated(() => getColor(imported), 'red')
})

test('postcss plugin that injects url()', async () => {
const imported = await page.$('.postcss-inject-url')
// alias should be resolved
expect(await getBg(imported)).toMatch(
/localhost(?::\d+)?\/(?:assets\/)?ok.*\.png/,
)
})

sassTest()

test('less', async () => {
Expand Down
3 changes: 3 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ <h1>CSS</h1>
<p class="postcss">
<span class="nesting">PostCSS nesting plugin: this should be pink</span>
</p>
<p class="postcss-inject-url">
PostCSS plugin: this should have a background image
</p>

<p class="sass">SASS: This should be orange</p>
<p class="sass-at-import">
Expand Down
1 change: 1 addition & 0 deletions playground/css/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './less.less'
import './less-plugin.less'
import './stylus.styl'
import './manual-chunk.css'
import './postcss-inject-url.css'

import urlCss from './url-imported.css?url'
appendLinkStylesheet(urlCss)
Expand Down
1 change: 1 addition & 0 deletions playground/css/postcss-inject-url.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@inject-url;
24 changes: 23 additions & 1 deletion playground/css/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { normalizePath } from 'vite'
import postcssNested from 'postcss-nested'

export default {
plugins: [postcssNested, testDirDep, testSourceInput],
plugins: [postcssNested, testDirDep, testSourceInput, testInjectUrl],
}

/**
Expand Down Expand Up @@ -61,3 +61,25 @@ function testSourceInput() {
}
}
testSourceInput.postcss = true

function testInjectUrl() {
return {
postcssPlugin: 'inject-url',
Once(root, { Rule }) {
root.walkAtRules('inject-url', (atRule) => {
const rule = new Rule({
selector: '.postcss-inject-url',
source: atRule.source,
})
rule.append({
prop: 'background',
value: "url('=/ok.png')",
source: atRule.source,
})
atRule.after(rule)
atRule.remove()
})
},
}
}
testInjectUrl.postcss = true

0 comments on commit bcdb51a

Please sign in to comment.