Skip to content

Commit 9ecd90f

Browse files
committed
Generate a simple source map if an existing one didn't exist
1 parent 9a4e69b commit 9ecd90f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

packages/react-server-dom-webpack/src/ReactFlightWebpackNodeLoader.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ function transformServerModule(
359359
lastMappedLine++;
360360
}
361361

362-
sourceLineCount = program.loc.end.line - 1;
362+
sourceLineCount = program.loc.end.line;
363363
if (sourceLineCount < lastMappedLine) {
364364
throw new Error(
365365
'The source map has more mappings than there are lines.',
@@ -374,13 +374,43 @@ function transformServerModule(
374374
) {
375375
mappings += ';';
376376
}
377+
} else {
378+
// If a file doesn't have a source map then we generate a blank source map that just
379+
// contains the original content and segments pointing to the original lines.
380+
sourceLineCount = 1;
381+
let idx = -1;
382+
while ((idx = source.indexOf('\n', idx + 1)) !== -1) {
383+
sourceLineCount++;
384+
}
385+
mappings = 'AAAA' + ';AACA'.repeat(sourceLineCount - 1);
386+
sourceMap = {
387+
version: 3,
388+
sources: [url],
389+
sourcesContent: [source],
390+
mappings: mappings,
391+
sourceRoot: '',
392+
};
393+
lastSourceIndex = 0;
394+
lastOriginalLine = sourceLineCount;
395+
lastOriginalColumn = 0;
396+
lastNameIndex = -1;
397+
lastMappedLine = sourceLineCount;
398+
399+
for (let i = 0; i < exportedEntries.length; i++) {
400+
// Point each entry to original location.
401+
const entry = exportedEntries[i];
402+
entry.originalSource = 0;
403+
entry.originalLine = entry.loc.start.line;
404+
// We use column zero since we do the short-hand line-only source maps above.
405+
entry.originalColumn = 0; // entry.loc.start.column;
406+
}
377407
}
378408

379409
newSrc += '\n\n;';
380410
newSrc +=
381411
'import {registerServerReference} from "react-server-dom-webpack/server";\n';
382412
if (mappings) {
383-
mappings += ';;;';
413+
mappings += ';;';
384414
}
385415

386416
const createMapping = createMappingsSerializer();
@@ -417,7 +447,7 @@ function transformServerModule(
417447
}
418448
}
419449

420-
if (sourceMap && mappings) {
450+
if (sourceMap) {
421451
// Override with an new mappings and serialize an inline source map.
422452
sourceMap.mappings = mappings;
423453
newSrc +=

0 commit comments

Comments
 (0)