mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(css): ensure sass compiler initialized only once (#18128)
This commit is contained in:
parent
a34a73a3ad
commit
4cc53224e9
@ -2350,7 +2350,7 @@ const makeModernCompilerScssWorker = (
|
||||
alias: Alias[],
|
||||
_maxWorkers: number | undefined,
|
||||
) => {
|
||||
let compiler: Sass.AsyncCompiler | undefined
|
||||
let compilerPromise: Promise<Sass.AsyncCompiler> | undefined
|
||||
|
||||
const worker: Awaited<ReturnType<typeof makeModernScssWorker>> = {
|
||||
async run(sassPath, data, options) {
|
||||
@ -2358,7 +2358,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)
|
||||
@ -2414,8 +2415,8 @@ const makeModernCompilerScssWorker = (
|
||||
} satisfies ScssWorkerResult
|
||||
},
|
||||
async stop() {
|
||||
compiler?.dispose()
|
||||
compiler = undefined
|
||||
;(await compilerPromise)?.dispose()
|
||||
compilerPromise = undefined
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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}
|
||||
"
|
||||
`)
|
||||
})
|
3
playground/css/sass-modern-compiler-build/entry1.scss
Normal file
3
playground/css/sass-modern-compiler-build/entry1.scss
Normal file
@ -0,0 +1,3 @@
|
||||
.entry1 {
|
||||
color: red;
|
||||
}
|
3
playground/css/sass-modern-compiler-build/entry2.scss
Normal file
3
playground/css/sass-modern-compiler-build/entry2.scss
Normal file
@ -0,0 +1,3 @@
|
||||
.entry2 {
|
||||
color: blue;
|
||||
}
|
27
playground/css/vite.config-sass-modern-compiler-build.js
Normal file
27
playground/css/vite.config-sass-modern-compiler-build.js
Normal file
@ -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',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
Loading…
Reference in New Issue
Block a user