Skip to content

Commit

Permalink
fix(css): resolve url aliases with fragments (fix: #17690) (#17691)
Browse files Browse the repository at this point in the history
Co-authored-by: Stanislav Lashmanov <slashmanov@gitlab.com>
  • Loading branch information
CyberAP and Stanislav Lashmanov authored Jul 23, 2024
1 parent 3c1bde3 commit d906d3f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,10 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
return joinUrlSegments(config.base, decodedUrl)
}
}
const resolved = await resolveUrl(decodedUrl, importer)
const [id, fragment] = decodedUrl.split('#')
let resolved = await resolveUrl(id, importer)
if (resolved) {
if (fragment) resolved += '#' + fragment
return fileToUrl(resolved, config, this)
}
if (config.command === 'build') {
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ describe('svg fragments', () => {
: /svg#icon-heart-view$/,
)
})

test('url with an alias', async () => {
expect(await getBg('.icon-clock-alias')).toMatch(
/\.svg#icon-clock-view"\)$/,
)
})
})

test('Unknown extension assets import', async () => {
Expand Down
4 changes: 4 additions & 0 deletions playground/assets/css/icons.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ img {
.icon-arrow-right {
background: url(../nested/fragment-bg.svg#icon-arrow-right-view) no-repeat;
}

.icon-clock-alias {
background: url('fragment#icon-clock-view') no-repeat;
}
1 change: 1 addition & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ <h2>SVG Fragments via CSS background url</h2>
<span class="icon icon-clock"></span>
<span class="icon icon-heart"></span>
<span class="icon icon-arrow-right"></span>
<span class="icon icon-clock-alias"></span>
</div>

<h2>SVG Fragments via JS Import</h2>
Expand Down
1 change: 1 addition & 0 deletions playground/assets/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, 'nested'),
fragment: path.resolve(__dirname, 'nested/fragment-bg.svg'),
},
},
assetsInclude: ['**/*.unknown'],
Expand Down

0 comments on commit d906d3f

Please sign in to comment.