@@ -45,63 +45,69 @@ async function handlePrerenderError(err, env, stack, entry) {
4545 let errorMessage = err . toString ( ) ;
4646 let isReferenceError = errorMessage . startsWith ( 'ReferenceError' ) ;
4747 let methodName = stack . getMethodName ( ) ;
48- let sourceMapContent , position , sourcePath , sourceLines , sourceCodeHighlight ;
4948
50- try {
51- sourceMapContent = JSON . parse ( readFileSync ( `${ entry } .map` , 'utf-8' ) ) ;
52- } catch ( err ) {
53- process . stderr . write ( red ( `Unable to read sourcemap: ${ entry } .map\n` ) ) ;
54- }
49+ process . stderr . write ( '\n' ) ;
50+ process . stderr . write ( red ( `\n${ errorMessage } \n` ) ) ;
5551
56- if ( sourceMapContent ) {
57- await SourceMapConsumer . with ( sourceMapContent , null , consumer => {
58- position = consumer . originalPositionFor ( {
59- line : stack . getLineNumber ( ) ,
60- column : stack . getColumnNumber ( ) ,
52+ // If a methodName exists, it's likely user code
53+ if ( methodName ) {
54+ let sourceMapContent ,
55+ position ,
56+ sourcePath ,
57+ sourceLines ,
58+ sourceCodeHighlight ;
59+ try {
60+ sourceMapContent = JSON . parse ( readFileSync ( `${ entry } .map` , 'utf-8' ) ) ;
61+ } catch ( err ) {
62+ process . stderr . write ( red ( `\n\nUnable to read sourcemap: ${ entry } .map\n` ) ) ;
63+ }
64+
65+ if ( sourceMapContent ) {
66+ await SourceMapConsumer . with ( sourceMapContent , null , consumer => {
67+ position = consumer . originalPositionFor ( {
68+ line : stack . getLineNumber ( ) ,
69+ column : stack . getColumnNumber ( ) ,
70+ } ) ;
6171 } ) ;
62- } ) ;
63-
64- if ( position . source ) {
65- position . source = position . source
66- . replace ( 'webpack://' , '.' )
67- . replace ( / ^ .* ~ \/ ( (?: @ [ ^ / ] + \/ ) ? [ ^ / ] + ) / , ( s , name ) =>
68- require
69- . resolve ( name )
70- . replace ( / ^ ( .* ?\/ n o d e _ m o d u l e s \/ ( @ [ ^ / ] + \/ ) ? [ ^ / ] + ) ( \/ .* ) $ / , '$1' )
71- ) ;
72-
73- sourcePath = resolve ( env . src , position . source ) ;
74- sourceLines ;
75- try {
76- sourceLines = readFileSync ( sourcePath , 'utf-8' ) . split ( '\n' ) ;
77- } catch ( err ) {
72+
73+ if ( position . source ) {
74+ position . source = position . source
75+ . replace ( 'webpack://' , '.' )
76+ . replace ( / ^ .* ~ \/ ( (?: @ [ ^ / ] + \/ ) ? [ ^ / ] + ) / , ( s , name ) =>
77+ require
78+ . resolve ( name )
79+ . replace ( / ^ ( .* ?\/ n o d e _ m o d u l e s \/ ( @ [ ^ / ] + \/ ) ? [ ^ / ] + ) ( \/ .* ) $ / , '$1' )
80+ ) ;
81+
82+ sourcePath = resolve ( env . src , position . source ) ;
83+ sourceLines ;
7884 try {
79- sourceLines = readFileSync (
80- require . resolve ( position . source ) ,
81- 'utf-8'
82- ) . split ( '\n' ) ;
85+ sourceLines = readFileSync ( sourcePath , 'utf-8' ) . split ( '\n' ) ;
8386 } catch ( err ) {
84- process . stderr . write ( red ( `Unable to read file: ${ sourcePath } \n` ) ) ;
87+ try {
88+ sourceLines = readFileSync (
89+ require . resolve ( position . source ) ,
90+ 'utf-8'
91+ ) . split ( '\n' ) ;
92+ } catch ( err ) {
93+ process . stderr . write (
94+ red ( `\n\nUnable to read file: ${ sourcePath } \n` )
95+ ) ;
96+ }
8597 }
86- // process.stderr.write(red(`Unable to read file: ${sourcePath}\n`)) ;
98+ sourceCodeHighlight = '' ;
8799 }
88- sourceCodeHighlight = '' ;
89- }
90100
91- if ( sourceLines ) {
92- for ( var i = - 4 ; i <= 4 ; i ++ ) {
93- let color = i === 0 ? red : yellow ;
94- let line = position . line + i ;
95- let sourceLine = sourceLines [ line - 1 ] ;
96- sourceCodeHighlight += sourceLine ? `${ color ( sourceLine ) } \n` : '' ;
101+ if ( sourceLines ) {
102+ for ( var i = - 4 ; i <= 4 ; i ++ ) {
103+ let color = i === 0 ? red : yellow ;
104+ let line = position . line + i ;
105+ let sourceLine = sourceLines [ line - 1 ] ;
106+ sourceCodeHighlight += sourceLine ? `${ color ( sourceLine ) } \n` : '' ;
107+ }
97108 }
98109 }
99- }
100110
101- process . stderr . write ( '\n' ) ;
102- process . stderr . write ( red ( `${ errorMessage } \n` ) ) ;
103- // check if we have methodName (ie, the error originated in user code)
104- if ( methodName ) {
105111 process . stderr . write ( `method: ${ methodName } \n` ) ;
106112 if ( sourceMapContent & sourceCodeHighlight ) {
107113 process . stderr . write (
@@ -119,11 +125,11 @@ async function handlePrerenderError(err, env, stack, entry) {
119125 } else {
120126 process . stderr . write (
121127 yellow (
122- 'Cannot determine error position. This most likely means it originated in node_modules.'
128+ 'Cannot determine error position. This most likely means it originated in node_modules.\n\n '
123129 )
124130 ) ;
125- process . stderr . write ( '\n\n' ) ;
126131 }
132+
127133 process . stderr . write (
128134 `This ${
129135 isReferenceError ? 'is most likely' : 'could be'
0 commit comments