@@ -239,6 +239,8 @@ Chunk.prototype.then = function <T>(
239
239
}
240
240
} ;
241
241
242
+ export type FindSourceMapURLCallback = ( fileName : string ) => null | string ;
243
+
242
244
export type Response = {
243
245
_bundlerConfig : SSRModuleMap ,
244
246
_moduleLoading : ModuleLoading ,
@@ -255,6 +257,7 @@ export type Response = {
255
257
_buffer : Array < Uint8Array > , // chunks received so far as part of this row
256
258
_tempRefs : void | TemporaryReferenceSet , // the set temporary references can be resolved from
257
259
_debugRootTask ?: null | ConsoleTask , // DEV-only
260
+ _debugFindSourceMapURL ?: void | FindSourceMapURLCallback , // DEV-only
258
261
} ;
259
262
260
263
function readChunk < T > (chunk: SomeChunk< T > ): T {
@@ -696,7 +699,7 @@ function createElement(
696
699
console ,
697
700
getTaskName ( type ) ,
698
701
) ;
699
- const callStack = buildFakeCallStack ( stack , createTaskFn ) ;
702
+ const callStack = buildFakeCallStack ( response , stack , createTaskFn ) ;
700
703
// This owner should ideally have already been initialized to avoid getting
701
704
// user stack frames on the stack.
702
705
const ownerTask =
@@ -1140,6 +1143,7 @@ export function createResponse(
1140
1143
encodeFormAction : void | EncodeFormActionCallback ,
1141
1144
nonce : void | string ,
1142
1145
temporaryReferences : void | TemporaryReferenceSet ,
1146
+ findSourceMapURL : void | FindSourceMapURLCallback ,
1143
1147
) : Response {
1144
1148
const chunks : Map < number , SomeChunk < any >> = new Map ( ) ;
1145
1149
const response : Response = {
@@ -1166,6 +1170,9 @@ export function createResponse(
1166
1170
// TODO: Make this string configurable.
1167
1171
response. _debugRootTask = ( console : any ) . createTask ( '"use server"' ) ;
1168
1172
}
1173
+ if ( __DEV__ ) {
1174
+ response . _debugFindSourceMapURL = findSourceMapURL ;
1175
+ }
1169
1176
// Don't inline this call because it causes closure to outline the call above.
1170
1177
response . _fromJSON = createFromJSONCallback ( response ) ;
1171
1178
return response ;
@@ -1673,6 +1680,7 @@ const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
1673
1680
function createFakeFunction < T > (
1674
1681
name: string,
1675
1682
filename: string,
1683
+ sourceMap: null | string,
1676
1684
line: number,
1677
1685
col: number,
1678
1686
): FakeFunction< T > {
@@ -1697,7 +1705,9 @@ function createFakeFunction<T>(
1697
1705
'_()\n' ;
1698
1706
}
1699
1707
1700
- if (filename) {
1708
+ if (sourceMap) {
1709
+ code += '//# sourceMappingURL=' + sourceMap ;
1710
+ } else if (filename) {
1701
1711
code += '//# sourceURL=' + filename ;
1702
1712
}
1703
1713
@@ -1720,10 +1730,18 @@ function createFakeFunction<T>(
1720
1730
return fn;
1721
1731
}
1722
1732
1733
+ // This matches either of these V8 formats.
1734
+ // at name (filename:0:0)
1735
+ // at filename:0:0
1736
+ // at async filename:0:0
1723
1737
const frameRegExp =
1724
- / ^ { 3 } at (?:(.+) \(([^\)]+):(\d+):(\d+)\)|([^\)]+):(\d+):(\d+))$/;
1738
+ / ^ { 3 } at (?:(.+) \(([^\)]+):(\d+):(\d+)\)|(?:async )?( [^\)]+):(\d+):(\d+))$/;
1725
1739
1726
- function buildFakeCallStack< T > (stack: string, innerCall: () => T ) : ( ) => T {
1740
+ function buildFakeCallStack< T > (
1741
+ response: Response,
1742
+ stack: string,
1743
+ innerCall: () => T ,
1744
+ ) : ( ) => T {
1727
1745
const frames = stack . split ( '\n' ) ;
1728
1746
let callStack = innerCall ;
1729
1747
for ( let i = 0 ; i < frames . length ; i ++ ) {
@@ -1739,7 +1757,13 @@ function buildFakeCallStack<T>(stack: string, innerCall: () => T): () => T {
1739
1757
const filename = parsed [ 2 ] || parsed [ 5 ] || '' ;
1740
1758
const line = + ( parsed [ 3 ] || parsed [ 6 ] ) ;
1741
1759
const col = + ( parsed [ 4 ] || parsed [ 7 ] ) ;
1742
- fn = createFakeFunction ( name , filename , line , col ) ;
1760
+ const sourceMap = response . _debugFindSourceMapURL
1761
+ ? response . _debugFindSourceMapURL ( filename )
1762
+ : null ;
1763
+ fn = createFakeFunction ( name , filename , sourceMap , line , col ) ;
1764
+ // TODO: This cache should technically live on the response since the _debugFindSourceMapURL
1765
+ // function is an input and can vary by response.
1766
+ fakeFunctionCache . set ( frame , fn ) ;
1743
1767
}
1744
1768
callStack = fn . bind ( null , callStack ) ;
1745
1769
}
@@ -1770,7 +1794,7 @@ function initializeFakeTask(
1770
1794
console,
1771
1795
getServerComponentTaskName(componentInfo),
1772
1796
);
1773
- const callStack = buildFakeCallStack(stack, createTaskFn);
1797
+ const callStack = buildFakeCallStack(response, stack, createTaskFn);
1774
1798
1775
1799
if (ownerTask === null) {
1776
1800
const rootTask = response . _debugRootTask ;
@@ -1832,6 +1856,7 @@ function resolveConsoleEntry(
1832
1856
return ;
1833
1857
}
1834
1858
const callStack = buildFakeCallStack(
1859
+ response,
1835
1860
stackTrace,
1836
1861
printToConsole.bind(null, methodName, args, env),
1837
1862
);
0 commit comments