Skip to content

Commit

Permalink
fix(lightningcss): search for assets with correct base path (#17856)
Browse files Browse the repository at this point in the history
  • Loading branch information
smeng9 authored Aug 11, 2024
1 parent e961b31 commit 4e5ce3c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,7 @@ async function compileLightningCSS(
}
deps.add(dep.url)
if (urlReplacer) {
const replaceUrl = await urlReplacer(dep.url, id)
const replaceUrl = await urlReplacer(dep.url, dep.loc.filePath)
css = css.replace(dep.placeholder, () => replaceUrl)
} else {
css = css.replace(dep.placeholder, () => dep.url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isBuild,
page,
untilUpdated,
viteTestUrl,
} from '~utils'

// note: tests should retrieve the element at the beginning of test and reuse it
Expand Down Expand Up @@ -71,3 +72,10 @@ test('css with external url', async () => {
const css = await page.$('.external')
expect(await getBg(css)).toMatch('url("https://vitejs.dev/logo.svg")')
})

test('nested css with relative asset', async () => {
const css = await page.$('.nested-css-relative-asset')
expect(await getBg(css)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
})
1 change: 1 addition & 0 deletions playground/css-lightningcss/imported.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import url('./nested/nested.css');
@import './imported-at-import.css';

.imported {
Expand Down
3 changes: 3 additions & 0 deletions playground/css-lightningcss/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ <h1>Lightning CSS</h1>

<p>External URL</p>
<div class="external"></div>

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

<script type="module" src="./main.js"></script>
5 changes: 5 additions & 0 deletions playground/css-lightningcss/nested/nested.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.nested-css-relative-asset {
background-image: url('../ok.png');
width: 50px;
height: 50px;
}

0 comments on commit 4e5ce3c

Please sign in to comment.