Skip to content

Commit

Permalink
fix(css): @import with .css in node_modules importing a different pac…
Browse files Browse the repository at this point in the history
…kage failed to resolve (#15000)

Co-authored-by: Shaoyu Meng <shaoyu>
Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
smeng9 and sapphi-red authored Jan 8, 2024
1 parent 953e697 commit 8ccf722
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,13 @@ const scss: SassStylePreprocessor = async (
importer = cleanScssBugUrl(importer)
resolvers.sass(url, importer).then((resolved) => {
if (resolved) {
rebaseUrls(resolved, options.filename, options.alias, '$')
rebaseUrls(
resolved,
options.filename,
options.alias,
'$',
resolvers.sass,
)
.then((data) => done?.(fixScssBugImportValue(data)))
.catch((data) => done?.(data))
} else {
Expand Down Expand Up @@ -1930,6 +1936,7 @@ async function rebaseUrls(
rootFile: string,
alias: Alias[],
variablePrefix: string,
resolver: ResolveFn,
): Promise<Sass.ImporterReturnType> {
file = path.resolve(file) // ensure os-specific flashes
// in the same dir, no need to rebase
Expand All @@ -1952,7 +1959,7 @@ async function rebaseUrls(
}

let rebased
const rebaseFn = (url: string) => {
const rebaseFn = async (url: string) => {
if (url[0] === '/') return url
// ignore url's starting with variable
if (url.startsWith(variablePrefix)) return url
Expand All @@ -1964,7 +1971,7 @@ async function rebaseUrls(
return url
}
}
const absolute = path.resolve(fileDir, url)
const absolute = (await resolver(url, file)) || path.resolve(fileDir, url)
const relative = path.relative(rootDir, absolute)
return normalizePath(relative)
}
Expand Down Expand Up @@ -2093,6 +2100,7 @@ function createViteLessPlugin(
this.rootFile,
this.alias,
'@',
this.resolvers.less,
)
let contents: string
if (result && 'contents' in result) {
Expand Down
4 changes: 4 additions & 0 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ test('@import dependency that @import another dependency', async () => {
expect(await getColor('.css-proxy-dep')).toBe('purple')
})

test('@import scss dependency that has @import with a css extension pointing to another dependency', async () => {
expect(await getColor('.scss-proxy-dep')).toBe('purple')
})

test('@import dependency w/out package scss', async () => {
expect(await getColor('.sass-dep')).toBe('lavender')
})
Expand Down
4 changes: 4 additions & 0 deletions playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ <h1>CSS</h1>
<p class="css-proxy-dep">
@import dependency that @import another dependency: this should be purple
</p>
<p class="scss-proxy-dep">
@import dependency that has @import with a css extension pointing to another
dependency: this should be purple
</p>

<p class="dir-dep">PostCSS dir-dependency: this should be grey</p>
<p class="dir-dep-2">
Expand Down
1 change: 1 addition & 0 deletions playground/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@vitejs/test-css-dep-exports": "link:./css-dep-exports",
"@vitejs/test-css-js-dep": "file:./css-js-dep",
"@vitejs/test-css-proxy-dep": "file:./css-proxy-dep",
"@vitejs/test-scss-proxy-dep": "file:./scss-proxy-dep",
"fast-glob": "^3.3.2",
"less": "^4.2.0",
"postcss-nested": "^6.0.1",
Expand Down
1 change: 1 addition & 0 deletions playground/css/sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@import '=/nested/partial'; // sass convention: omitting leading _ for partials
@import '@vitejs/test-css-dep'; // package w/ sass entry points
@import '@vitejs/test-css-dep-exports'; // package with a sass export mapping
@import '@vitejs/test-scss-proxy-dep'; // package with a sass proxy import
@import 'virtual-dep'; // virtual file added through importer
@import '=/pkg-dep'; // package w/out sass field
@import '=/weapp.wxss'; // wxss file
Expand Down
3 changes: 3 additions & 0 deletions playground/css/scss-proxy-dep-nested/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.scss-proxy-dep {
color: purple;
}
5 changes: 5 additions & 0 deletions playground/css/scss-proxy-dep-nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitejs/test-scss-proxy-dep-nested",
"private": true,
"version": "1.0.0"
}
1 change: 1 addition & 0 deletions playground/css/scss-proxy-dep/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '@vitejs/test-scss-proxy-dep-nested/index.css';
9 changes: 9 additions & 0 deletions playground/css/scss-proxy-dep/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-scss-proxy-dep",
"private": true,
"version": "1.0.0",
"sass": "index.scss",
"dependencies": {
"@vitejs/test-scss-proxy-dep-nested": "file:../scss-proxy-dep-nested"
}
}
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 8ccf722

Please sign in to comment.