Skip to content

Commit

Permalink
Added some more caching logic to the SourceMapManager
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdp committed Jan 22, 2025
1 parent 1a10c07 commit 491829b
Showing 1 changed file with 46 additions and 28 deletions.
74 changes: 46 additions & 28 deletions src/managers/SourceMapManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class SourceMapManager {
} catch (e) {
this.logger.error(`Error loading or parsing source map for '${sourceMapPath}'`, e);
}
} else {
this.cache[key] = null;
}
}
return this.cache[key];
Expand Down Expand Up @@ -88,38 +90,46 @@ export class SourceMapManager {
let sourceMapPath = `${filePath}.map`;

//if we have a source map, use it
if (await fsExtra.pathExists(sourceMapPath)) {
let parsedSourceMap = await this.getSourceMap(sourceMapPath);
if (parsedSourceMap) {
let position = await SourceMapConsumer.with(parsedSourceMap, null, (consumer) => {
return consumer.originalPositionFor({
line: currentLineNumber,
column: currentColumnIndex,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
});
if (position?.source) {
return {
columnIndex: position.column,
lineNumber: position.line,
filePath: position.source
};
}
//if the sourcemap didn't find a valid mapped location,
//try to fallback to the first source referenced in the map
if (parsedSourceMap.sources?.[0]) {
return {
columnIndex: currentColumnIndex,
lineNumber: currentLineNumber,
filePath: parsedSourceMap.sources[0]
};
} else {
return undefined;
}
let parsedSourceMap = await this.getSourceMap(sourceMapPath);
if (parsedSourceMap) {
const consumer = await this.getSourceMapConsumer(sourceMapPath, parsedSourceMap);
let position = consumer.originalPositionFor({
line: currentLineNumber,
column: currentColumnIndex,
bias: SourceMapConsumer.LEAST_UPPER_BOUND
});
if (position?.source) {
return {
columnIndex: position.column,
lineNumber: position.line,
filePath: position.source
};
}
//if the sourcemap didn't find a valid mapped location,
//try to fallback to the first source referenced in the map
if (parsedSourceMap.sources?.[0]) {
return {
columnIndex: currentColumnIndex,
lineNumber: currentLineNumber,
filePath: parsedSourceMap.sources[0]
};
} else {
return undefined;
}
}
}

private sourceMapConsumerCache: Record<string, SourceMapConsumer> = {};

private async getSourceMapConsumer(sourceMapPath: string, parsedSourceMap: RawSourceMap) {
let consumer = this.sourceMapConsumerCache[sourceMapPath];
if (!consumer) {
consumer = await new SourceMapConsumer(parsedSourceMap);
this.sourceMapConsumerCache[sourceMapPath] = consumer;
}
return consumer;
}

/**
* Given a source location, find the generated location using source maps
*/
Expand Down Expand Up @@ -159,4 +169,12 @@ export class SourceMapManager {
}));
return locations;
}

public destroy() {
// Clean up all source map consumers
for (let key in this.sourceMapConsumerCache) {
let consumer = this.sourceMapConsumerCache[key];
consumer?.destroy?.();
}
}
}

0 comments on commit 491829b

Please sign in to comment.