diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 38ca4cac277538..82ac2cc5b9cfde 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -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, })) diff --git a/playground/css/__tests__/css.spec.ts b/playground/css/__tests__/css.spec.ts index c3ae17f0ed9237..749bcd595af0d6 100644 --- a/playground/css/__tests__/css.spec.ts +++ b/playground/css/__tests__/css.spec.ts @@ -285,6 +285,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') }) diff --git a/playground/css/css-dep-exports/index.js b/playground/css/css-dep-exports/index.js new file mode 100644 index 00000000000000..47b55353d03edb --- /dev/null +++ b/playground/css/css-dep-exports/index.js @@ -0,0 +1 @@ +throw new Error('should not be imported') diff --git a/playground/css/css-dep-exports/package.json b/playground/css/css-dep-exports/package.json new file mode 100644 index 00000000000000..70cb0e17aad988 --- /dev/null +++ b/playground/css/css-dep-exports/package.json @@ -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" + } + } +} diff --git a/playground/css/css-dep-exports/style.css b/playground/css/css-dep-exports/style.css new file mode 100644 index 00000000000000..838a8afbe4d435 --- /dev/null +++ b/playground/css/css-dep-exports/style.css @@ -0,0 +1,3 @@ +.css-dep-exports { + color: purple; +} diff --git a/playground/css/css-dep-exports/style.scss b/playground/css/css-dep-exports/style.scss new file mode 100644 index 00000000000000..37df38d7d49d24 --- /dev/null +++ b/playground/css/css-dep-exports/style.scss @@ -0,0 +1,3 @@ +.css-dep-exports-sass { + color: orange; +} diff --git a/playground/css/dep.css b/playground/css/dep.css index ad3e1bcd12480c..3578a9d5312363 100644 --- a/playground/css/dep.css +++ b/playground/css/dep.css @@ -1 +1,2 @@ @import '@vitejs/test-css-dep'; +@import '@vitejs/test-css-dep-exports'; diff --git a/playground/css/index.html b/playground/css/index.html index f520d6514b0df7..69e9ce677a1c40 100644 --- a/playground/css/index.html +++ b/playground/css/index.html @@ -125,6 +125,13 @@
+ @import dependency w/ style export mapping: this should be purple +
++ @import dependency w/ sass export mapping: this should be orange +
+PostCSS dir-dependency: this should be grey
PostCSS dir-dependency (file 2): this should be grey too diff --git a/playground/css/package.json b/playground/css/package.json index 04b3e25b20d0e5..22ccc686d75ef8 100644 --- a/playground/css/package.json +++ b/playground/css/package.json @@ -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", diff --git a/playground/css/sass.scss b/playground/css/sass.scss index 796d9ba68b0c05..4105e1aefa9c55 100644 --- a/playground/css/sass.scss +++ b/playground/css/sass.scss @@ -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 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43dfb7698713e6..c8ab2492a2c56b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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: {}