Skip to content

Commit

Permalink
refactor: add modern-compiler API option and call to dispose()
Browse files Browse the repository at this point in the history
  • Loading branch information
renspoesse committed Mar 29, 2024
1 parent ecc9206 commit 5952f5a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,12 @@ module.exports = {
Type:

```ts
type api = "legacy" | "modern";
type api = "legacy" | "modern" | "modern-compiler";
```

Default: `"legacy"`

Allows you to switch between `legacy` and `modern` API. You can find more information [here](https://sass-lang.com/documentation/js-api).
Allows you to switch between `legacy` and `modern` API. You can find more information [here](https://sass-lang.com/documentation/js-api). The `modern-compiler` option enables the modern API with support for [Shared Resources](https://github.com/sass/sass/blob/main/accepted/shared-resources.d.ts.md).

> **Warning**
>
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ async function loader(content) {
: true;

if (shouldUseWebpackImporter) {
const isModernAPI = options.api === "modern";
const isModernAPI =
options.api === "modern" || options.api === "modern-compiler";

if (!isModernAPI) {
const { includePaths } = sassOptions;
Expand All @@ -65,7 +66,7 @@ async function loader(content) {
let compile;

try {
compile = getCompileFn(implementation, options);
compile = getCompileFn(this, implementation, options);
} catch (error) {
callback(error);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"api": {
"description": "Switch between old and modern API for `sass` (`Dart Sass`) and `Sass Embedded` implementations.",
"link": "https://github.com/webpack-contrib/sass-loader#sassoptions",
"enum": ["legacy", "modern"]
"enum": ["legacy", "modern", "modern-compiler"]
},
"sassOptions": {
"description": "Options for `node-sass` or `sass` (`Dart Sass`) implementation.",
Expand Down
36 changes: 28 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ async function getSassOptions(
};
}

const isModernAPI = loaderOptions.api === "modern";
const isModernAPI =
loaderOptions.api === "modern" || loaderOptions.api === "modern-compiler";
const { resourcePath } = loaderContext;

if (isModernAPI) {
Expand Down Expand Up @@ -655,27 +656,46 @@ let sassEmbeddedCompiler = null;
/**
* Verifies that the implementation and version of Sass is supported by this loader.
*
* @param {Object} loaderContext
* @param {Object} implementation
* @param {Object} options
* @returns {Function}
*/
function getCompileFn(implementation, options) {
function getCompileFn(loaderContext, implementation, options) {
const isNewSass =
implementation.info.includes("dart-sass") ||
implementation.info.includes("sass-embedded");

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

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

if (options.api === "modern-compiler") {
return async (sassOptions) => {
// eslint-disable-next-line no-underscore-dangle
const webpackCompiler = loaderContext._compiler;
const { data, ...rest } = sassOptions;

if (!sassEmbeddedCompiler) {
// Create a long-running compiler process that can be reused
// for compiling individual files.
sassEmbeddedCompiler = await implementation.initAsyncCompiler();
// Some people can run the loader in a multi-threading way;
// there is no webpack compiler object in such case.
if (webpackCompiler) {
if (!sassEmbeddedCompiler) {
// Create a long-running compiler process that can be reused
// for compiling individual files.
sassEmbeddedCompiler = await implementation.initAsyncCompiler();
webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
sassEmbeddedCompiler.dispose();
});
}
return sassEmbeddedCompiler.compileStringAsync(data, rest);
}

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

Expand All @@ -693,7 +713,7 @@ function getCompileFn(implementation, options) {
});
}

if (options.api === "modern") {
if (options.api === "modern" || options.api === "modern-compiler") {
throw new Error("Modern API is not supported for 'node-sass'");
}

Expand Down

0 comments on commit 5952f5a

Please sign in to comment.