Skip to content

Commit 4cc5322

Browse files
authoredSep 23, 2024··
fix(css): ensure sass compiler initialized only once (#18128)
1 parent a34a73a commit 4cc5322

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed
 

‎packages/vite/src/node/plugins/css.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -2350,15 +2350,16 @@ const makeModernCompilerScssWorker = (
23502350
alias: Alias[],
23512351
_maxWorkers: number | undefined,
23522352
) => {
2353-
let compiler: Sass.AsyncCompiler | undefined
2353+
let compilerPromise: Promise<Sass.AsyncCompiler> | undefined
23542354

23552355
const worker: Awaited<ReturnType<typeof makeModernScssWorker>> = {
23562356
async run(sassPath, data, options) {
23572357
// need pathToFileURL for windows since import("D:...") fails
23582358
// https://github.com/nodejs/node/issues/31710
23592359
const sass: typeof Sass = (await import(pathToFileURL(sassPath).href))
23602360
.default
2361-
compiler ??= await sass.initAsyncCompiler()
2361+
compilerPromise ??= sass.initAsyncCompiler()
2362+
const compiler = await compilerPromise
23622363

23632364
const sassOptions = { ...options } as Sass.StringOptions<'async'>
23642365
sassOptions.url = pathToFileURL(options.filename)
@@ -2414,8 +2415,8 @@ const makeModernCompilerScssWorker = (
24142415
} satisfies ScssWorkerResult
24152416
},
24162417
async stop() {
2417-
compiler?.dispose()
2418-
compiler = undefined
2418+
;(await compilerPromise)?.dispose()
2419+
compilerPromise = undefined
24192420
},
24202421
}
24212422

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import { findAssetFile, isBuild } from '~utils'
3+
4+
test.runIf(isBuild)('sass modern compiler build multiple entries', () => {
5+
expect(findAssetFile(/entry1/, 'sass-modern-compiler-build'))
6+
.toMatchInlineSnapshot(`
7+
".entry1{color:red}
8+
"
9+
`)
10+
expect(findAssetFile(/entry2/, 'sass-modern-compiler-build'))
11+
.toMatchInlineSnapshot(`
12+
".entry2{color:#00f}
13+
"
14+
`)
15+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.entry1 {
2+
color: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.entry2 {
2+
color: blue;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
build: {
6+
outDir: 'dist/sass-modern-compiler-build',
7+
rollupOptions: {
8+
input: {
9+
entry1: path.join(
10+
import.meta.dirname,
11+
'sass-modern-compiler-build/entry1.scss',
12+
),
13+
entry2: path.join(
14+
import.meta.dirname,
15+
'sass-modern-compiler-build/entry2.scss',
16+
),
17+
},
18+
},
19+
},
20+
css: {
21+
preprocessorOptions: {
22+
scss: {
23+
api: 'modern-compiler',
24+
},
25+
},
26+
},
27+
})

0 commit comments

Comments
 (0)
Please sign in to comment.