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(css): @import with .css in node_modules importing a different package failed to resolve #15000

Merged
merged 6 commits into from
Jan 8, 2024
Merged

fix(css): @import with .css in node_modules importing a different package failed to resolve #15000

merged 6 commits into from
Jan 8, 2024

Conversation

smeng9
Copy link
Contributor

@smeng9 smeng9 commented Nov 15, 2023

Description

fixes #12227
refs #9681

Additional context

The cause of the bug is when performing sass style preprocessing, it's trying to rewrite the @import lines in the code using sass render with internalImporter.

The internalImporter resolve the url and then tries to perform a rebase of the url. The rebaseUrls is not correctly performing rebase if we try to perform another import from a different package.

So instead of calling path.resolve, we have to try the corresponding resolver first with id and importer.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@smeng9 smeng9 changed the title fix(css): deep css import resolve fix(css): deep scss/sass import resolve Nov 21, 2023
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

I haven't tried yet, but IIUC this rebaseUrl function only rewrites @import with css extension. The issue is about @import with less extension. Does this work?

Also would you add a test?

@smeng9
Copy link
Contributor Author

smeng9 commented Dec 7, 2023

Hi @sapphi-red,

I have tested this fix by linking vite against the failing case https://stackblitz.com/edit/vite-sass-transitive-import?file=src%2Fstyle.scss in #12227 and now the file can be correctly resolved. The less version should work the same.

I have tried to write a test case for that, but it seems the issue only happens for npm instead of pnpm package manager. I can't make the test case fail without this fix.

@smeng9 smeng9 requested a review from sapphi-red December 9, 2023 00:12
@sapphi-red
Copy link
Member

I see. It seems #9681 and #12227 is caused by a different reason. I mistakenly said #12227 is a duplicate.

I have tried to write a test case for that, but it seems the issue only happens for npm instead of pnpm package manager. I can't make the test case fail without this fix.

I guess it would fail if you use file: instead of link:.

@sapphi-red sapphi-red added feat: css p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 9, 2023
@smeng9
Copy link
Contributor Author

smeng9 commented Dec 17, 2023

Hi @sapphi-red a test cases is added with your method to use file instead of link.

Can we merge this soon? Thanks!

There are some flaky tests on node-20, macos-latest however it passes on my development mac

playground/css/__tests__/css.spec.ts Outdated Show resolved Hide resolved
playground/css/index.html Outdated Show resolved Hide resolved
sapphi-red
sapphi-red previously approved these changes Dec 30, 2023
@sapphi-red sapphi-red changed the title fix(css): deep scss/sass import resolve fix(css): @import with .css in node_modules importing a different package failed to resolve Dec 30, 2023
@sapphi-red sapphi-red requested a review from bluwy January 2, 2024 08:06
@patak-dev patak-dev merged commit 8ccf722 into vitejs:main Jan 8, 2024
10 checks passed
@smeng9 smeng9 deleted the fix-scss-deep-resolve branch January 9, 2024 01:18
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
3 participants