-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
index.js
99 lines (81 loc) · 2.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import path from 'path';
import validateOptions from 'schema-utils';
import { getOptions } from 'loader-utils';
import schema from './options.json';
import {
getSassImplementation,
getSassOptions,
getWebpackImporter,
getRenderFunctionFromSassImplementation,
absolutifySourceMapSource,
} from './utils';
import SassError from './SassError';
/**
* The sass-loader makes node-sass and dart-sass available to webpack modules.
*
* @this {object}
* @param {string} content
*/
function loader(content) {
const options = getOptions(this);
validateOptions(schema, options, {
name: 'Sass Loader',
baseDataPath: 'options',
});
const implementation = getSassImplementation(options.implementation);
const useSourceMap =
typeof options.sourceMap === 'boolean' ? options.sourceMap : this.sourceMap;
const sassOptions = getSassOptions(
this,
options,
content,
implementation,
useSourceMap
);
const shouldUseWebpackImporter =
typeof options.webpackImporter === 'boolean'
? options.webpackImporter
: true;
if (shouldUseWebpackImporter) {
const { includePaths } = sassOptions;
sassOptions.importer.push(
getWebpackImporter(this, implementation, includePaths)
);
}
const callback = this.async();
const render = getRenderFunctionFromSassImplementation(implementation);
render(sassOptions, (error, result) => {
if (error) {
// There are situations when the `file` property do not exist
if (error.file) {
// `node-sass` returns POSIX paths
this.addDependency(path.normalize(error.file));
}
callback(new SassError(error));
return;
}
// Modify source paths only for webpack, otherwise we do nothing
if (result.map && useSourceMap) {
// eslint-disable-next-line no-param-reassign
result.map = JSON.parse(result.map);
// result.map.file is an optional property that provides the output filename.
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
// eslint-disable-next-line no-param-reassign
delete result.map.file;
// eslint-disable-next-line no-param-reassign
result.sourceRoot = '';
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
// This fixes an error on windows where the source-map module cannot resolve the source maps.
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
// eslint-disable-next-line no-param-reassign
result.map.sources = result.map.sources.map((source) =>
absolutifySourceMapSource(this.rootContext, source)
);
}
result.stats.includedFiles.forEach((includedFile) => {
this.addDependency(path.normalize(includedFile));
});
callback(null, result.css.toString(), result.map);
});
}
export default loader;