Skip to content

Commit 1d8c318

Browse files
refactor: code (#919)
1 parent f8c8055 commit 1d8c318

File tree

5 files changed

+11
-91
lines changed

5 files changed

+11
-91
lines changed

package-lock.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
},
5858
"dependencies": {
5959
"klona": "^2.0.4",
60-
"neo-async": "^2.6.2",
61-
"semver": "^7.3.4"
60+
"neo-async": "^2.6.2"
6261
},
6362
"devDependencies": {
6463
"@babel/cli": "^7.12.10",
@@ -91,6 +90,7 @@
9190
"npm-run-all": "^4.1.5",
9291
"prettier": "^2.2.1",
9392
"sass": "^1.32.0",
93+
"semver": "^7.3.4",
9494
"standard-version": "^9.1.0",
9595
"style-loader": "^2.0.0",
9696
"webpack": "^5.20.0"

src/utils.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import url from "url";
22
import path from "path";
33

4-
import semver from "semver";
54
import { klona } from "klona/full";
65
import async from "neo-async";
76

@@ -59,26 +58,12 @@ function getSassImplementation(loaderContext, implementation) {
5958
return;
6059
}
6160

62-
const [implementationName, version] = infoParts;
61+
const [implementationName] = infoParts;
6362

6463
if (implementationName === "dart-sass") {
65-
if (!semver.satisfies(version, "^1.3.0")) {
66-
loaderContext.emitError(
67-
new Error(`Dart Sass version ${version} is incompatible with ^1.3.0.`)
68-
);
69-
}
70-
7164
// eslint-disable-next-line consistent-return
7265
return resolvedImplementation;
7366
} else if (implementationName === "node-sass") {
74-
if (!semver.satisfies(version, "^4.0.0 || ^5.0.0")) {
75-
loaderContext.emitError(
76-
new Error(
77-
`Node Sass version ${version} is incompatible with ^4.0.0 || ^5.0.0.`
78-
)
79-
);
80-
}
81-
8267
// eslint-disable-next-line consistent-return
8368
return resolvedImplementation;
8469
}
@@ -230,7 +215,7 @@ async function getSassOptions(
230215
// - ~@org/package
231216
// - ~@org/package/
232217
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
233-
const moduleRequestRegex = /^[^?]*~/;
218+
const MODULE_REQUEST_REGEX = /^[^?]*~/;
234219

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

256241
// In case there is module request, send this to webpack resolver
257242
if (forWebpackResolver) {
258-
request = request.replace(moduleRequestRegex, "");
243+
if (MODULE_REQUEST_REGEX.test(url)) {
244+
request = request.replace(MODULE_REQUEST_REGEX, "");
245+
}
259246

260247
if (isModuleImport.test(url)) {
261248
request = request[request.length - 1] === "/" ? request : `${request}/`;

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ Unknown Sass implementation \\"strange-sass\\".",
3030

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

33-
exports[`implementation option should throw an error when the "info" is unparseable #2: errors 1`] = `
34-
Array [
35-
"ModuleError: Module Error (from ../src/cjs.js):
36-
Node Sass version 1 is incompatible with ^4.0.0 || ^5.0.0.",
37-
]
38-
`;
39-
40-
exports[`implementation option should throw an error when the "info" is unparseable #2: warnings 1`] = `Array []`;
41-
4233
exports[`implementation option should throw an error when the "info" is unparseable: errors 1`] = `
4334
Array [
4435
"ModuleError: Module Error (from ../src/cjs.js):
@@ -48,24 +39,6 @@ Unknown Sass implementation \\"asdfj\\".",
4839

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

51-
exports[`implementation option should throw an error when the "node-sass" package is an incompatible version: errors 1`] = `
52-
Array [
53-
"ModuleError: Module Error (from ../src/cjs.js):
54-
Node Sass version 3.0.0 is incompatible with ^4.0.0 || ^5.0.0.",
55-
]
56-
`;
57-
58-
exports[`implementation option should throw an error when the "node-sass" package is an incompatible version: warnings 1`] = `Array []`;
59-
60-
exports[`implementation option should throw an error when the "sass" package is an incompatible version: errors 1`] = `
61-
Array [
62-
"ModuleError: Module Error (from ../src/cjs.js):
63-
Dart Sass version 1.2.0 is incompatible with ^1.3.0.",
64-
]
65-
`;
66-
67-
exports[`implementation option should throw an error when the "sass" package is an incompatible version: warnings 1`] = `Array []`;
68-
6942
exports[`implementation option should throw error when the "info" does not exist: errors 1`] = `
7043
Array [
7144
"ModuleError: Module Error (from ../src/cjs.js):

test/implementation-option.test.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,6 @@ describe("implementation option", () => {
7474
expect(dartSassSpy).toHaveBeenCalledTimes(1);
7575
});
7676

77-
it('should throw an error when the "node-sass" package is an incompatible version', async () => {
78-
const testId = getTestId("language", "scss");
79-
const options = {
80-
implementation: Object.assign({}, nodeSass, {
81-
info: "node-sass\t3.0.0",
82-
}),
83-
};
84-
85-
const compiler = getCompiler(testId, { loader: { options } });
86-
const stats = await compile(compiler);
87-
88-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
89-
expect(getErrors(stats)).toMatchSnapshot("errors");
90-
});
91-
92-
it('should throw an error when the "sass" package is an incompatible version', async () => {
93-
const testId = getTestId("language", "scss");
94-
const options = {
95-
implementation: Object.assign({}, dartSass, {
96-
info: "dart-sass\t1.2.0",
97-
}),
98-
};
99-
100-
const compiler = getCompiler(testId, { loader: { options } });
101-
const stats = await compile(compiler);
102-
103-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
104-
expect(getErrors(stats)).toMatchSnapshot("errors");
105-
});
106-
10777
it("should throw an error on an unknown sass implementation", async () => {
10878
const testId = getTestId("language", "scss");
10979
const options = {
@@ -132,19 +102,6 @@ describe("implementation option", () => {
132102
expect(getErrors(stats)).toMatchSnapshot("errors");
133103
});
134104

135-
it('should throw an error when the "info" is unparseable #2', async () => {
136-
const testId = getTestId("language", "scss");
137-
const options = {
138-
implementation: Object.assign({}, nodeSass, { info: "node-sass\t1" }),
139-
};
140-
141-
const compiler = getCompiler(testId, { loader: { options } });
142-
const stats = await compile(compiler);
143-
144-
expect(getWarnings(stats)).toMatchSnapshot("warnings");
145-
expect(getErrors(stats)).toMatchSnapshot("errors");
146-
});
147-
148105
it('should throw error when the "info" does not exist', async () => {
149106
const testId = getTestId("language", "scss");
150107
const options = {

0 commit comments

Comments
 (0)