Skip to content

Commit

Permalink
fix: resolve url aliases with fragments (fix: vitejs#17690)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Lashmanov committed Jul 16, 2024
1 parent 1465b20 commit 1654ef0
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 1654ef0

Please sign in to comment.