Skip to content

Commit

Permalink
fix: generating empty inline sourcemaps
Browse files Browse the repository at this point in the history
Fixes by lazily init `SourceMapGenerator`
  • Loading branch information
xnuk committed Dec 25, 2021
1 parent 82248e7 commit 8059720
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/swc/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ export default async function ({
file: string,
...results: swc.Output[]
): Promise<swc.Output> {
const map = new SourceMapGenerator({
file,
sourceRoot: swcOptions.sourceRoot,
});
const initMap = () =>
new SourceMapGenerator({
file,
sourceRoot: swcOptions.sourceRoot,
});

let map: SourceMapGenerator | null = null;

let code = "";
let offset = 0;
Expand All @@ -35,6 +38,11 @@ export default async function ({

consumer.eachMapping(mapping => {
sources.add(mapping.source);

if (map == null) {
map = initMap();
}

map.addMapping({
generated: {
line: mapping.generatedLine + offset,
Expand All @@ -50,7 +58,7 @@ export default async function ({

sources.forEach(source => {
const content = consumer.sourceContentFor(source, true);
if (content !== null) {
if (content !== null && map != null) {
map.setSourceContent(source, content);
}
});
Expand All @@ -60,7 +68,7 @@ export default async function ({

return {
code,
map: map,
map: map || undefined,
};
}

Expand Down

0 comments on commit 8059720

Please sign in to comment.