From 2e8a443654ef215bc0672b433397329356788b47 Mon Sep 17 00:00:00 2001 From: Rens Poesse Date: Fri, 29 Mar 2024 17:25:00 -0500 Subject: [PATCH] refactor: allow `implementation` to change --- src/utils.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/utils.js b/src/utils.js index 6964365c..29aadae5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -651,7 +651,7 @@ function getWebpackImporter(loaderContext, implementation, includePaths) { } let nodeSassJobQueue = null; -let sassEmbeddedCompiler = null; +const sassModernCompilers = {}; /** * Verifies that the implementation and version of Sass is supported by this loader. @@ -662,11 +662,10 @@ let sassEmbeddedCompiler = null; * @returns {Function} */ function getCompileFn(loaderContext, implementation, options) { - const isNewSass = - implementation.info.includes("dart-sass") || - implementation.info.includes("sass-embedded"); + const isDartSass = implementation.info.includes("dart-sass"); + const isSassEmbedded = implementation.info.includes("sass-embedded"); - if (isNewSass) { + if (isDartSass || isSassEmbedded) { if (options.api === "modern") { return (sassOptions) => { const { data, ...rest } = sassOptions; @@ -684,15 +683,17 @@ function getCompileFn(loaderContext, implementation, options) { // Some people can run the loader in a multi-threading way; // there is no webpack compiler object in such case. if (webpackCompiler) { - if (!sassEmbeddedCompiler) { + const key = isDartSass ? "dart-sass" : "sass-embedded"; + if (!sassModernCompilers[key]) { // Create a long-running compiler process that can be reused // for compiling individual files. - sassEmbeddedCompiler = await implementation.initAsyncCompiler(); + const compiler = await implementation.initAsyncCompiler(); webpackCompiler.hooks.shutdown.tap("sass-loader", () => { - sassEmbeddedCompiler.dispose(); + compiler.dispose(); }); + sassModernCompilers[key] = compiler; } - return sassEmbeddedCompiler.compileStringAsync(data, rest); + return sassModernCompilers[key].compileStringAsync(data, rest); } return implementation.compileStringAsync(data, rest);