Skip to content

Commit

Permalink
perf: reuse compiler process when using sass-embedded
Browse files Browse the repository at this point in the history
This implements the Shared Resources proposal that was accepted and implemented in Dart Sass 1.70.0.
By reusing the same compiler process when compiling multiple files, this significantly improves
performance for tools like webpack.

Closes: #1163
  • Loading branch information
renspoesse committed Mar 28, 2024
1 parent 13f0dc8 commit ecc9206
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function loader(content) {
let result;

try {
result = await compile(sassOptions, options);
result = await compile(sassOptions);
} catch (error) {
// There are situations when the `file`/`span.url` property do not exist
// Modern API
Expand Down
11 changes: 9 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ function getWebpackImporter(loaderContext, implementation, includePaths) {
}

let nodeSassJobQueue = null;
let sassEmbeddedCompiler = null;

/**
* Verifies that the implementation and version of Sass is supported by this loader.
Expand All @@ -665,10 +666,16 @@ function getCompileFn(implementation, options) {

if (isNewSass) {
if (options.api === "modern") {
return (sassOptions) => {
return async (sassOptions) => {
const { data, ...rest } = sassOptions;

return implementation.compileStringAsync(data, rest);
if (!sassEmbeddedCompiler) {
// Create a long-running compiler process that can be reused
// for compiling individual files.
sassEmbeddedCompiler = await implementation.initAsyncCompiler();
}

return sassEmbeddedCompiler.compileStringAsync(data, rest);
};
}

Expand Down

0 comments on commit ecc9206

Please sign in to comment.