forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.ts
More file actions
45 lines (38 loc) · 1.34 KB
/
Copy pathmodule.ts
File metadata and controls
45 lines (38 loc) · 1.34 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
import { basename, dirname } from '@sentry/utils';
/** normalizes Windows paths */
function normalizePath(path: string): string {
return path
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
.replace(/\\/g, '/'); // replace all `\` instances with `/`
}
/** Gets the module from a filename */
export function getModule(filename: string | undefined): string | undefined {
if (!filename) {
return;
}
const normalizedFilename = normalizePath(filename);
// We could use optional chaining here but webpack does like that mixed with require
const base = normalizePath(
`${(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()}/`,
);
// It's specifically a module
const file = basename(normalizedFilename, '.js');
const path = dirname(normalizedFilename);
let n = path.lastIndexOf('/node_modules/');
if (n > -1) {
// /node_modules/ is 14 chars
return `${path.substr(n + 14).replace(/\//g, '.')}:${file}`;
}
// Let's see if it's a part of the main module
// To be a part of main module, it has to share the same base
n = `${path}/`.lastIndexOf(base, 0);
if (n === 0) {
let moduleName = path.substr(base.length).replace(/\//g, '.');
if (moduleName) {
moduleName += ':';
}
moduleName += file;
return moduleName;
}
return file;
}