@@ -239,6 +239,8 @@ Chunk.prototype.then = function <T>(
239239 }
240240} ;
241241
242+ export type FindSourceMapURLCallback = ( fileName : string ) => null | string ;
243+
242244export type Response = {
243245 _bundlerConfig : SSRModuleMap ,
244246 _moduleLoading : ModuleLoading ,
@@ -255,6 +257,7 @@ export type Response = {
255257 _buffer : Array < Uint8Array > , // chunks received so far as part of this row
256258 _tempRefs : void | TemporaryReferenceSet , // the set temporary references can be resolved from
257259 _debugRootTask ?: null | ConsoleTask , // DEV-only
260+ _debugFindSourceMapURL ?: void | FindSourceMapURLCallback , // DEV-only
258261} ;
259262
260263function readChunk < T > (chunk: SomeChunk< T > ): T {
@@ -696,7 +699,7 @@ function createElement(
696699 console ,
697700 getTaskName ( type ) ,
698701 ) ;
699- const callStack = buildFakeCallStack ( stack , createTaskFn ) ;
702+ const callStack = buildFakeCallStack ( response , stack , createTaskFn ) ;
700703 // This owner should ideally have already been initialized to avoid getting
701704 // user stack frames on the stack.
702705 const ownerTask =
@@ -1140,6 +1143,7 @@ export function createResponse(
11401143 encodeFormAction : void | EncodeFormActionCallback ,
11411144 nonce : void | string ,
11421145 temporaryReferences : void | TemporaryReferenceSet ,
1146+ findSourceMapURL : void | FindSourceMapURLCallback ,
11431147) : Response {
11441148 const chunks : Map < number , SomeChunk < any >> = new Map ( ) ;
11451149 const response : Response = {
@@ -1166,6 +1170,9 @@ export function createResponse(
11661170 // TODO: Make this string configurable.
11671171 response. _debugRootTask = ( console : any ) . createTask ( '"use server"' ) ;
11681172 }
1173+ if ( __DEV__ ) {
1174+ response . _debugFindSourceMapURL = findSourceMapURL ;
1175+ }
11691176 // Don't inline this call because it causes closure to outline the call above.
11701177 response . _fromJSON = createFromJSONCallback ( response ) ;
11711178 return response ;
@@ -1673,6 +1680,7 @@ const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
16731680function createFakeFunction < T > (
16741681 name: string,
16751682 filename: string,
1683+ sourceMap: null | string,
16761684 line: number,
16771685 col: number,
16781686): FakeFunction< T > {
@@ -1697,7 +1705,9 @@ function createFakeFunction<T>(
16971705 '_()\n' ;
16981706 }
16991707
1700- if (filename) {
1708+ if (sourceMap) {
1709+ code += '//# sourceMappingURL=' + sourceMap ;
1710+ } else if (filename) {
17011711 code += '//# sourceURL=' + filename ;
17021712 }
17031713
@@ -1720,10 +1730,18 @@ function createFakeFunction<T>(
17201730 return fn;
17211731}
17221732
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
17231737const frameRegExp =
1724- / ^ { 3 } at (?:(.+) \(([^\)]+):(\d+):(\d+)\)|([^\)]+):(\d+):(\d+))$/;
1738+ / ^ { 3 } at (?:(.+) \(([^\)]+):(\d+):(\d+)\)|(?:async )?( [^\)]+):(\d+):(\d+))$/;
17251739
1726- function buildFakeCallStack< T > (stack: string, innerCall: () => T ) : ( ) => T {
1740+ function buildFakeCallStack< T > (
1741+ response: Response,
1742+ stack: string,
1743+ innerCall: () => T ,
1744+ ) : ( ) => T {
17271745 const frames = stack . split ( '\n' ) ;
17281746 let callStack = innerCall ;
17291747 for ( let i = 0 ; i < frames . length ; i ++ ) {
@@ -1739,7 +1757,13 @@ function buildFakeCallStack<T>(stack: string, innerCall: () => T): () => T {
17391757 const filename = parsed [ 2 ] || parsed [ 5 ] || '' ;
17401758 const line = + ( parsed [ 3 ] || parsed [ 6 ] ) ;
17411759 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 ) ;
17431767 }
17441768 callStack = fn . bind ( null , callStack ) ;
17451769 }
@@ -1770,7 +1794,7 @@ function initializeFakeTask(
17701794 console,
17711795 getServerComponentTaskName(componentInfo),
17721796 );
1773- const callStack = buildFakeCallStack(stack, createTaskFn);
1797+ const callStack = buildFakeCallStack(response, stack, createTaskFn);
17741798
17751799 if (ownerTask === null) {
17761800 const rootTask = response . _debugRootTask ;
@@ -1832,6 +1856,7 @@ function resolveConsoleEntry(
18321856 return ;
18331857 }
18341858 const callStack = buildFakeCallStack(
1859+ response,
18351860 stackTrace,
18361861 printToConsole.bind(null, methodName, args, env),
18371862 );
0 commit comments