Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): ensure sass compiler initialized only once #18128

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2350,15 +2350,16 @@ 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) {
// need pathToFileURL for windows since import("D:...") fails
// 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)
Expand Down Expand Up @@ -2414,8 +2415,8 @@ const makeModernCompilerScssWorker = (
} satisfies ScssWorkerResult
},
async stop() {
compiler?.dispose()
compiler = undefined
;(await compilerPromise)?.dispose()
compilerPromise = undefined
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions playground/css/sass-modern-compiler-build/entry1.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.entry1 {
color: red;
}
3 changes: 3 additions & 0 deletions playground/css/sass-modern-compiler-build/entry2.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.entry2 {
color: blue;
}
27 changes: 27 additions & 0 deletions playground/css/vite.config-sass-modern-compiler-build.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
})