Skip to content

Commit 8327d55

Browse files
fix: source maps generation (#886)
1 parent 53592da commit 8327d55

File tree

4 files changed

+596
-74
lines changed

4 files changed

+596
-74
lines changed

src/index.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getSassOptions,
1010
getWebpackImporter,
1111
getRenderFunctionFromSassImplementation,
12-
absolutifySourceMapSource,
12+
normalizeSourceMap,
1313
} from './utils';
1414
import SassError from './SassError';
1515

@@ -66,33 +66,18 @@ function loader(content) {
6666
return;
6767
}
6868

69+
let map = result.map ? JSON.parse(result.map) : null;
70+
6971
// Modify source paths only for webpack, otherwise we do nothing
70-
if (result.map && useSourceMap) {
71-
// eslint-disable-next-line no-param-reassign
72-
result.map = JSON.parse(result.map);
73-
74-
// result.map.file is an optional property that provides the output filename.
75-
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
76-
// eslint-disable-next-line no-param-reassign
77-
delete result.map.file;
78-
79-
// eslint-disable-next-line no-param-reassign
80-
result.sourceRoot = '';
81-
82-
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
83-
// This fixes an error on windows where the source-map module cannot resolve the source maps.
84-
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
85-
// eslint-disable-next-line no-param-reassign
86-
result.map.sources = result.map.sources.map((source) =>
87-
absolutifySourceMapSource(this.rootContext, source)
88-
);
72+
if (map && useSourceMap) {
73+
map = normalizeSourceMap(map, this.rootContext);
8974
}
9075

9176
result.stats.includedFiles.forEach((includedFile) => {
9277
this.addDependency(path.normalize(includedFile));
9378
});
9479

95-
callback(null, result.css.toString(), result.map);
80+
callback(null, result.css.toString(), map);
9681
});
9782
}
9883

src/utils.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ function getSassOptions(
159159
// Pretty complicated... :(
160160
options.sourceMap = true;
161161
options.outFile = path.join(loaderContext.rootContext, 'style.css.map');
162-
// options.sourceMapRoot = process.cwd();
163162
options.sourceMapContents = true;
164163
options.omitSourceMapUrl = true;
165164
options.sourceMapEmbed = false;
@@ -507,15 +506,33 @@ function getURLType(source) {
507506
return ABSOLUTE_SCHEME.test(source) ? 'absolute' : 'path-relative';
508507
}
509508

510-
function absolutifySourceMapSource(sourceRoot, source) {
511-
const sourceType = getURLType(source);
509+
function normalizeSourceMap(map, rootContext) {
510+
const newMap = map;
512511

513-
// Do no touch `scheme-relative`, `path-absolute` and `absolute` types
514-
if (sourceType === 'path-relative') {
515-
return path.resolve(sourceRoot, path.normalize(source));
516-
}
512+
// result.map.file is an optional property that provides the output filename.
513+
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
514+
// eslint-disable-next-line no-param-reassign
515+
delete newMap.file;
516+
517+
// eslint-disable-next-line no-param-reassign
518+
newMap.sourceRoot = '';
519+
520+
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
521+
// This fixes an error on windows where the source-map module cannot resolve the source maps.
522+
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
523+
// eslint-disable-next-line no-param-reassign
524+
newMap.sources = newMap.sources.map((source) => {
525+
const sourceType = getURLType(source);
526+
527+
// Do no touch `scheme-relative`, `path-absolute` and `absolute` types
528+
if (sourceType === 'path-relative') {
529+
return path.resolve(rootContext, path.normalize(source));
530+
}
531+
532+
return source;
533+
});
517534

518-
return source;
535+
return newMap;
519536
}
520537

521538
export {
@@ -524,5 +541,5 @@ export {
524541
getWebpackResolver,
525542
getWebpackImporter,
526543
getRenderFunctionFromSassImplementation,
527-
absolutifySourceMapSource,
544+
normalizeSourceMap,
528545
};

0 commit comments

Comments
 (0)