Skip to content

Commit d906d3f

Browse files
CyberAPStanislav Lashmanov
and
Stanislav Lashmanov
authoredJul 23, 2024··
fix(css): resolve url aliases with fragments (fix: #17690) (#17691)
Co-authored-by: Stanislav Lashmanov <slashmanov@gitlab.com>
1 parent 3c1bde3 commit d906d3f

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed
 

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,10 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
336336
return joinUrlSegments(config.base, decodedUrl)
337337
}
338338
}
339-
const resolved = await resolveUrl(decodedUrl, importer)
339+
const [id, fragment] = decodedUrl.split('#')
340+
let resolved = await resolveUrl(id, importer)
340341
if (resolved) {
342+
if (fragment) resolved += '#' + fragment
341343
return fileToUrl(resolved, config, this)
342344
}
343345
if (config.command === 'build') {

‎playground/assets/__tests__/assets.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@ describe('svg fragments', () => {
358358
: /svg#icon-heart-view$/,
359359
)
360360
})
361+
362+
test('url with an alias', async () => {
363+
expect(await getBg('.icon-clock-alias')).toMatch(
364+
/\.svg#icon-clock-view"\)$/,
365+
)
366+
})
361367
})
362368

363369
test('Unknown extension assets import', async () => {

‎playground/assets/css/icons.css

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ img {
2020
.icon-arrow-right {
2121
background: url(../nested/fragment-bg.svg#icon-arrow-right-view) no-repeat;
2222
}
23+
24+
.icon-clock-alias {
25+
background: url('fragment#icon-clock-view') no-repeat;
26+
}

‎playground/assets/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ <h2>SVG Fragments via CSS background url</h2>
213213
<span class="icon icon-clock"></span>
214214
<span class="icon icon-heart"></span>
215215
<span class="icon icon-arrow-right"></span>
216+
<span class="icon icon-clock-alias"></span>
216217
</div>
217218

218219
<h2>SVG Fragments via JS Import</h2>

‎playground/assets/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig({
77
resolve: {
88
alias: {
99
'@': path.resolve(__dirname, 'nested'),
10+
fragment: path.resolve(__dirname, 'nested/fragment-bg.svg'),
1011
},
1112
},
1213
assetsInclude: ['**/*.unknown'],

0 commit comments

Comments
 (0)
Please sign in to comment.