forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve-caller.ts
More file actions
53 lines (47 loc) · 1.24 KB
/
resolve-caller.ts
File metadata and controls
53 lines (47 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function stripURLAndLine(s: string) {
return s.replace(/.*\.(\w{2}|\w{3})\//, '').replace(/:.*/, '');
}
/**
* Gets the file that called the resolve, resolve calls this. It's literally
*
* ```js
* module.exports = function () {
* // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
* var origPrepareStackTrace = Error.prepareStackTrace;
* Error.prepareStackTrace = function (_, stack) { return stack; };
* var stack = (new Error()).stack;
* Error.prepareStackTrace = origPrepareStackTrace;
* return stack[2].getFileName();
* };
* ```
*
* from https://unpkg.com/resolve@1.10.0/lib/caller.js
*
* but it's made to work in Firefox too (doesn't have prepareStackTrace sadly)
*/
export function getCaller() {
const stack = new Error().stack;
if (stack) {
const stacks = stack.split('\n');
return '/' + stripURLAndLine(stacks[3]);
} else {
return '';
}
}
export function getCallSites() {
const stack = new Error().stack;
if (stack) {
const stacks = stack.split('\n');
stacks.shift();
return stacks.map(s => {
const filename = stripURLAndLine(s);
return {
getFileName() {
return filename;
},
};
});
} else {
return [];
}
}