Skip to content

Commit 8464d97

Browse files
sapphi-redhi-ogawa
andauthoredSep 24, 2024
fix(css): backport #18128, ensure sass compiler initialized only once (#18184)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent 7d47fc1 commit 8464d97

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
@@ -2314,15 +2314,16 @@ const makeModernCompilerScssWorker = (
23142314
alias: Alias[],
23152315
_maxWorkers: number | undefined,
23162316
) => {
2317-
let compiler: Sass.AsyncCompiler | undefined
2317+
let compilerPromise: Promise<Sass.AsyncCompiler> | undefined
23182318

23192319
const worker: Awaited<ReturnType<typeof makeModernScssWorker>> = {
23202320
async run(sassPath, data, options) {
23212321
// need pathToFileURL for windows since import("D:...") fails
23222322
// https://github.com/nodejs/node/issues/31710
23232323
const sass: typeof Sass = (await import(pathToFileURL(sassPath).href))
23242324
.default
2325-
compiler ??= await sass.initAsyncCompiler()
2325+
compilerPromise ??= sass.initAsyncCompiler()
2326+
const compiler = await compilerPromise
23262327

23272328
const sassOptions = { ...options } as Sass.StringOptions<'async'>
23282329
sassOptions.url = pathToFileURL(options.filename)
@@ -2373,8 +2374,8 @@ const makeModernCompilerScssWorker = (
23732374
} satisfies ScssWorkerResult
23742375
},
23752376
async stop() {
2376-
compiler?.dispose()
2377-
compiler = undefined
2377+
;(await compilerPromise)?.dispose()
2378+
compilerPromise = undefined
23782379
},
23792380
}
23802381

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.