Skip to content

Commit

Permalink
fix(css/assets): properly replace multiple css asset urls on the same…
Browse files Browse the repository at this point in the history
… line

fix #1975
  • Loading branch information
yyx990803 committed Feb 11, 2021
1 parent 8b3d0ea commit 1d805a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ describe('css url() references', () => {
expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match)
})

test('multiple urls on the same line', async () => {
const bg = await getBg('.css-url-same-line')
expect(bg).toMatch(assetMatch)
expect(bg).toMatch(iconMatch)
})

if (isBuild) {
test('preserve postfix query/hash', () => {
expect(findAssetFile(/\.css$/, 'foo')).toMatch(`woff2?#iefix`)
Expand Down
5 changes: 5 additions & 0 deletions packages/playground/assets/css/css-url.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ <h2>CSS url references</h2>
<div class="css-url-quotes-base64-inline">
<span style="background: #fff">CSS background (base64 inline in prod)</span>
</div>
<div class="css-url-same-line">
<span style="background: #fff"
>CSS background (multiple urls on same line)</span
>
</div>

<h2>SVG Fragments</h2>
<div>
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { PluginContext, RenderedChunk } from 'rollup'
import MagicString from 'magic-string'
import { createHash } from 'crypto'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:(.*?)__)?/g
export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g

// urls in JS must be quoted as strings, so when replacing them we need
// a different regex
const assetUrlQuotedRE = /"__VITE_ASSET__([a-z\d]{8})__(?:(.*?)__)?"/g
const assetUrlQuotedRE = /"__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?"/g

const rawRE = /(\?|&)raw(?:&|$)/
const urlRE = /(\?|&)url(?:&|$)/
Expand Down Expand Up @@ -234,7 +234,7 @@ async function fileToBuiltUrl(
})
}

url = `__VITE_ASSET__${contentHash}__${postfix ? `${postfix}__` : ``}`
url = `__VITE_ASSET__${contentHash}__${postfix ? `$_${postfix}__` : ``}`
}

cache.set(id, url)
Expand Down

0 comments on commit 1d805a6

Please sign in to comment.