Skip to content

Optimize external source maps without full cache #40130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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: 19 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ namespace ts {
let sourceMapGenerator: SourceMapGenerator | undefined;
let sourceMapSource: SourceMapSource;
let sourceMapSourceIndex = -1;
let mostRecentlyAddedSourceMapSource: SourceMapSource;
let mostRecentlyAddedSourceMapSourceIndex = -1;

// Comments
let containerPos = -1;
Expand Down Expand Up @@ -5337,9 +5339,10 @@ namespace ts {
function emitSourcePos(source: SourceMapSource, pos: number) {
if (source !== sourceMapSource) {
const savedSourceMapSource = sourceMapSource;
const savedSourceMapSourceIndex = sourceMapSourceIndex;
setSourceMapSource(source);
emitPos(pos);
setSourceMapSource(savedSourceMapSource);
resetSourceMapSource(savedSourceMapSource, savedSourceMapSourceIndex);
}
else {
emitPos(pos);
Expand Down Expand Up @@ -5386,6 +5389,13 @@ namespace ts {

sourceMapSource = source;

if (source === mostRecentlyAddedSourceMapSource) {
// Fast path for when the new source map is the most recently added, in which case
// we use its captured index without going through the source map generator.
sourceMapSourceIndex = mostRecentlyAddedSourceMapSourceIndex;
return;
}

if (isJsonSourceMapSource(source)) {
return;
}
Expand All @@ -5394,6 +5404,14 @@ namespace ts {
if (printerOptions.inlineSources) {
sourceMapGenerator!.setSourceContent(sourceMapSourceIndex, source.text);
}

mostRecentlyAddedSourceMapSource = source;
mostRecentlyAddedSourceMapSourceIndex = sourceMapSourceIndex;
}

function resetSourceMapSource(source: SourceMapSource, sourceIndex: number) {
sourceMapSource = source;
sourceMapSourceIndex = sourceIndex;
}

function isJsonSourceMapSource(sourceFile: SourceMapSource) {
Expand Down