Skip to content

Commit 7051e1a

Browse files
committed
Added better support for Next13 and Monorepos
1 parent 456fe47 commit 7051e1a

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-root",
3-
"version": "1.3.8",
3+
"version": "1.3.9",
44
"description": "Determines absolute path to project root and main file",
55
"files": [
66
"lib/**/*"

src/index.ts

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,78 @@ try {
99
throw new Error();
1010
} catch (ex: any) {
1111
const lines = ex.stack.toString().split('\n');
12+
1213
for (let i = lines.length - 1; i >= 0; i--) {
1314
const line = lines[i].trim();
14-
let start = line.indexOf('(');
15-
if (start < 0) continue;
16-
start++;
17-
let end = line.indexOf(')', start);
18-
if (!end) continue;
19-
let path = line.substring(start, end).trim().replace(/\\/g, '/'); // '\' -> '/'
20-
if (
21-
path.length === 0 ||
22-
path.startsWith('internal/modules/') ||
23-
path.startsWith('node:') ||
24-
path.lastIndexOf('/.next/server/') >= 0
25-
)
26-
continue;
27-
28-
// remove line and column numbers at end
29-
for (let j = 0; j < 2; j++) {
30-
end = path.lastIndexOf(':');
31-
if (end < 0) break;
32-
path = path.substring(0, end);
33-
}
34-
35-
if (path === _MAIN) continue;
36-
37-
_MAIN = path;
38-
end = path.lastIndexOf('/node_modules/');
39-
end = end < 0 ? path.lastIndexOf('/') : end;
40-
_ROOT = end > 0 ? path.substring(0, end + 1) : path;
41-
_ROOT = _ROOT.endsWith('/') ? _ROOT.substring(0, _ROOT.length - 1) : _ROOT;
42-
43-
// strip common build directories
44-
for (const buildDir of COMMON_BUILD_DIRECTORIES) {
45-
if (_ROOT.endsWith(buildDir)) {
46-
_ROOT = _ROOT.substring(0, _ROOT.length - buildDir.length);
47-
break;
15+
let start = 0;
16+
let found = false;
17+
18+
do {
19+
start = line.indexOf('(', start);
20+
if (start < 0) break;
21+
start++;
22+
let end = start;
23+
24+
// find closing bracket
25+
let countOpening = 1;
26+
do {
27+
const idx1 = line.indexOf('(', end);
28+
const idx2 = line.indexOf(')', end);
29+
if(idx1 < 0 && idx2 < 0){ countOpening = -1; break; }
30+
31+
if(idx1 >= 0 && idx1 < idx2){
32+
// another opening bracket found
33+
countOpening++;
34+
end = idx1 + 1;
35+
} else { // closing bracket found
36+
countOpening--;
37+
end = idx2 + (countOpening === 0 ? 0 : 1);
38+
}
39+
} while(countOpening > 0);
40+
if(countOpening < 0) break;
41+
42+
let path = line.substring(start, end).trim().replace(/\\/g, '/'); // '\' -> '/'
43+
start = end + 1;
44+
if (
45+
path.length === 0 ||
46+
path === 'rsc' ||
47+
path.startsWith('internal/modules/') ||
48+
path.startsWith('node:') ||
49+
path.startsWith('webpack-internal:') ||
50+
path.lastIndexOf('/webpack-runtime.') >= 0 ||
51+
path.lastIndexOf('/next/dist') >= 0 ||
52+
path.startsWith('index ')
53+
)
54+
continue;
55+
56+
// remove line and column numbers at end
57+
for (let j = 0; j < 2; j++) {
58+
end = path.lastIndexOf(':');
59+
if (end < 0) break;
60+
path = path.substring(0, end);
4861
}
49-
}
5062

51-
break;
63+
if (path === _MAIN && path.lastIndexOf('/.next/server') < 0) break;
64+
65+
_MAIN = path;
66+
end = path.lastIndexOf('/node_modules/');
67+
end = end < 0 ? path.lastIndexOf('/.next/server') : end;
68+
end = end < 0 ? path.lastIndexOf('/') : end;
69+
_ROOT = end > 0 ? path.substring(0, end + 1) : path;
70+
_ROOT = _ROOT.endsWith('/') ? _ROOT.substring(0, _ROOT.length - 1) : _ROOT;
71+
72+
// strip common build directories
73+
for (const buildDir of COMMON_BUILD_DIRECTORIES) {
74+
if (_ROOT.endsWith(buildDir)) {
75+
_ROOT = _ROOT.substring(0, _ROOT.length - buildDir.length);
76+
break;
77+
}
78+
}
79+
80+
found = true;
81+
} while(!found);
82+
83+
if(found) break;
5284
}
5385
}
5486
Error.stackTraceLimit = originalStackTraceLimit;

0 commit comments

Comments
 (0)