Skip to content

Commit

Permalink
refactor: allow implementation to change
Browse files Browse the repository at this point in the history
  • Loading branch information
renspoesse committed Mar 29, 2024
1 parent 5952f5a commit 2e8a443
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 2e8a443

Please sign in to comment.