Skip to content

Commit

Permalink
Slightly speed up mapping generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 3, 2022
1 parent 3652776 commit c87ffbe
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/utils/collapseSourcemaps.ts
Expand Up @@ -47,6 +47,7 @@ class Link {

traceMappings() {
const sources: string[] = [];
const sourceIndexMap = new Map<string, number>();
const sourcesContent: string[] = [];
const names: string[] = [];
const nameIndexMap = new Map<string, number>();
Expand All @@ -68,36 +69,34 @@ class Link {
);

if (traced) {
// newer sources are more likely to be used, so search backwards.
let sourceIndex = sources.lastIndexOf(traced.source.filename);
if (sourceIndex === -1) {
const {
column,
line,
name,
source: { content, filename }
} = traced;
let sourceIndex = sourceIndexMap.get(filename);
if (sourceIndex === undefined) {
sourceIndex = sources.length;
sources.push(traced.source.filename);
sourcesContent[sourceIndex] = traced.source.content;
sources.push(filename);
sourceIndexMap.set(filename, sourceIndex);
sourcesContent[sourceIndex] = content;
} else if (sourcesContent[sourceIndex] == null) {
sourcesContent[sourceIndex] = traced.source.content;
} else if (
traced.source.content != null &&
sourcesContent[sourceIndex] !== traced.source.content
) {
sourcesContent[sourceIndex] = content;
} else if (content != null && sourcesContent[sourceIndex] !== content) {
return error({
message: `Multiple conflicting contents for sourcemap source ${traced.source.filename}`
message: `Multiple conflicting contents for sourcemap source ${filename}`
});
}

const tracedSegment: SourceMapSegment = [
segment[0],
sourceIndex,
traced.line,
traced.column
];
const tracedSegment: SourceMapSegment = [segment[0], sourceIndex, line, column];

if (traced.name) {
let nameIndex = nameIndexMap.get(traced.name);
if (name) {
let nameIndex = nameIndexMap.get(name);
if (nameIndex === undefined) {
nameIndex = names.length;
names.push(traced.name);
nameIndexMap.set(traced.name, nameIndex);
names.push(name);
nameIndexMap.set(name, nameIndex);
}

(tracedSegment as SourceMapSegment)[4] = nameIndex;
Expand Down

0 comments on commit c87ffbe

Please sign in to comment.