Skip to content

Commit 9ce7c0c

Browse files
authored
Fix regressions in source maps (#876)
* Silence import deprecation warning while node-sass is supported * fix: convert to same sourcemap format as gulp-sass v5 Default to relative paths in the sources array. * test: fix timing issue of rimraf that broke tests on Windows * chore: upgrade rimraf to a version more stable on Windows Drop testing on Node 12 * test: move deletion to before, debugging a timing issue on CI
1 parent 8a45970 commit 9ce7c0c

File tree

5 files changed

+709
-177
lines changed

5 files changed

+709
-177
lines changed

.github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ jobs:
4141
- ubuntu-latest
4242
- windows-latest
4343
node:
44-
- 12
4544
- 14
4645
- 16
4746

index.js

+33-19
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,47 @@ const filePush = (file, compileResult, callback) => {
3030

3131
// Build Source Maps!
3232
if (compileResult.sourceMap) {
33+
const proto = /^file:\/\/?/;
34+
const leadingSlash = /^\//;
3335
const sassMap = compileResult.sourceMap;
36+
const base = path.resolve(file.cwd, file.base);
37+
3438
if (!sassMap.file) {
35-
sassMap.file = file.path;
39+
// Convert from absolute path to relative as in gulp-sass 5.0.0
40+
sassMap.file = file.history[0]
41+
.replace(base + path.sep, '')
42+
.replace(proto, '');
3643
}
3744

38-
// Grab the stdout and transform it into stdin
39-
const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
45+
// Transform to relative file paths as in gulp-sass 5.0.0
46+
sassMap.sources = sassMap.sources.map((src) => {
47+
// file uses Windows-style path separators, source is a URL.
48+
const baseUri = base.replace(/\\/g, '/');
49+
// The current file and its content is included
50+
// as data:<encoded file contents> in the new Sass JS API.
51+
// Map it to the original file name (first history entry).
52+
if (src.startsWith('data:')) {
53+
return file.history[0]
54+
.replace(/\\/g, '/')
55+
.replace(`${baseUri}/`, '')
56+
.replace(proto, '')
57+
.replace(leadingSlash, '');
58+
}
59+
return src
60+
.replace(proto, '')
61+
.replace(`${baseUri}/`, '')
62+
.replace(leadingSlash, '');
63+
});
64+
4065
// Grab the base filename that's being worked on
4166
const sassFileSrc = file.relative;
42-
// Grab the path portion of the file that's being worked on
43-
const sassFileSrcPath = path.dirname(sassFileSrc);
44-
45-
if (sassFileSrcPath) {
46-
const sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
47-
// Prepend the path to all files in the sources array except the file that's being worked on
48-
sassMap.sources = sassMap.sources.map((source, index) => (
49-
index === sourceFileIndex
50-
? source
51-
: path.join(sassFileSrcPath, source)
52-
));
53-
}
54-
55-
// Remove 'stdin' from souces and replace with filenames!
56-
sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src);
57-
5867
// Replace the map file with the original filename (but new extension)
5968
sassMap.file = replaceExtension(sassFileSrc, '.css');
69+
70+
if (file.sourceMap.sourcesContent && !sassMap.sourcesContent) {
71+
sassMap.sourcesContent = file.sourceMap.sourcesContent;
72+
}
73+
6074
// Apply the map
6175
applySourceMap(file, sassMap);
6276
}

0 commit comments

Comments
 (0)