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 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/vite/src/node/__tests__/plugins/css.spec.ts
Expand Up @@ -122,6 +122,12 @@ 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 with semicolon in quotes', async () => {
const css = `.foo{color:red;}@import "bla;bar";`
const result = await hoistAtRules(css)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -1112,7 +1112,7 @@ 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.replace(/@import\s*(?:url\()?(?:"[^"]*"|'[^']*'|[^;]*).*?;/gm, (match) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we keep the same pattern?

Suggested change
s.replace(/@import\s*(?:url\()?(?:"[^"]*"|'[^']*'|[^;]*).*?;/gm, (match) => {
s.replace(/@import\s*(?:url\([^\)]*\)|"[^"]*"|'[^']*'|[^;]*).*?;/gm, (match) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw your suggestion after my commit 72f03bc !

I used the same pattern, but not quite the same way as what you suggest. What do you prefer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I think it is better to have the url( ... ) follow the exact same pattern as the others 🤔
At least to me, it is easier to follow. If you think that both versions are doing the same, then let's go with the one I suggested. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is 4fd2810 !

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is still not the same, no? What I'm proposing is to have a single non-capturing group instead of two.

Current one:

/@import\s*(?:url\()?(?:[^)]*\)|"[^"]*"|'[^']*'|[^;]*).*?;/gm

Proposal:

/@import\s*(?:url\([^\)]*\)|"[^"]*"|'[^']*'|[^;]*).*?;/gm

Or maybe I'm missing something 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied this change in chore: simplify regex 👍🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry, I misread your regexp... Thanks for the fix, I was not available for the last hour!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! Thanks again for the quick report and PR! And eager to see Histoire hit the public hopefully soon 👏🏼

s.appendLeft(0, match)
return ''
})
Expand Down