Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): resolve url aliases with fragments (fix: #17690) #17691

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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