|
1 | | -import { posix, sep } from 'path'; |
2 | | - |
3 | | -const isWindowsPlatform = sep === '\\'; |
| 1 | +import { basename, dirname } from '@sentry/utils'; |
4 | 2 |
|
5 | 3 | /** normalizes Windows paths */ |
6 | | -function normalizeWindowsPath(path: string): string { |
| 4 | +function normalizePath(path: string): string { |
7 | 5 | return path |
8 | 6 | .replace(/^[A-Z]:/, '') // remove Windows-style prefix |
9 | 7 | .replace(/\\/g, '/'); // replace all `\` instances with `/` |
10 | 8 | } |
11 | 9 |
|
12 | 10 | /** Gets the module from a filename */ |
13 | | -export function getModule( |
14 | | - filename: string | undefined, |
15 | | - normalizeWindowsPathSeparator: boolean = isWindowsPlatform, |
16 | | -): string | undefined { |
| 11 | +export function getModule(filename: string | undefined): string | undefined { |
17 | 12 | if (!filename) { |
18 | 13 | return; |
19 | 14 | } |
20 | 15 |
|
21 | | - const normalizedFilename = normalizeWindowsPathSeparator ? normalizeWindowsPath(filename) : filename; |
22 | | - |
23 | | - // eslint-disable-next-line prefer-const |
24 | | - let { root, dir, base: basename, ext } = posix.parse(normalizedFilename); |
| 16 | + const normalizedFilename = normalizePath(filename); |
25 | 17 |
|
26 | | - const base = (require && require.main && require.main.filename && dir) || global.process.cwd(); |
27 | | - |
28 | | - const normalizedBase = `${base}/`; |
| 18 | + // We could use optional chaining here but webpack does like that mixed with require |
| 19 | + const base = normalizePath( |
| 20 | + `${(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()}/`, |
| 21 | + ); |
29 | 22 |
|
30 | 23 | // It's specifically a module |
31 | | - let file = basename; |
32 | | - |
33 | | - if (ext === '.js') { |
34 | | - file = file.slice(0, file.length - '.js'.length); |
35 | | - } |
36 | | - |
37 | | - if (!root && !dir) { |
38 | | - // No dirname whatsoever |
39 | | - dir = '.'; |
40 | | - } |
| 24 | + const file = basename(normalizedFilename, '.js'); |
41 | 25 |
|
42 | | - let n = dir.lastIndexOf('/node_modules/'); |
| 26 | + const path = dirname(normalizedFilename); |
| 27 | + let n = path.lastIndexOf('/node_modules/'); |
43 | 28 | if (n > -1) { |
44 | 29 | // /node_modules/ is 14 chars |
45 | | - return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`; |
| 30 | + return `${path.slice(n + 14).replace(/\//g, '.')}:${file}`; |
46 | 31 | } |
47 | 32 | // Let's see if it's a part of the main module |
48 | 33 | // To be a part of main module, it has to share the same base |
49 | | - n = `${dir}/`.lastIndexOf(normalizedBase, 0); |
| 34 | + n = `${path}/`.lastIndexOf(base, 0); |
50 | 35 |
|
51 | 36 | if (n === 0) { |
52 | | - let moduleName = dir.slice(normalizedBase.length).replace(/\//g, '.'); |
| 37 | + let moduleName = path.slice(base.length).replace(/\//g, '.'); |
53 | 38 | if (moduleName) { |
54 | 39 | moduleName += ':'; |
55 | 40 | } |
|
0 commit comments