Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

--source-maps=false generates empty, double stringified sourcemaps. #100

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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: JSON.stringify(map),
map: map || undefined,
};
}

Expand Down