fix(css): fix lightningcss dep url resolution with custom root (#18125)

This commit is contained in:
Hiroshi Ogawa 2024-09-26 17:22:38 +09:00 committed by GitHub
parent 70813c7f05
commit eb08f605dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 52 additions and 1 deletions

View File

@ -3095,7 +3095,10 @@ async function compileLightningCSS(
}
deps.add(dep.url)
if (urlReplacer) {
const replaceUrl = await urlReplacer(dep.url, dep.loc.filePath)
const replaceUrl = await urlReplacer(
dep.url,
toAbsolute(dep.loc.filePath),
)
css = css.replace(dep.placeholder, () => replaceUrl)
} else {
css = css.replace(dep.placeholder, () => dep.url)

View File

@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import { getBg, isBuild, page, viteTestUrl } from '~utils'
test('url dependency', async () => {
const css = await page.$('.url-dep')
expect(await getBg(css)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
})

View File

@ -0,0 +1,14 @@
{
"name": "@vitejs/test-css-lightningcss-root",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"devDependencies": {
"lightningcss": "^1.27.0"
}
}

View File

@ -0,0 +1,3 @@
<p>url() dependency</p>
<div class="url-dep"></div>
<script type="module" src="./main.js"></script>

View File

@ -0,0 +1 @@
import './url-dep.css'

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -0,0 +1,7 @@
.url-dep {
background-image: url('./ok.png');
background-size: cover;
width: 50px;
height: 50px;
border: 1px solid black;
}

View File

@ -0,0 +1,8 @@
import { defineConfig } from 'vite'
export default defineConfig({
root: 'root',
css: {
transformer: 'lightningcss',
},
})

View File

@ -598,6 +598,12 @@ importers:
specifier: ^1.27.0
version: 1.27.0
playground/css-lightningcss-root:
devDependencies:
lightningcss:
specifier: ^1.27.0
version: 1.27.0
playground/css-no-codesplit: {}
playground/css-sourcemap: