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: handle url imports with semicolon (fix #7717) #7718

Merged
merged 4 commits into from Apr 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Expand Up @@ -122,6 +122,20 @@ describe('hoist @ rules', () => {
expect(result).toBe(`@import "bla";.foo{color:red;}`)
})

test('hoist @import url with semicolon', async () => {
const css = `.foo{color:red;}@import url("bla;bla");`
const result = await hoistAtRules(css)
expect(result).toBe(`@import url("bla;bla");.foo{color:red;}`)
})

test('hoist @import url data with semicolon', async () => {
const css = `.foo{color:red;}@import url(data:image/png;base64,iRxVB0);`
const result = await hoistAtRules(css)
expect(result).toBe(
`@import url(data:image/png;base64,iRxVB0);.foo{color:red;}`
)
})

test('hoist @import with semicolon in quotes', async () => {
const css = `.foo{color:red;}@import "bla;bar";`
const result = await hoistAtRules(css)
Expand Down
11 changes: 7 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -1112,10 +1112,13 @@ export async function hoistAtRules(css: string) {
// CSS @import can only appear at top of the file. We need to hoist all @import
// to top when multiple files are concatenated.
// match until semicolon that's not in quotes
s.replace(/@import\s*(?:"[^"]*"|'[^']*'|[^;]*).*?;/gm, (match) => {
s.appendLeft(0, match)
return ''
})
s.replace(
/@import\s*(?:url\([^\)]*\)|"[^"]*"|'[^']*'|[^;]*).*?;/gm,
(match) => {
s.appendLeft(0, match)
return ''
}
)
// #6333
// CSS @charset must be the top-first in the file, hoist the first to top
let foundCharset = false
Expand Down