Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 10be1ba

Browse files
committedJul 25, 2024·
feat!: use modern Sass JS API by default
1 parent 57eee6b commit 10be1ba

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ Type:
683683
type api = "legacy" | "modern" | "modern-compiler";
684684
```
685685

686-
Default: `"legacy"`
686+
Default: `"modern"`
687687

688688
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).
689689

‎src/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ async function loader(content) {
3434

3535
const useSourceMap =
3636
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
37+
const apiType = typeof options.api === "undefined" ? "modern" : options.api;
3738
const sassOptions = await getSassOptions(
3839
this,
3940
options,
4041
content,
4142
implementation,
4243
useSourceMap,
44+
apiType,
4345
);
46+
4447
const shouldUseWebpackImporter =
4548
typeof options.webpackImporter === "boolean"
4649
? options.webpackImporter
4750
: true;
4851

4952
if (shouldUseWebpackImporter) {
50-
const isModernAPI =
51-
options.api === "modern" || options.api === "modern-compiler";
53+
const isModernAPI = apiType === "modern" || apiType === "modern-compiler";
5254

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

6971
try {
70-
compile = getCompileFn(this, implementation, options);
72+
compile = getCompileFn(this, implementation, apiType);
7173
} catch (error) {
7274
callback(error);
7375
return;

‎src/utils.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function proxyCustomImporters(importers, loaderContext) {
9494
* @param {string} content
9595
* @param {object} implementation
9696
* @param {boolean} useSourceMap
97+
* @param {"legacy" | "modern" | "modern-compiler"} apiType
9798
* @returns {Object}
9899
*/
99100
async function getSassOptions(
@@ -102,6 +103,7 @@ async function getSassOptions(
102103
content,
103104
implementation,
104105
useSourceMap,
106+
apiType,
105107
) {
106108
const options = loaderOptions.sassOptions
107109
? typeof loaderOptions.sassOptions === "function"
@@ -175,7 +177,7 @@ async function getSassOptions(
175177
}
176178

177179
const isModernAPI =
178-
loaderOptions.api === "modern" || loaderOptions.api === "modern-compiler";
180+
apiType === "modern" || apiType === "modern-compiler";
179181
const { resourcePath } = loaderContext;
180182

181183
if (isModernAPI) {
@@ -735,24 +737,24 @@ const sassModernCompilers = new WeakMap();
735737
*
736738
* @param {Object} loaderContext
737739
* @param {Object} implementation
738-
* @param {Object} options
740+
* @param {"legacy" | "modern" | "modern-compiler"} apiType
739741
* @returns {Function}
740742
*/
741-
function getCompileFn(loaderContext, implementation, options) {
743+
function getCompileFn(loaderContext, implementation, apiType) {
742744
const isNewSass =
743745
implementation.info.includes("dart-sass") ||
744746
implementation.info.includes("sass-embedded");
745747

746748
if (isNewSass) {
747-
if (options.api === "modern") {
749+
if (apiType === "modern") {
748750
return (sassOptions) => {
749751
const { data, ...rest } = sassOptions;
750752

751753
return implementation.compileStringAsync(data, rest);
752754
};
753755
}
754756

755-
if (options.api === "modern-compiler") {
757+
if (apiType === "modern-compiler") {
756758
return async (sassOptions) => {
757759
// eslint-disable-next-line no-underscore-dangle
758760
const webpackCompiler = loaderContext._compiler;
@@ -799,7 +801,7 @@ function getCompileFn(loaderContext, implementation, options) {
799801
});
800802
}
801803

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

‎test/implementation-option.test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,17 @@ describe("implementation option", () => {
185185
expect(getWarnings(stats)).toMatchSnapshot("warnings");
186186
expect(getErrors(stats)).toMatchSnapshot("errors");
187187

188-
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(1);
188+
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
189+
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(1);
189190
expect(nodeSassSpy).toHaveBeenCalledTimes(0);
190191
expect(dartSassSpy).toHaveBeenCalledTimes(0);
192+
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
191193

192194
sassEmbeddedSpy.mockClear();
195+
sassEmbeddedSpyModernAPI.mockClear();
193196
nodeSassSpy.mockClear();
194197
dartSassSpy.mockClear();
198+
dartSassSpyModernAPI.mockClear();
195199

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

218222
sassEmbeddedSpy.mockClear();
223+
sassEmbeddedSpyModernAPI.mockClear();
219224
nodeSassSpy.mockClear();
220225
dartSassSpy.mockClear();
226+
dartSassSpyModernAPI.mockClear();
221227

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

250+
sassEmbeddedSpy.mockClear();
244251
sassEmbeddedSpyModernAPI.mockClear();
245252
nodeSassSpy.mockClear();
253+
dartSassSpy.mockClear();
246254
dartSassSpyModernAPI.mockClear();
247255

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

280+
sassEmbeddedSpy.mockClear();
281+
sassEmbeddedSpyModernAPI.mockClear();
272282
nodeSassSpy.mockClear();
283+
dartSassSpy.mockClear();
273284
dartSassSpyModernAPI.mockClear();
274-
dartSassCompilerSpies.mockClear();
275285

276286
await close(compiler);
277287
});

0 commit comments

Comments
 (0)
Please sign in to comment.