Skip to content

Commit 006c02e

Browse files
refactor: code
1 parent 2a18d5b commit 006c02e

File tree

12 files changed

+140
-107
lines changed

12 files changed

+140
-107
lines changed

src/SassError.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class SassError extends Error {
2-
constructor(sassError, resourcePath) {
2+
constructor(sassError) {
33
super();
44

55
this.name = 'SassError';
@@ -13,13 +13,13 @@ class SassError extends Error {
1313
this.message = `${this.name}: ${this.originalSassError.message}`;
1414

1515
if (this.originalSassError.formatted) {
16-
this.message = `${this.name}: ${this.originalSassError.formatted
17-
.replace(/^Error: /, '')
18-
.replace(/(\s*)stdin(\s*)/, `$1${resourcePath}$2`)}`;
16+
this.message = `${this.name}: ${this.originalSassError.formatted.replace(
17+
/^Error: /,
18+
''
19+
)}`;
1920

2021
// Instruct webpack to hide the JS stack from the console.
2122
// Usually you're only interested in the SASS stack in this case.
22-
// eslint-disable-next-line no-param-reassign
2323
this.hideStack = true;
2424

2525
Error.captureStackTrace(this, this.constructor);

src/getSassOptions.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ import path from 'path';
22

33
import cloneDeep from 'clone-deep';
44

5-
import proxyCustomImporters from './proxyCustomImporters';
6-
75
function isProductionLikeMode(loaderContext) {
8-
return (
9-
loaderContext.mode === 'production' ||
10-
!loaderContext.mode ||
11-
loaderContext.minimize
12-
);
6+
return loaderContext.mode === 'production' || !loaderContext.mode;
137
}
148

159
/**
@@ -81,7 +75,7 @@ function getSassOptions(loaderContext, loaderOptions, content, implementation) {
8175
// But since we're using the data option, the source map will not actually be written, but
8276
// all paths in sourceMap.sources will be relative to that path.
8377
// Pretty complicated... :(
84-
options.sourceMap = path.join(process.cwd(), '/sass.map');
78+
options.sourceMap = path.join(process.cwd(), '/sass.css.map');
8579
options.sourceMapRoot = process.cwd();
8680
options.sourceMapContents = true;
8781
options.omitSourceMapUrl = true;
@@ -102,9 +96,11 @@ function getSassOptions(loaderContext, loaderOptions, content, implementation) {
10296
options.indentedSyntax = Boolean(options.indentedSyntax);
10397
}
10498

105-
// Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
99+
// Allow passing custom importers to `sass`/`node-sass`. Accepts `Function` or an array of `Function`s.
106100
options.importer = options.importer
107-
? proxyCustomImporters(options.importer, resourcePath)
101+
? Array.isArray(options.importer)
102+
? options.importer
103+
: [options.importer]
108104
: [];
109105

110106
options.includePaths = []

src/index.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SassError from './SassError';
1717
* @param {string} content
1818
*/
1919
function loader(content) {
20-
const options = getOptions(this) || {};
20+
const options = getOptions(this);
2121

2222
validateOptions(schema, options, {
2323
name: 'Sass Loader',
@@ -39,28 +39,19 @@ function loader(content) {
3939
}
4040

4141
const callback = this.async();
42-
43-
// Skip empty files, otherwise it will stop webpack, see issue #21
44-
if (sassOptions.data.trim() === '') {
45-
callback(null, '');
46-
47-
return;
48-
}
49-
5042
const render = getRenderFunctionFromSassImplementation(implementation);
5143

5244
render(sassOptions, (error, result) => {
5345
if (error) {
54-
if (error.file) {
55-
this.addDependency(path.normalize(error.file));
56-
}
46+
// `node-sass` returns POSIX paths
47+
this.addDependency(path.normalize(error.file));
5748

58-
callback(new SassError(error, this.resourcePath));
49+
callback(new SassError(error));
5950

6051
return;
6152
}
6253

63-
if (result.map && result.map !== '{}') {
54+
if (result.map) {
6455
// eslint-disable-next-line no-param-reassign
6556
result.map = JSON.parse(result.map);
6657

@@ -69,33 +60,13 @@ function loader(content) {
6960
// eslint-disable-next-line no-param-reassign
7061
delete result.map.file;
7162

72-
// One of the sources is 'stdin' according to dart-sass/node-sass because we've used the data input.
73-
// Now let's override that value with the correct relative path.
74-
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in getSassOptions,
75-
// we know that this path is relative to process.cwd(). This is how node-sass works.
76-
// eslint-disable-next-line no-param-reassign
77-
const stdinIndex = result.map.sources.findIndex((source) =>
78-
source.includes('stdin')
79-
);
80-
81-
if (stdinIndex !== -1) {
82-
// eslint-disable-next-line no-param-reassign
83-
result.map.sources[stdinIndex] = path.relative(
84-
process.cwd(),
85-
this.resourcePath
86-
);
87-
}
88-
8963
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
9064
// This fixes an error on windows where the source-map module cannot resolve the source maps.
9165
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
9266
// eslint-disable-next-line no-param-reassign
9367
result.map.sourceRoot = path.normalize(result.map.sourceRoot);
9468
// eslint-disable-next-line no-param-reassign
9569
result.map.sources = result.map.sources.map(path.normalize);
96-
} else {
97-
// eslint-disable-next-line no-param-reassign
98-
result.map = null;
9970
}
10071

10172
result.stats.includedFiles.forEach((includedFile) => {

src/proxyCustomImporters.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/webpackImporter.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ const matchCss = /\.css$/i;
2020
*
2121
*/
2222
function webpackImporter(loaderContext, includePaths) {
23-
function dirContextFrom(fileContext) {
24-
return path.dirname(
25-
// The first file is 'stdin' when we're using the data option
26-
fileContext === 'stdin' ? loaderContext.resourcePath : fileContext
27-
);
28-
}
29-
3023
function startResolving(resolutionMap) {
3124
if (resolutionMap.length === 0) {
3225
return Promise.reject();
@@ -116,7 +109,7 @@ function webpackImporter(loaderContext, includePaths) {
116109

117110
resolutionMap = resolutionMap.concat({
118111
resolve: webpackResolve,
119-
context: isFileScheme ? '/' : dirContextFrom(prev),
112+
context: isFileScheme ? '/' : path.dirname(prev),
120113
possibleRequests: webpackPossibleRequests,
121114
});
122115

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,29 +1732,53 @@ exports[`sassOptions option should work with the "functions" option (node-sass)
17321732

17331733
exports[`sassOptions option should work with the "functions" option (node-sass) (scss): warnings 1`] = `Array []`;
17341734

1735-
exports[`sassOptions option should work with the "importer" option (dart-sass) (sass): css 1`] = `""`;
1735+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (sass): css 1`] = `""`;
17361736

1737-
exports[`sassOptions option should work with the "importer" option (dart-sass) (sass): errors 1`] = `Array []`;
1737+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (sass): errors 1`] = `Array []`;
17381738

1739-
exports[`sassOptions option should work with the "importer" option (dart-sass) (sass): warnings 1`] = `Array []`;
1739+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (sass): warnings 1`] = `Array []`;
17401740

1741-
exports[`sassOptions option should work with the "importer" option (dart-sass) (scss): css 1`] = `""`;
1741+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (scss): css 1`] = `""`;
17421742

1743-
exports[`sassOptions option should work with the "importer" option (dart-sass) (scss): errors 1`] = `Array []`;
1743+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (scss): errors 1`] = `Array []`;
17441744

1745-
exports[`sassOptions option should work with the "importer" option (dart-sass) (scss): warnings 1`] = `Array []`;
1745+
exports[`sassOptions option should work with the "importer" as a array of functions option (dart-sass) (scss): warnings 1`] = `Array []`;
17461746

1747-
exports[`sassOptions option should work with the "importer" option (node-sass) (sass): css 1`] = `""`;
1747+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (sass): css 1`] = `""`;
17481748

1749-
exports[`sassOptions option should work with the "importer" option (node-sass) (sass): errors 1`] = `Array []`;
1749+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (sass): errors 1`] = `Array []`;
17501750

1751-
exports[`sassOptions option should work with the "importer" option (node-sass) (sass): warnings 1`] = `Array []`;
1751+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (sass): warnings 1`] = `Array []`;
17521752

1753-
exports[`sassOptions option should work with the "importer" option (node-sass) (scss): css 1`] = `""`;
1753+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (scss): css 1`] = `""`;
17541754

1755-
exports[`sassOptions option should work with the "importer" option (node-sass) (scss): errors 1`] = `Array []`;
1755+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (scss): errors 1`] = `Array []`;
17561756

1757-
exports[`sassOptions option should work with the "importer" option (node-sass) (scss): warnings 1`] = `Array []`;
1757+
exports[`sassOptions option should work with the "importer" as a array of functions option (node-sass) (scss): warnings 1`] = `Array []`;
1758+
1759+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (sass): css 1`] = `""`;
1760+
1761+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (sass): errors 1`] = `Array []`;
1762+
1763+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (sass): warnings 1`] = `Array []`;
1764+
1765+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (scss): css 1`] = `""`;
1766+
1767+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (scss): errors 1`] = `Array []`;
1768+
1769+
exports[`sassOptions option should work with the "importer" as a single function option (dart-sass) (scss): warnings 1`] = `Array []`;
1770+
1771+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (sass): css 1`] = `""`;
1772+
1773+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (sass): errors 1`] = `Array []`;
1774+
1775+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (sass): warnings 1`] = `Array []`;
1776+
1777+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (scss): css 1`] = `""`;
1778+
1779+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (scss): errors 1`] = `Array []`;
1780+
1781+
exports[`sassOptions option should work with the "importer" as a single function option (node-sass) (scss): warnings 1`] = `Array []`;
17581782

17591783
exports[`sassOptions option should work with the "includePaths" option (dart-sass) (sass): css 1`] = `
17601784
".include-path-module {

test/helpers/customImporter.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
function customImporter(path, prev) {
2-
expect(path).toBe('import-with-custom-logic');
1+
function customImporter(url, prev, done) {
2+
expect(url).toBe('import-with-custom-logic');
33
expect(prev).toMatch(/(sass|scss)[/\\]custom-importer\.(scss|sass)/);
44
expect(this.options).toBeDefined();
55

6+
if (done) {
7+
done(customImporter.returnValue);
8+
9+
return;
10+
}
11+
12+
// eslint-disable-next-line consistent-return
613
return customImporter.returnValue;
714
}
815

test/helpers/getCodeFromSass.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,13 @@ function getCodeFromSass(testId, options) {
723723
}
724724

725725
sassOptions.importer = sassOptions.importer
726-
? [sassOptions.importer, testImporter]
726+
? []
727+
.concat(
728+
Array.isArray(sassOptions.importer)
729+
? [...sassOptions.importer]
730+
: [sassOptions.importer]
731+
)
732+
.concat([testImporter])
727733
: [testImporter];
728734

729735
const { css, map } = implementation.renderSync(sassOptions);

test/helpers/getErrors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import normalizeErrors from './normalizeErrors';
22

33
export default (stats) => {
4-
return normalizeErrors(stats.compilation.errors);
4+
return normalizeErrors(stats.compilation.errors.sort());
55
};

test/helpers/getWarnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import normalizeErrors from './normalizeErrors';
22

33
export default (stats) => {
4-
return normalizeErrors(stats.compilation.warnings);
4+
return normalizeErrors(stats.compilation.warnings.sort());
55
};

0 commit comments

Comments
 (0)