From ebf39a04329ddc6ba765e006a5d463680a952270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0?= Date: Tue, 19 May 2026 13:54:40 +0900 Subject: [PATCH] test(css): sass does not use main field (#22449) --- .../src/node/__tests__/plugins/css.spec.ts | 32 +++++++++++++++++++ .../sass-package-resolution/.gitignore | 1 + .../sass-pkg-with-index/index.scss | 3 ++ .../sass-pkg-with-index/main.scss | 3 ++ .../sass-pkg-with-index/package.json | 6 ++++ packages/vite/src/node/plugins/css.ts | 6 ++-- 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/.gitignore create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/index.scss create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/main.scss create mode 100644 packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/package.json diff --git a/packages/vite/src/node/__tests__/plugins/css.spec.ts b/packages/vite/src/node/__tests__/plugins/css.spec.ts index 9f06607b0648f1..d925ee96a45dc8 100644 --- a/packages/vite/src/node/__tests__/plugins/css.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/css.spec.ts @@ -6,6 +6,7 @@ import { resolveConfig } from '../../config' import type { InlineConfig } from '../../config' import { convertTargets, + createCSSResolvers, cssPlugin, cssUrlRE, getEmptyChunkReplacer, @@ -15,6 +16,7 @@ import { resolveLibCssFilename, } from '../../plugins/css' import { PartialEnvironment } from '../../baseEnvironment' +import { normalizePath } from '../../utils' const dirname = import.meta.dirname @@ -421,6 +423,36 @@ describe('preprocessCSS', () => { }) }) +// Sass does not consult the `main` field; see +// https://sass-lang.com/documentation/js-api/classes/nodepackageimporter/ +describe('sass package resolution', () => { + const fixtureRoot = path.resolve(dirname, 'fixtures/sass-package-resolution') + const importer = path.resolve(fixtureRoot, 'entry.scss') + + async function getSassResolver() { + const config = await resolveConfig( + { configFile: false, root: fixtureRoot }, + 'serve', + ) + const environment = new PartialEnvironment('client', config) + const resolvers = createCSSResolvers(config) + return (id: string) => resolvers.sass(environment, id, importer) + } + + test('resolves to index.scss at package root, ignoring main field', async () => { + const resolve = await getSassResolver() + const resolved = await resolve('sass-pkg-with-index') + expect(resolved).toBe( + normalizePath( + path.resolve( + fixtureRoot, + 'node_modules/sass-pkg-with-index/index.scss', + ), + ), + ) + }) +}) + describe('resolveLibCssFilename', () => { test('use name from package.json', () => { const filename = resolveLibCssFilename( diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/.gitignore b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/.gitignore new file mode 100644 index 00000000000000..2b9b8877da603f --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/.gitignore @@ -0,0 +1 @@ +!/node_modules/ diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/index.scss b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/index.scss new file mode 100644 index 00000000000000..06f82754b691b0 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/index.scss @@ -0,0 +1,3 @@ +.from-index { + color: navy; +} diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/main.scss b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/main.scss new file mode 100644 index 00000000000000..c944e6403a3c8a --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/main.scss @@ -0,0 +1,3 @@ +.from-main { + color: red; +} diff --git a/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/package.json b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/package.json new file mode 100644 index 00000000000000..a44811ac1b9c6f --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/fixtures/sass-package-resolution/node_modules/sass-pkg-with-index/package.json @@ -0,0 +1,6 @@ +{ + "name": "sass-pkg-with-index", + "private": true, + "version": "1.0.0", + "main": "main.scss" +} diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 7a271f93e7f90a..cf6152c9eb8ae2 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -1283,13 +1283,15 @@ export function getEmptyChunkReplacer( const fileURLWithWindowsDriveRE = /^file:\/\/\/[a-zA-Z]:\// -interface CSSAtImportResolvers { +export interface CSSAtImportResolvers { css: ResolveIdFn sass: ResolveIdFn less: ResolveIdFn } -function createCSSResolvers(config: ResolvedConfig): CSSAtImportResolvers { +export function createCSSResolvers( + config: ResolvedConfig, +): CSSAtImportResolvers { let cssResolve: ResolveIdFn | undefined let sassResolve: ResolveIdFn | undefined let lessResolve: ResolveIdFn | undefined