Skip to content

Commit 8517645

Browse files
MarabyteArnaudBarre
andauthoredJul 3, 2023
fix: lightningCSS should load external URL in CSS file (#13692)
Co-authored-by: Arnaud Barré <arnaud.barre@carbometrix.com>
1 parent 277e844 commit 8517645

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed
 

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,14 @@ async function rewriteCssImageSet(
14201420
return url
14211421
})
14221422
}
1423+
function skipUrlReplacer(rawUrl: string) {
1424+
return (
1425+
isExternalUrl(rawUrl) ||
1426+
isDataUrl(rawUrl) ||
1427+
rawUrl[0] === '#' ||
1428+
varRE.test(rawUrl)
1429+
)
1430+
}
14231431
async function doUrlReplace(
14241432
rawUrl: string,
14251433
matched: string,
@@ -1433,12 +1441,7 @@ async function doUrlReplace(
14331441
rawUrl = rawUrl.slice(1, -1)
14341442
}
14351443

1436-
if (
1437-
isExternalUrl(rawUrl) ||
1438-
isDataUrl(rawUrl) ||
1439-
rawUrl[0] === '#' ||
1440-
varRE.test(rawUrl)
1441-
) {
1444+
if (skipUrlReplacer(rawUrl)) {
14421445
return matched
14431446
}
14441447

@@ -2184,6 +2187,10 @@ async function compileLightningCSS(
21842187
for (const dep of res.dependencies!) {
21852188
switch (dep.type) {
21862189
case 'url':
2190+
if (skipUrlReplacer(dep.url)) {
2191+
css = css.replace(dep.placeholder, dep.url)
2192+
break
2193+
}
21872194
deps.add(dep.url)
21882195
if (urlReplacer) {
21892196
css = css.replace(dep.placeholder, await urlReplacer(dep.url, id))

‎playground/css-lightningcss/__tests__/lightningcss.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect, test } from 'vitest'
22
import {
33
editFile,
44
findAssetFile,
5+
getBg,
56
getColor,
67
isBuild,
78
page,
@@ -65,3 +66,8 @@ test.runIf(isBuild)('minify css', async () => {
6566
expect(cssFile).toMatch('rgba(')
6667
expect(cssFile).not.toMatch('#ffff00b3')
6768
})
69+
70+
test('css with external url', async () => {
71+
const css = await page.$('.external')
72+
expect(await getBg(css)).toMatch('url("https://vitejs.dev/logo.svg")')
73+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.external {
2+
background-image: url('https://vitejs.dev/logo.svg');
3+
background-size: 100%;
4+
width: 200px;
5+
height: 200px;
6+
background-color: #bed;
7+
}

‎playground/css-lightningcss/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ <h1>Lightning CSS</h1>
2323

2424
<p>Inline CSS module:</p>
2525
<pre class="modules-inline"></pre>
26+
27+
<p>External URL</p>
28+
<div class="external"></div>
2629
</div>
2730

2831
<script type="module" src="./main.js"></script>

‎playground/css-lightningcss/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import './minify.css'
22
import './imported.css'
33
import mod from './mod.module.css'
4+
import './external-url.css'
45

56
document.querySelector('.modules').classList.add(mod['apply-color'])
67
text('.modules-code', JSON.stringify(mod, null, 2))

0 commit comments

Comments
 (0)
Please sign in to comment.