Skip to content

Commit

Permalink
fixed compiler hanging at the end of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glromeo authored and glromeo committed Jan 30, 2024
1 parent 11fe88f commit 0e723bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)$/

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 = []
}
Expand Down
9 changes: 4 additions & 5 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ 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<RenderResult>
export type RenderAsync = (path: string) => Promise<RenderResult>

export type RenderResult = {
cssText: string
watchFiles: string[]
warnings?: PartialMessage[]
}

export async function createRenderer(options: SassPluginOptions = {}, sourcemap: boolean): Promise<RenderSync> {
export function createRenderer(compiler: AsyncCompiler, options: SassPluginOptions = {}, sourcemap: boolean): RenderAsync {

const loadPaths = options.loadPaths!
const resolveModule = createResolver(options, loadPaths)
Expand Down Expand Up @@ -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<RenderResult> {

Expand Down

0 comments on commit 0e723bf

Please sign in to comment.