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