Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regressions in source maps #876

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
- ubuntu-latest
- windows-latest
node:
- 12
- 14
- 16

Expand Down
52 changes: 33 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,47 @@ const filePush = (file, compileResult, callback) => {

// Build Source Maps!
if (compileResult.sourceMap) {
const proto = /^file:\/\/?/;
const leadingSlash = /^\//;
const sassMap = compileResult.sourceMap;
const base = path.resolve(file.cwd, file.base);

if (!sassMap.file) {
sassMap.file = file.path;
// Convert from absolute path to relative as in gulp-sass 5.0.0
sassMap.file = file.history[0]
.replace(base + path.sep, '')
.replace(proto, '');
}

// Grab the stdout and transform it into stdin
const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
// Transform to relative file paths as in gulp-sass 5.0.0
sassMap.sources = sassMap.sources.map((src) => {
// file uses Windows-style path separators, source is a URL.
const baseUri = base.replace(/\\/g, '/');
// The current file and its content is included
// as data:<encoded file contents> in the new Sass JS API.
// Map it to the original file name (first history entry).
if (src.startsWith('data:')) {
return file.history[0]
.replace(/\\/g, '/')
.replace(`${baseUri}/`, '')
.replace(proto, '')
.replace(leadingSlash, '');
}
return src
.replace(proto, '')
.replace(`${baseUri}/`, '')
.replace(leadingSlash, '');
});

// Grab the base filename that's being worked on
const sassFileSrc = file.relative;
// Grab the path portion of the file that's being worked on
const sassFileSrcPath = path.dirname(sassFileSrc);

if (sassFileSrcPath) {
const sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
// Prepend the path to all files in the sources array except the file that's being worked on
sassMap.sources = sassMap.sources.map((source, index) => (
index === sourceFileIndex
? source
: path.join(sassFileSrcPath, source)
));
}

// Remove 'stdin' from souces and replace with filenames!
sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src);

// Replace the map file with the original filename (but new extension)
sassMap.file = replaceExtension(sassFileSrc, '.css');

if (file.sourceMap.sourcesContent && !sassMap.sourcesContent) {
sassMap.sourcesContent = file.sourceMap.sourcesContent;
}

// Apply the map
applySourceMap(file, sassMap);
}
Expand Down
Loading