Skip to content

Commit

Permalink
Generate a simple source map if an existing one didn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Aug 20, 2024
1 parent 9a4e69b commit 9ecd90f
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function transformServerModule(
lastMappedLine++;
}

sourceLineCount = program.loc.end.line - 1;
sourceLineCount = program.loc.end.line;
if (sourceLineCount < lastMappedLine) {
throw new Error(
'The source map has more mappings than there are lines.',
Expand All @@ -374,13 +374,43 @@ function transformServerModule(
) {
mappings += ';';
}
} else {
// If a file doesn't have a source map then we generate a blank source map that just
// contains the original content and segments pointing to the original lines.
sourceLineCount = 1;
let idx = -1;
while ((idx = source.indexOf('\n', idx + 1)) !== -1) {
sourceLineCount++;
}
mappings = 'AAAA' + ';AACA'.repeat(sourceLineCount - 1);
sourceMap = {
version: 3,
sources: [url],
sourcesContent: [source],
mappings: mappings,
sourceRoot: '',
};
lastSourceIndex = 0;
lastOriginalLine = sourceLineCount;
lastOriginalColumn = 0;
lastNameIndex = -1;
lastMappedLine = sourceLineCount;

for (let i = 0; i < exportedEntries.length; i++) {
// Point each entry to original location.
const entry = exportedEntries[i];
entry.originalSource = 0;
entry.originalLine = entry.loc.start.line;
// We use column zero since we do the short-hand line-only source maps above.
entry.originalColumn = 0; // entry.loc.start.column;
}
}

newSrc += '\n\n;';
newSrc +=
'import {registerServerReference} from "react-server-dom-webpack/server";\n';
if (mappings) {
mappings += ';;;';
mappings += ';;';
}

const createMapping = createMappingsSerializer();
Expand Down Expand Up @@ -417,7 +447,7 @@ function transformServerModule(
}
}

if (sourceMap && mappings) {
if (sourceMap) {
// Override with an new mappings and serialize an inline source map.
sourceMap.mappings = mappings;
newSrc +=
Expand Down

0 comments on commit 9ecd90f

Please sign in to comment.