Skip to content

Commit 7c21581

Browse files
fix: use legacy
1 parent 10be1ba commit 7c21581

File tree

5 files changed

+44
-107
lines changed

5 files changed

+44
-107
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ Type:
683683
type api = "legacy" | "modern" | "modern-compiler";
684684
```
685685

686-
Default: `"modern"`
686+
Default: `"modern"` for `sass` (`dart-sass`) and `sass-embedded` or `"legacy"` for `node-sass`
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

@@ -709,7 +709,7 @@ module.exports = {
709709
{
710710
loader: "sass-loader",
711711
options: {
712-
api: "modern",
712+
api: "modern-compiler",
713713
sassOptions: {
714714
// Your sass options
715715
},

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ 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;
37+
// Use `legacy` for `node-sass` and `modern` for `dart-sass` and `sass-embedded`
38+
const apiType = !implementation.compileStringAsync
39+
? "legacy"
40+
: typeof options.api === "undefined"
41+
? "modern"
42+
: options.api;
3843
const sassOptions = await getSassOptions(
3944
this,
4045
options,
Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`implementation option 'dart-sass', 'legacy' API: errors 1`] = `[]`;
3+
exports[`implementation option not specify with node-sass: errors 1`] = `[]`;
44

5-
exports[`implementation option 'dart-sass', 'legacy' API: warnings 1`] = `[]`;
6-
7-
exports[`implementation option 'dart-sass', 'modern' API: errors 1`] = `[]`;
8-
9-
exports[`implementation option 'dart-sass', 'modern' API: warnings 1`] = `[]`;
10-
11-
exports[`implementation option 'dart-sass', 'modern-compiler' API: errors 1`] = `[]`;
12-
13-
exports[`implementation option 'dart-sass', 'modern-compiler' API: warnings 1`] = `[]`;
14-
15-
exports[`implementation option 'sass_string', 'legacy' API: errors 1`] = `[]`;
16-
17-
exports[`implementation option 'sass_string', 'legacy' API: warnings 1`] = `[]`;
18-
19-
exports[`implementation option 'sass-embedded', 'legacy' API: errors 1`] = `[]`;
20-
21-
exports[`implementation option 'sass-embedded', 'legacy' API: warnings 1`] = `[]`;
22-
23-
exports[`implementation option 'sass-embedded', 'modern' API: errors 1`] = `[]`;
24-
25-
exports[`implementation option 'sass-embedded', 'modern' API: warnings 1`] = `[]`;
26-
27-
exports[`implementation option 'sass-embedded', 'modern-compiler' API: errors 1`] = `[]`;
28-
29-
exports[`implementation option 'sass-embedded', 'modern-compiler' API: warnings 1`] = `[]`;
30-
31-
exports[`implementation option not specify with legacy API: errors 1`] = `[]`;
32-
33-
exports[`implementation option not specify with legacy API: warnings 1`] = `[]`;
34-
35-
exports[`implementation option not specify with modern API: errors 1`] = `[]`;
36-
37-
exports[`implementation option not specify with modern API: warnings 1`] = `[]`;
38-
39-
exports[`implementation option not specify with modern-compiler API: errors 1`] = `[]`;
40-
41-
exports[`implementation option not specify with modern-compiler API: warnings 1`] = `[]`;
42-
43-
exports[`implementation option not specify: errors 1`] = `[]`;
44-
45-
exports[`implementation option not specify: warnings 1`] = `[]`;
46-
47-
exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
48-
[
49-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
50-
Some error sass-embedded",
51-
]
52-
`;
53-
54-
exports[`implementation option should not swallow an error when trying to load a sass implementation: warnings 1`] = `[]`;
55-
56-
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 1`] = `[]`;
57-
58-
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 2`] = `[]`;
59-
60-
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 1`] = `[]`;
61-
62-
exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 2`] = `[]`;
63-
64-
exports[`implementation option should throw an error on an unknown sass implementation: errors 1`] = `
65-
[
66-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
67-
Error: Unknown Sass implementation "strange-sass".",
68-
]
69-
`;
70-
71-
exports[`implementation option should throw an error on an unknown sass implementation: warnings 1`] = `[]`;
72-
73-
exports[`implementation option should throw an error when the "info" is unparseable: errors 1`] = `
74-
[
75-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
76-
Error: Unknown Sass implementation "asdfj".",
77-
]
78-
`;
79-
80-
exports[`implementation option should throw an error when the "info" is unparseable: warnings 1`] = `[]`;
81-
82-
exports[`implementation option should throw error when the "info" does not exist: errors 1`] = `
83-
[
84-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
85-
Error: Unknown Sass implementation.",
86-
]
87-
`;
88-
89-
exports[`implementation option should throw error when the "info" does not exist: warnings 1`] = `[]`;
90-
91-
exports[`implementation option should throw error when unresolved package: errors 1`] = `
92-
[
93-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
94-
Error: Cannot find module 'unresolved' from 'src/utils.js'",
95-
]
96-
`;
97-
98-
exports[`implementation option should throw error when unresolved package: warnings 1`] = `[]`;
99-
100-
exports[`implementation option should try to load using valid order: errors 1`] = `
101-
[
102-
"ModuleBuildError: Module build failed (from ../src/cjs.js):
103-
Some error sass-embedded",
104-
]
105-
`;
106-
107-
exports[`implementation option should try to load using valid order: warnings 1`] = `[]`;
5+
exports[`implementation option not specify with node-sass: warnings 1`] = `[]`;

test/__snapshots__/implementation-option.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ exports[`implementation option not specify with modern-compiler API: errors 1`]
4444

4545
exports[`implementation option not specify with modern-compiler API: warnings 1`] = `[]`;
4646

47+
exports[`implementation option not specify with node-sass: errors 1`] = `[]`;
48+
49+
exports[`implementation option not specify with node-sass: warnings 1`] = `[]`;
50+
4751
exports[`implementation option not specify: errors 1`] = `[]`;
4852

4953
exports[`implementation option not specify: warnings 1`] = `[]`;

test/implementation-option.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ describe("implementation option", () => {
200200
await close(compiler);
201201
});
202202

203+
it("not specify with node-sass", async () => {
204+
const testId = getTestId("language", "scss");
205+
const options = {
206+
implementation: nodeSass
207+
};
208+
const compiler = getCompiler(testId, { loader: { options } });
209+
const stats = await compile(compiler);
210+
const { css, sourceMap } = getCodeFromBundle(stats, compiler);
211+
212+
expect(css).toBeDefined();
213+
expect(sourceMap).toBeUndefined();
214+
215+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
216+
expect(getErrors(stats)).toMatchSnapshot("errors");
217+
218+
expect(sassEmbeddedSpy).toHaveBeenCalledTimes(0);
219+
expect(sassEmbeddedSpyModernAPI).toHaveBeenCalledTimes(0);
220+
expect(nodeSassSpy).toHaveBeenCalledTimes(1);
221+
expect(dartSassSpy).toHaveBeenCalledTimes(0);
222+
expect(dartSassSpyModernAPI).toHaveBeenCalledTimes(0);
223+
224+
sassEmbeddedSpy.mockClear();
225+
sassEmbeddedSpyModernAPI.mockClear();
226+
nodeSassSpy.mockClear();
227+
dartSassSpy.mockClear();
228+
dartSassSpyModernAPI.mockClear();
229+
230+
await close(compiler);
231+
});
232+
203233
it("not specify with legacy API", async () => {
204234
const testId = getTestId("language", "scss");
205235
const options = {

0 commit comments

Comments
 (0)