Skip to content

Commit

Permalink
refactor: code (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 5, 2021
1 parent f8c8055 commit 1d8c318
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 91 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -57,8 +57,7 @@
},
"dependencies": {
"klona": "^2.0.4",
"neo-async": "^2.6.2",
"semver": "^7.3.4"
"neo-async": "^2.6.2"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
Expand Down Expand Up @@ -91,6 +90,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"sass": "^1.32.0",
"semver": "^7.3.4",
"standard-version": "^9.1.0",
"style-loader": "^2.0.0",
"webpack": "^5.20.0"
Expand Down
23 changes: 5 additions & 18 deletions src/utils.js
@@ -1,7 +1,6 @@
import url from "url";
import path from "path";

import semver from "semver";
import { klona } from "klona/full";
import async from "neo-async";

Expand Down Expand Up @@ -59,26 +58,12 @@ function getSassImplementation(loaderContext, implementation) {
return;
}

const [implementationName, version] = infoParts;
const [implementationName] = infoParts;

if (implementationName === "dart-sass") {
if (!semver.satisfies(version, "^1.3.0")) {
loaderContext.emitError(
new Error(`Dart Sass version ${version} is incompatible with ^1.3.0.`)
);
}

// eslint-disable-next-line consistent-return
return resolvedImplementation;
} else if (implementationName === "node-sass") {
if (!semver.satisfies(version, "^4.0.0 || ^5.0.0")) {
loaderContext.emitError(
new Error(
`Node Sass version ${version} is incompatible with ^4.0.0 || ^5.0.0.`
)
);
}

// eslint-disable-next-line consistent-return
return resolvedImplementation;
}
Expand Down Expand Up @@ -230,7 +215,7 @@ async function getSassOptions(
// - ~@org/package
// - ~@org/package/
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
const moduleRequestRegex = /^[^?]*~/;
const MODULE_REQUEST_REGEX = /^[^?]*~/;

/**
* When `sass`/`node-sass` tries to resolve an import, it uses a special algorithm.
Expand All @@ -255,7 +240,9 @@ function getPossibleRequests(

// In case there is module request, send this to webpack resolver
if (forWebpackResolver) {
request = request.replace(moduleRequestRegex, "");
if (MODULE_REQUEST_REGEX.test(url)) {
request = request.replace(MODULE_REQUEST_REGEX, "");
}

if (isModuleImport.test(url)) {
request = request[request.length - 1] === "/" ? request : `${request}/`;
Expand Down
27 changes: 0 additions & 27 deletions test/__snapshots__/implementation-option.test.js.snap
Expand Up @@ -30,15 +30,6 @@ Unknown Sass implementation \\"strange-sass\\".",

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

exports[`implementation option should throw an error when the "info" is unparseable #2: errors 1`] = `
Array [
"ModuleError: Module Error (from ../src/cjs.js):
Node Sass version 1 is incompatible with ^4.0.0 || ^5.0.0.",
]
`;

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

exports[`implementation option should throw an error when the "info" is unparseable: errors 1`] = `
Array [
"ModuleError: Module Error (from ../src/cjs.js):
Expand All @@ -48,24 +39,6 @@ Unknown Sass implementation \\"asdfj\\".",

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

exports[`implementation option should throw an error when the "node-sass" package is an incompatible version: errors 1`] = `
Array [
"ModuleError: Module Error (from ../src/cjs.js):
Node Sass version 3.0.0 is incompatible with ^4.0.0 || ^5.0.0.",
]
`;

exports[`implementation option should throw an error when the "node-sass" package is an incompatible version: warnings 1`] = `Array []`;

exports[`implementation option should throw an error when the "sass" package is an incompatible version: errors 1`] = `
Array [
"ModuleError: Module Error (from ../src/cjs.js):
Dart Sass version 1.2.0 is incompatible with ^1.3.0.",
]
`;

exports[`implementation option should throw an error when the "sass" package is an incompatible version: warnings 1`] = `Array []`;

exports[`implementation option should throw error when the "info" does not exist: errors 1`] = `
Array [
"ModuleError: Module Error (from ../src/cjs.js):
Expand Down
43 changes: 0 additions & 43 deletions test/implementation-option.test.js
Expand Up @@ -74,36 +74,6 @@ describe("implementation option", () => {
expect(dartSassSpy).toHaveBeenCalledTimes(1);
});

it('should throw an error when the "node-sass" package is an incompatible version', async () => {
const testId = getTestId("language", "scss");
const options = {
implementation: Object.assign({}, nodeSass, {
info: "node-sass\t3.0.0",
}),
};

const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);

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

it('should throw an error when the "sass" package is an incompatible version', async () => {
const testId = getTestId("language", "scss");
const options = {
implementation: Object.assign({}, dartSass, {
info: "dart-sass\t1.2.0",
}),
};

const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);

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

it("should throw an error on an unknown sass implementation", async () => {
const testId = getTestId("language", "scss");
const options = {
Expand Down Expand Up @@ -132,19 +102,6 @@ describe("implementation option", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should throw an error when the "info" is unparseable #2', async () => {
const testId = getTestId("language", "scss");
const options = {
implementation: Object.assign({}, nodeSass, { info: "node-sass\t1" }),
};

const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);

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

it('should throw error when the "info" does not exist', async () => {
const testId = getTestId("language", "scss");
const options = {
Expand Down

0 comments on commit 1d8c318

Please sign in to comment.