Skip to content

Commit

Permalink
fix: Correctly process urls when they are rewritten to contain space (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Mar 27, 2022
1 parent 4c95e99 commit 9ee2cf6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2 deletions.
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 @@ -1029,7 +1029,12 @@ async function doUrlReplace(
return matched
}

return `url(${wrap}${await replacer(rawUrl)}${wrap})`
const newUrl = await replacer(rawUrl)
if (wrap === '' && newUrl !== encodeURI(newUrl)) {
// 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

0 comments on commit 9ee2cf6

Please sign in to comment.