|
1 | 1 | let _MAIN = __filename.replace(/\\/g, '/'); // '\' -> '/' |
2 | 2 | let _ROOT = __dirname.replace(/\\/g, '/'); // '\' -> '/' |
3 | 3 |
|
4 | | -const COMMON_BUILD_DIRECTORIES = ['/bin', '/.bin', '/build', '/out', '/target']; |
| 4 | +const COMMON_BUILD_DIRECTORIES = ['/bin', '/.bin', '/build', '/dist', '/lib', '/out', '/target']; |
5 | 5 |
|
6 | 6 | const originalStackTraceLimit = Error.stackTraceLimit; |
7 | 7 | Error.stackTraceLimit = Infinity; |
8 | 8 | try { |
9 | 9 | throw new Error(); |
10 | 10 | } catch (ex: any) { |
11 | 11 | const lines = ex.stack.toString().split('\n'); |
| 12 | + let found = false; |
12 | 13 |
|
13 | | - for (let i = lines.length - 1; i >= 0; i--) { |
| 14 | + for (let i = lines.length - 1; i >= 0 && !found; i--) { |
14 | 15 | const line = lines[i].trim(); |
15 | 16 | let start = 0; |
16 | | - let found = false; |
17 | | - |
18 | 17 | do { |
19 | 18 | start = line.indexOf('(', start); |
20 | 19 | if (start < 0) break; |
21 | 20 | start++; |
22 | 21 | let end = start; |
23 | 22 |
|
24 | | - // find closing bracket |
| 23 | + // find closing bracket |
25 | 24 | let countOpening = 1; |
26 | 25 | do { |
27 | 26 | const idx1 = line.indexOf('(', end); |
28 | 27 | const idx2 = line.indexOf(')', end); |
29 | | - if(idx1 < 0 && idx2 < 0){ countOpening = -1; break; } |
| 28 | + if (idx1 < 0 && idx2 < 0) { |
| 29 | + countOpening = -1; |
| 30 | + break; |
| 31 | + } |
30 | 32 |
|
31 | | - if(idx1 >= 0 && idx1 < idx2){ |
| 33 | + if (idx1 >= 0 && idx1 < idx2) { |
32 | 34 | // another opening bracket found |
33 | 35 | countOpening++; |
34 | 36 | end = idx1 + 1; |
35 | | - } else { // closing bracket found |
| 37 | + } else { |
| 38 | + // closing bracket found |
36 | 39 | countOpening--; |
37 | 40 | end = idx2 + (countOpening === 0 ? 0 : 1); |
38 | 41 | } |
39 | | - } while(countOpening > 0); |
40 | | - if(countOpening < 0) break; |
| 42 | + } while (countOpening > 0); |
| 43 | + if (countOpening < 0) break; |
41 | 44 |
|
42 | 45 | let path = line.substring(start, end).trim().replace(/\\/g, '/'); // '\' -> '/' |
43 | 46 | start = end + 1; |
|
47 | 50 | path.startsWith('node:') || |
48 | 51 | path.startsWith('webpack-internal:') || |
49 | 52 | path.lastIndexOf('/webpack-runtime.') >= 0 || |
50 | | - path.lastIndexOf('/next/dist') >= 0 || |
| 53 | + path.lastIndexOf('/next/dist') >= 0 || |
51 | 54 | path.startsWith('index ') |
52 | 55 | ) |
53 | 56 | continue; |
|
59 | 62 | path = path.substring(0, end); |
60 | 63 | } |
61 | 64 |
|
62 | | - if (path === _MAIN && path.lastIndexOf('/.next/server') < 0) break; |
| 65 | + // if (path === _MAIN && path.lastIndexOf('/.next/server') < 0) break; |
| 66 | + |
| 67 | + // next middleware (special case) |
| 68 | + end = path.lastIndexOf('/.next/dev/server/'); |
| 69 | + if (end >= 0) { |
| 70 | + const fileName = _MAIN.substring(_ROOT.length + 1); |
| 71 | + _ROOT = path.substring(0, end); |
| 72 | + _MAIN = _ROOT + (!_ROOT.endsWith('/') ? '/' : '') + fileName.substring(fileName.startsWith('/') ? 1 : 0); |
| 73 | + found = true; |
| 74 | + break; |
| 75 | + } |
63 | 76 |
|
64 | 77 | _MAIN = path; |
65 | 78 | end = path.lastIndexOf('/node_modules/'); |
|
77 | 90 | } |
78 | 91 |
|
79 | 92 | found = true; |
80 | | - } while(!found); |
81 | | - |
82 | | - if(found) break; |
| 93 | + } while (!found); |
83 | 94 | } |
84 | 95 | } |
85 | 96 | Error.stackTraceLimit = originalStackTraceLimit; |
|
0 commit comments