Skip to content

Commit

Permalink
fix(css): referencing aliased svg asset with lightningcss enabled err…
Browse files Browse the repository at this point in the history
…ored (#18819)
  • Loading branch information
sapphi-red authored Nov 28, 2024
1 parent dac7992 commit ae68958
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3195,7 +3195,6 @@ async function compileLightningCSS(
css = css.replace(dep.placeholder, () => dep.url)
break
}
deps.add(dep.url)
if (urlReplacer) {
const replaceUrl = await urlReplacer(
dep.url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,8 @@ test('nested css with relative asset', async () => {
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
})

test('aliased asset', async () => {
const bg = await getBg('.css-url-aliased')
expect(bg).toMatch('data:image/svg+xml,')
})
4 changes: 4 additions & 0 deletions playground/css-lightningcss/css-url.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.css-url-aliased {
background: url('@/fragment.svg');
background-size: 10px;
}
4 changes: 4 additions & 0 deletions playground/css-lightningcss/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ <h1>Lightning CSS</h1>

<p>Assets relative to nested CSS</p>
<div class="nested-css-relative-asset"></div>

<div class="css-url-aliased">
<span style="background: #fff">CSS background (aliased)</span>
</div>
</div>

<script type="module" src="./main.js"></script>
1 change: 1 addition & 0 deletions playground/css-lightningcss/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './minify.css'
import './imported.css'
import mod from './mod.module.css'
import './external-url.css'
import './css-url.css'

document.querySelector('.modules').classList.add(mod['apply-color'])
text('.modules-code', JSON.stringify(mod, null, 2))
Expand Down
22 changes: 22 additions & 0 deletions playground/css-lightningcss/nested/fragment.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions playground/css-lightningcss/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import path from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
css: {
transformer: 'lightningcss',
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'nested'),
},
},
build: {
cssTarget: ['chrome61'],
cssMinify: 'lightningcss',
Expand Down

0 comments on commit ae68958

Please sign in to comment.