feat(css): support resolving stylesheets from exports map (#7817)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
This commit is contained in:
Kyle Herock 2023-03-07 06:49:14 -06:00 committed by GitHub
parent ce18eba39f
commit 108aadf728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 45 additions and 0 deletions

View File

@ -731,6 +731,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
(cssResolve = config.createResolver({
extensions: ['.css'],
mainFields: ['style'],
conditions: ['style'],
tryIndex: false,
preferRelative: true,
}))
@ -743,6 +744,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
(sassResolve = config.createResolver({
extensions: ['.scss', '.sass', '.css'],
mainFields: ['sass', 'style'],
conditions: ['sass', 'style'],
tryIndex: true,
tryPrefix: '_',
preferRelative: true,
@ -756,6 +758,7 @@ function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers {
(lessResolve = config.createResolver({
extensions: ['.less', '.css'],
mainFields: ['less', 'style'],
conditions: ['less', 'style'],
tryIndex: false,
preferRelative: true,
}))

View File

@ -289,6 +289,14 @@ test('@import dependency w/ sass entry', async () => {
expect(await getColor('.css-dep-sass')).toBe('orange')
})
test('@import dependency w/ style export mapping', async () => {
expect(await getColor('.css-dep-exports')).toBe('purple')
})
test('@import dependency w/ sass export mapping', async () => {
expect(await getColor('.css-dep-exports-sass')).toBe('orange')
})
test('@import dependency w/out package scss', async () => {
expect(await getColor('.sass-dep')).toBe('lavender')
})

View File

@ -0,0 +1 @@
throw new Error('should not be imported')

View File

@ -0,0 +1,12 @@
{
"name": "@vitejs/test-css-dep-exports",
"private": true,
"version": "1.0.0",
"exports": {
".": {
"sass": "./style.scss",
"style": "./style.css",
"import": "./index.js"
}
}
}

View File

@ -0,0 +1,3 @@
.css-dep-exports {
color: purple;
}

View File

@ -0,0 +1,3 @@
.css-dep-exports-sass {
color: orange;
}

View File

@ -1 +1,2 @@
@import '@vitejs/test-css-dep';
@import '@vitejs/test-css-dep-exports';

View File

@ -133,6 +133,13 @@
@import dependency w/ sass entrypoints: this should be orange
</p>
<p class="css-dep-exports">
@import dependency w/ style export mapping: this should be purple
</p>
<p class="css-dep-exports-sass">
@import dependency w/ sass export mapping: this should be orange
</p>
<p class="dir-dep">PostCSS dir-dependency: this should be grey</p>
<p class="dir-dep-2">
PostCSS dir-dependency (file 2): this should be grey too

View File

@ -13,6 +13,7 @@
},
"devDependencies": {
"@vitejs/test-css-dep": "link:./css-dep",
"@vitejs/test-css-dep-exports": "link:./css-dep-exports",
"@vitejs/test-css-js-dep": "file:./css-js-dep",
"fast-glob": "^3.2.12",
"less": "^4.1.3",

View File

@ -1,6 +1,7 @@
@import '=/nested'; // alias + custom index resolving -> /nested/_index.scss
@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 'virtual-dep'; // virtual file added through importer
@import '=/pkg-dep'; // package w/out sass field
@import '=/weapp.wxss'; // wxss file

View File

@ -370,6 +370,7 @@ importers:
playground/css:
specifiers:
'@vitejs/test-css-dep': link:./css-dep
'@vitejs/test-css-dep-exports': link:./css-dep-exports
'@vitejs/test-css-js-dep': file:./css-js-dep
fast-glob: ^3.2.12
less: ^4.1.3
@ -379,6 +380,7 @@ importers:
sugarss: ^4.0.1
devDependencies:
'@vitejs/test-css-dep': link:css-dep
'@vitejs/test-css-dep-exports': link:css-dep-exports
'@vitejs/test-css-js-dep': file:playground/css/css-js-dep
fast-glob: 3.2.12
less: 4.1.3
@ -413,6 +415,9 @@ importers:
playground/css/css-dep:
specifiers: {}
playground/css/css-dep-exports:
specifiers: {}
playground/css/css-js-dep:
specifiers: {}