Skip to content

Commit

Permalink
Fix source map URLs for the legacy API (#113)
Browse files Browse the repository at this point in the history
If an importer was passed, these URLs were broken by our end-of-load
import injection. This fixes the issue by explicitly passing a
`sourceMapUrl` field which allows the wrapper to reconstruct the
correct URLs.

See webpack-contrib/sass-loader#774 (comment)
  • Loading branch information
nex3 authored Feb 17, 2022
1 parent 8d3a982 commit aeac846
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
9 changes: 7 additions & 2 deletions lib/src/legacy/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ export class LegacyImporterWrapper<sync extends 'sync' | 'async'>
load(canonicalUrl: URL): ImporterResult | null {
if (canonicalUrl.protocol === endOfLoadProtocol) {
this.prev.pop();
return {contents: '', syntax: 'scss'};
return {
contents: '',
syntax: 'scss',
sourceMapUrl: new URL(endOfLoadProtocol),
};
}

if (canonicalUrl.protocol === 'file:') {
Expand All @@ -195,7 +199,7 @@ export class LegacyImporterWrapper<sync extends 'sync' | 'async'>
this.prev.pop();
}

return {contents, syntax};
return {contents, syntax, sourceMapUrl: canonicalUrl};
}

const lastContents = this.lastContents;
Expand All @@ -204,6 +208,7 @@ export class LegacyImporterWrapper<sync extends 'sync' | 'async'>
return {
contents: lastContents + this.endOfLoadImport,
syntax: 'scss',
sourceMapUrl: canonicalUrl,
};
}

Expand Down
26 changes: 15 additions & 11 deletions lib/src/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,21 @@ function newLegacyResult(
sourceMap.file = 'stdin.css';
}

sourceMap.sources = sourceMap.sources.map(source => {
if (source.startsWith('file://')) {
return pathToUrlString(
p.relative(sourceMapDir, fileUrlToPathCrossPlatform(source))
);
} else if (source.startsWith('data:')) {
return 'stdin';
} else {
return source;
}
});
sourceMap.sources = sourceMap.sources
.filter(source => !source.startsWith(endOfLoadProtocol))
.map(source => {
if (source.startsWith('file://')) {
return pathToUrlString(
p.relative(sourceMapDir, fileUrlToPathCrossPlatform(source))
);
} else if (source.startsWith(legacyImporterProtocol)) {
return source.substring(legacyImporterProtocol.length);
} else if (source.startsWith('data:')) {
return 'stdin';
} else {
return source;
}
});

sourceMapBytes = Buffer.from(JSON.stringify(sourceMap));

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sass-embedded",
"version": "1.49.7",
"version": "1.49.8-dev",
"protocol-version": "1.0.0",
"compiler-version": "1.49.7",
"description": "Node.js library that communicates with Embedded Dart Sass using the Embedded Sass protocol",
Expand Down

0 comments on commit aeac846

Please sign in to comment.