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: Correctly process urls when they are rewritten to contain space #7452

Merged
merged 3 commits into from Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -54,6 +54,12 @@ test('css import from js', async () => {
await untilUpdated(() => getColor(atImport), 'blue')
})

test('css import asset with space', async () => {
const importedWithSpace = await page.$('.import-with-space')

expect(await getBg(importedWithSpace)).toMatch(/.*ok\..*png/)
})

test('postcss config', async () => {
const imported = await page.$('.postcss .nesting')
expect(await getColor(imported)).toBe('pink')
Expand Down
Binary file added packages/playground/css/folder with space/ok.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/playground/css/folder with space/space.css
@@ -0,0 +1,5 @@
.import-with-space {
color: green;
background: url(spacefolder/ok.png);
background-position: center;
}
1 change: 1 addition & 0 deletions packages/playground/css/imported.css
@@ -1,4 +1,5 @@
@import './imported-at-import.css';
@import 'spacefolder/space.css';

.imported {
color: green;
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -10,6 +10,10 @@ <h1>CSS</h1>
<p class="imported-at-import">
@import in import from js: This should be purple
</p>
<p class="import-with-space">
@import from file with space: This should be green and have a background
image
</p>
<p>Imported css string:</p>
<pre class="imported-css"></pre>
<pre class="imported-css-glob"></pre>
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/css/vite.config.js
Expand Up @@ -9,7 +9,8 @@ module.exports = {
},
resolve: {
alias: {
'@': __dirname
'@': __dirname,
spacefolder: __dirname + '/folder with space'
}
},
css: {
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -997,7 +997,12 @@ async function doUrlReplace(
return matched
}

return `url(${wrap}${await replacer(rawUrl)}${wrap})`
const newUrl = await replacer(rawUrl)
if (wrap === '' && newUrl != encodeURI(newUrl)) {
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
// The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement
wrap = "'"
}
return `url(${wrap}${newUrl}${wrap})`
}

async function doImportCSSReplace(
Expand Down