Skip to content

Commit

Permalink
feat!: use modern Sass JS API by default
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 25, 2024
1 parent 57eee6b commit 10be1ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ Type:
type api = "legacy" | "modern" | "modern-compiler";
```

Default: `"legacy"`
Default: `"modern"`

Allows you to switch between the `legacy` and `modern` APIs. 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).

Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ async function loader(content) {

const useSourceMap =
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
const apiType = typeof options.api === "undefined" ? "modern" : options.api;
const sassOptions = await getSassOptions(
this,
options,
content,
implementation,
useSourceMap,
apiType,
);

const shouldUseWebpackImporter =
typeof options.webpackImporter === "boolean"
? options.webpackImporter
: true;

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

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

try {
compile = getCompileFn(this, implementation, options);
compile = getCompileFn(this, implementation, apiType);
} catch (error) {
callback(error);
return;
Expand Down
14 changes: 8 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function proxyCustomImporters(importers, loaderContext) {
* @param {string} content
* @param {object} implementation
* @param {boolean} useSourceMap
* @param {"legacy" | "modern" | "modern-compiler"} apiType
* @returns {Object}
*/
async function getSassOptions(
Expand All @@ -102,6 +103,7 @@ async function getSassOptions(
content,
implementation,
useSourceMap,
apiType,
) {
const options = loaderOptions.sassOptions
? typeof loaderOptions.sassOptions === "function"
Expand Down Expand Up @@ -175,7 +177,7 @@ async function getSassOptions(
}

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

if (isModernAPI) {
Expand Down Expand Up @@ -735,24 +737,24 @@ const sassModernCompilers = new WeakMap();
*
* @param {Object} loaderContext
* @param {Object} implementation
* @param {Object} options
* @param {"legacy" | "modern" | "modern-compiler"} apiType
* @returns {Function}
*/
function getCompileFn(loaderContext, implementation, options) {
function getCompileFn(loaderContext, implementation, apiType) {
const isNewSass =
implementation.info.includes("dart-sass") ||
implementation.info.includes("sass-embedded");

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

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

if (options.api === "modern-compiler") {
if (apiType === "modern-compiler") {
return async (sassOptions) => {
// eslint-disable-next-line no-underscore-dangle
const webpackCompiler = loaderContext._compiler;
Expand Down Expand Up @@ -799,7 +801,7 @@ function getCompileFn(loaderContext, implementation, options) {
});
}

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

Expand Down
14 changes: 12 additions & 2 deletions test/implementation-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,17 @@ describe("implementation option", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

expect(sassEmbeddedSpy).toHaveBeenCalledTimes(1);
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(1);
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
expect(dartSassSpy).toHaveBeenCalledTimes(0);
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);

sassEmbeddedSpy.mockClear();
sassEmbeddedSpyModernAPI.mockClear();
nodeSassSpy.mockClear();
dartSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();

await close(compiler);
});
Expand All @@ -216,8 +220,10 @@ describe("implementation option", () => {
expect(dartSassSpy).toHaveBeenCalledTimes(0);

sassEmbeddedSpy.mockClear();
sassEmbeddedSpyModernAPI.mockClear();
nodeSassSpy.mockClear();
dartSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();

await close(compiler);
});
Expand All @@ -241,8 +247,10 @@ describe("implementation option", () => {
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);

sassEmbeddedSpy.mockClear();
sassEmbeddedSpyModernAPI.mockClear();
nodeSassSpy.mockClear();
dartSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();

await close(compiler);
Expand All @@ -269,9 +277,11 @@ describe("implementation option", () => {
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
expect(dartSassCompilerSpies.compileStringSpy).toHaveBeenCalledTimes(0);

sassEmbeddedSpy.mockClear();
sassEmbeddedSpyModernAPI.mockClear();
nodeSassSpy.mockClear();
dartSassSpy.mockClear();
dartSassSpyModernAPI.mockClear();
dartSassCompilerSpies.mockClear();

await close(compiler);
});
Expand Down

0 comments on commit 10be1ba

Please sign in to comment.