From 0e723bf89f19acb1f22e868d20540bdfa3b1bf8c Mon Sep 17 00:00:00 2001 From: glromeo Date: Tue, 30 Jan 2024 02:47:11 +0000 Subject: [PATCH] fixed compiler hanging at the end of tests --- src/plugin.ts | 11 ++++++++--- src/render.ts | 9 ++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 65be781..a267798 100755 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -4,6 +4,7 @@ import {SassPluginOptions} from './index' import {getContext, makeModule, modulesPaths, parseNonce, posixRelative} from './utils' import {useCache} from './cache' import {createRenderer} from './render' +import {initAsyncCompiler} from 'sass-embedded' const DEFAULT_FILTER = /\.(s[ac]ss|css)$/ @@ -31,7 +32,7 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin { return { name: 'sass-plugin', - async setup({initialOptions, onResolve, onLoad, resolve, onStart}) { + async setup({initialOptions, onResolve, onLoad, resolve, onStart, onDispose}) { options.loadPaths = Array.from(new Set([ ...options.loadPaths || modulesPaths(initialOptions.absWorkingDir), @@ -72,11 +73,15 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin { })) } - const renderSync = await createRenderer(options, options.sourceMap ?? sourcemap) + const compiler = await initAsyncCompiler(); + + onDispose(()=> compiler.dispose()) + + const renderAsync = createRenderer(compiler, options, options.sourceMap ?? sourcemap) onLoad({filter: options.filter ?? DEFAULT_FILTER}, useCache(options, fsStatCache, async path => { try { - let {cssText, watchFiles, warnings} = await renderSync(path) + let {cssText, watchFiles, warnings} = await renderAsync(path) if (!warnings) { warnings = [] } diff --git a/src/render.ts b/src/render.ts index c5760b3..0465537 100644 --- a/src/render.ts +++ b/src/render.ts @@ -6,8 +6,9 @@ import * as sass from 'sass-embedded' import {ImporterResult, initAsyncCompiler} from 'sass-embedded' import {fileURLToPath, pathToFileURL} from 'url' import {SassPluginOptions} from './index' +import {AsyncCompiler} from 'sass-embedded/dist/types/compile' -export type RenderSync = (path: string) => Promise +export type RenderAsync = (path: string) => Promise export type RenderResult = { cssText: string @@ -15,7 +16,7 @@ export type RenderResult = { warnings?: PartialMessage[] } -export async function createRenderer(options: SassPluginOptions = {}, sourcemap: boolean): Promise { +export function createRenderer(compiler: AsyncCompiler, options: SassPluginOptions = {}, sourcemap: boolean): RenderAsync { const loadPaths = options.loadPaths! const resolveModule = createResolver(options, loadPaths) @@ -61,10 +62,8 @@ export async function createRenderer(options: SassPluginOptions = {}, sourcemap: const sepTilde = `${sep}~` - const compiler = await initAsyncCompiler(); - /** - * renderSync + * renderAsync */ return async function (path: string): Promise {