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(less): prevent rebasing @import url(...) #17857

Merged
merged 6 commits into from
Oct 30, 2024
Merged

Conversation

smeng9
Copy link
Contributor

@smeng9 smeng9 commented Aug 10, 2024

Description

Closes #13143

Since we called the rebaseUrls functions to rebase everything to the root file,
Next time when we use resolvers.less we must also pass the root file instead of the current dir.

Used a different method to fix the issue. See below.

EDIT @bluwy: description of issue at #17857 (comment)

Copy link

stackblitz bot commented Aug 10, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@smeng9
Copy link
Contributor Author

smeng9 commented Aug 10, 2024

Still needs a test case

@smeng9 smeng9 marked this pull request as draft August 10, 2024 05:07
@smeng9 smeng9 marked this pull request as ready for review August 10, 2024 14:13
@smeng9
Copy link
Contributor Author

smeng9 commented Aug 14, 2024 via email

@smeng9
Copy link
Contributor Author

smeng9 commented Oct 13, 2024

Rebased to fix conflicts with v6 environment api.

the @import is also used in scss, removing that line will cause scss to fail.

Ideally Vite should always be able to resolve it yes.
Shall we consider using less to resolve this file instead of using vite to resolve this file?

@smeng9 smeng9 changed the title fix(less): always use root file as base when resolving id fix(less): always resolve to absolute path on fs when rebasing urls Oct 21, 2024
@smeng9 smeng9 requested a review from bluwy October 21, 2024 22:16
@smeng9
Copy link
Contributor Author

smeng9 commented Oct 21, 2024

Hi @bluwy I have used a different method from the one you suggested to solve the problem.

The relative path is not very robust, we can just choose to use the absolute path as the id for a file.

If we use absolute path, less is able resolve the file instead of falling back to vite to resolve the file

@bluwy
Copy link
Member

bluwy commented Oct 30, 2024

Sorry for not following up on this. It required a lot of context to pick back up. I've dig into the bug again and have a better idea of the issue now.

  1. We have a generic url() rebase logic, however it shouldn't rebase on @import url(...) because that gets compiled by the preprocessor straight away instead of being retained in the final single compiled file.
  2. We have a @import "*.css" rebase logic to fix less/scss, because they don't compile those imports that end with .css files, so they're retained and needs rebasing. However, the regex wasn't capturing @import url(...).

The fix I made is to ensure url() rebase does not handle @import url(...). And ensure @import url(*.css) rebase gets handled. That fixed the test locally for me.

I think we should still be rebasing to relative paths to avoid unintended side-effects. Also, I think no2 handling is kinda fragile and should've been forced to be compiled by the preprocessor in the first place. The @import will get re-ordered otherwise for example. I don't know if there's a way to enable that in the preprocessors easily, but something to tackle in the future.

@bluwy bluwy added feat: css p3-minor-bug An edge case that only affects very specific usage (priority) labels Oct 30, 2024
@bluwy bluwy changed the title fix(less): always resolve to absolute path on fs when rebasing urls fix(less): prevent rebasing @import url(...) Oct 30, 2024
@patak-dev patak-dev merged commit aec5fdd into vitejs:main Oct 30, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: css p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

less use @import url() When this style file also imported by other files, an error will be reported
3 participants