Skip to content

Commit

Permalink
fix: use legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jul 25, 2024
1 parent 10be1ba commit 7c21581
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 107 deletions.
4 changes: 2 additions & 2 deletions 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: `"modern"`
Default: `"modern"` for `sass` (`dart-sass`) and `sass-embedded` or `"legacy"` for `node-sass`

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 All @@ -709,7 +709,7 @@ module.exports = {
{
loader: "sass-loader",
options: {
api: "modern",
api: "modern-compiler",
sassOptions: {
// Your sass options
},
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ async function loader(content) {

const useSourceMap =
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
const apiType = typeof options.api === "undefined" ? "modern" : options.api;
// Use `legacy` for `node-sass` and `modern` for `dart-sass` and `sass-embedded`
const apiType = !implementation.compileStringAsync
? "legacy"
: typeof options.api === "undefined"
? "modern"
: options.api;
const sassOptions = await getSassOptions(
this,
options,
Expand Down
106 changes: 2 additions & 104 deletions test/__snapshots__/implementation-option.test.js.no-node-sass.snap
Original file line number Diff line number Diff line change
@@ -1,107 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`implementation option 'dart-sass', 'legacy' API: warnings 1`] = `[]`;

exports[`implementation option 'dart-sass', 'modern' API: errors 1`] = `[]`;

exports[`implementation option 'dart-sass', 'modern' API: warnings 1`] = `[]`;

exports[`implementation option 'dart-sass', 'modern-compiler' API: errors 1`] = `[]`;

exports[`implementation option 'dart-sass', 'modern-compiler' API: warnings 1`] = `[]`;

exports[`implementation option 'sass_string', 'legacy' API: errors 1`] = `[]`;

exports[`implementation option 'sass_string', 'legacy' API: warnings 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'legacy' API: errors 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'legacy' API: warnings 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'modern' API: errors 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'modern' API: warnings 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'modern-compiler' API: errors 1`] = `[]`;

exports[`implementation option 'sass-embedded', 'modern-compiler' API: warnings 1`] = `[]`;

exports[`implementation option not specify with legacy API: errors 1`] = `[]`;

exports[`implementation option not specify with legacy API: warnings 1`] = `[]`;

exports[`implementation option not specify with modern API: errors 1`] = `[]`;

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

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

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

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

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

exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Some error sass-embedded",
]
`;

exports[`implementation option should not swallow an error when trying to load a sass implementation: warnings 1`] = `[]`;

exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 1`] = `[]`;

exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: errors 2`] = `[]`;

exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 1`] = `[]`;

exports[`implementation option should support switching the implementation within the same process when using the modern-compiler API: warnings 2`] = `[]`;

exports[`implementation option should throw an error on an unknown sass implementation: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation "strange-sass".",
]
`;

exports[`implementation option should throw an error on an unknown sass implementation: warnings 1`] = `[]`;

exports[`implementation option should throw an error when the "info" is unparseable: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation "asdfj".",
]
`;

exports[`implementation option should throw an error when the "info" is unparseable: warnings 1`] = `[]`;

exports[`implementation option should throw error when the "info" does not exist: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Unknown Sass implementation.",
]
`;

exports[`implementation option should throw error when the "info" does not exist: warnings 1`] = `[]`;

exports[`implementation option should throw error when unresolved package: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Error: Cannot find module 'unresolved' from 'src/utils.js'",
]
`;

exports[`implementation option should throw error when unresolved package: warnings 1`] = `[]`;

exports[`implementation option should try to load using valid order: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Some error sass-embedded",
]
`;

exports[`implementation option should try to load using valid order: warnings 1`] = `[]`;
exports[`implementation option not specify with node-sass: warnings 1`] = `[]`;
4 changes: 4 additions & 0 deletions test/__snapshots__/implementation-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ exports[`implementation option not specify with modern-compiler API: errors 1`]

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

exports[`implementation option not specify with node-sass: errors 1`] = `[]`;

exports[`implementation option not specify with node-sass: warnings 1`] = `[]`;

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

exports[`implementation option not specify: warnings 1`] = `[]`;
Expand Down
30 changes: 30 additions & 0 deletions test/implementation-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,36 @@ describe("implementation option", () => {
await close(compiler);
});

it("not specify with node-sass", async () => {
const testId = getTestId("language", "scss");
const options = {
implementation: nodeSass
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const { css, sourceMap } = getCodeFromBundle(stats, compiler);

expect(css).toBeDefined();
expect(sourceMap).toBeUndefined();

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");

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

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

await close(compiler);
});

it("not specify with legacy API", async () => {
const testId = getTestId("language", "scss");
const options = {
Expand Down

0 comments on commit 7c21581

Please sign in to comment.