diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index d1a3978a42f3cb..e8527b949d544c 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -2314,7 +2314,7 @@ const makeModernCompilerScssWorker = ( alias: Alias[], _maxWorkers: number | undefined, ) => { - let compiler: Sass.AsyncCompiler | undefined + let compilerPromise: Promise | undefined const worker: Awaited> = { async run(sassPath, data, options) { @@ -2322,7 +2322,8 @@ const makeModernCompilerScssWorker = ( // https://github.com/nodejs/node/issues/31710 const sass: typeof Sass = (await import(pathToFileURL(sassPath).href)) .default - compiler ??= await sass.initAsyncCompiler() + compilerPromise ??= sass.initAsyncCompiler() + const compiler = await compilerPromise const sassOptions = { ...options } as Sass.StringOptions<'async'> sassOptions.url = pathToFileURL(options.filename) @@ -2373,8 +2374,8 @@ const makeModernCompilerScssWorker = ( } satisfies ScssWorkerResult }, async stop() { - compiler?.dispose() - compiler = undefined + ;(await compilerPromise)?.dispose() + compilerPromise = undefined }, } diff --git a/playground/css/__tests__/sass-modern-compiler-build/sass-modern-compiler.spec.ts b/playground/css/__tests__/sass-modern-compiler-build/sass-modern-compiler.spec.ts new file mode 100644 index 00000000000000..98bba744b175d5 --- /dev/null +++ b/playground/css/__tests__/sass-modern-compiler-build/sass-modern-compiler.spec.ts @@ -0,0 +1,15 @@ +import { expect, test } from 'vitest' +import { findAssetFile, isBuild } from '~utils' + +test.runIf(isBuild)('sass modern compiler build multiple entries', () => { + expect(findAssetFile(/entry1/, 'sass-modern-compiler-build')) + .toMatchInlineSnapshot(` + ".entry1{color:red} + " + `) + expect(findAssetFile(/entry2/, 'sass-modern-compiler-build')) + .toMatchInlineSnapshot(` + ".entry2{color:#00f} + " + `) +}) diff --git a/playground/css/sass-modern-compiler-build/entry1.scss b/playground/css/sass-modern-compiler-build/entry1.scss new file mode 100644 index 00000000000000..e21334eb8337bc --- /dev/null +++ b/playground/css/sass-modern-compiler-build/entry1.scss @@ -0,0 +1,3 @@ +.entry1 { + color: red; +} diff --git a/playground/css/sass-modern-compiler-build/entry2.scss b/playground/css/sass-modern-compiler-build/entry2.scss new file mode 100644 index 00000000000000..eca3004c9d247f --- /dev/null +++ b/playground/css/sass-modern-compiler-build/entry2.scss @@ -0,0 +1,3 @@ +.entry2 { + color: blue; +} diff --git a/playground/css/vite.config-sass-modern-compiler-build.js b/playground/css/vite.config-sass-modern-compiler-build.js new file mode 100644 index 00000000000000..b44ef1e354d4d9 --- /dev/null +++ b/playground/css/vite.config-sass-modern-compiler-build.js @@ -0,0 +1,27 @@ +import path from 'node:path' +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + outDir: 'dist/sass-modern-compiler-build', + rollupOptions: { + input: { + entry1: path.join( + import.meta.dirname, + 'sass-modern-compiler-build/entry1.scss', + ), + entry2: path.join( + import.meta.dirname, + 'sass-modern-compiler-build/entry2.scss', + ), + }, + }, + }, + css: { + preprocessorOptions: { + scss: { + api: 'modern-compiler', + }, + }, + }, +})