11import { posix , sep } from 'path' ;
2-
3- const isWindowsPlatform = sep === '\\' ;
2+ import { dirname } from '@sentry/utils' ;
43
54/** normalizes Windows paths */
65function normalizeWindowsPath ( path : string ) : string {
@@ -9,52 +8,67 @@ function normalizeWindowsPath(path: string): string {
98 . replace ( / \\ / g, '/' ) ; // replace all `\` instances with `/`
109}
1110
11+ // We cache this so we don't have to recompute it
12+ let basePath : string | undefined ;
13+
14+ function getBasePath ( ) : string {
15+ if ( ! basePath ) {
16+ const baseDir =
17+ require && require . main && require . main . filename ? dirname ( require . main . filename ) : global . process . cwd ( ) ;
18+ basePath = `${ baseDir } /` ;
19+ }
20+
21+ return basePath ;
22+ }
23+
1224/** Gets the module from a filename */
1325export function getModuleFromFilename (
1426 filename : string | undefined ,
15- normalizeWindowsPathSeparator : boolean = isWindowsPlatform ,
27+ basePath : string = getBasePath ( ) ,
28+ isWindows : boolean = sep === '\\' ,
1629) : string | undefined {
1730 if ( ! filename ) {
1831 return ;
1932 }
2033
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 ) ;
25-
26- const base = ( require && require . main && require . main . filename && dir ) || global . process . cwd ( ) ;
34+ // If it's a node internal module, we can just return it
35+ if ( filename . startsWith ( 'node:' ) ) {
36+ return filename . replace ( / \/ / g, '.' ) ;
37+ }
2738
28- const normalizedBase = `${ base } /` ;
39+ const normalizedBase = isWindows ? normalizeWindowsPath ( basePath ) : basePath ;
40+ const normalizedFilename = isWindows ? normalizeWindowsPath ( filename ) : filename ;
2941
30- // It's specifically a module
31- let file = basename ;
42+ // eslint-disable-next-line prefer-const
43+ let { dir , base : file , ext } = posix . parse ( normalizedFilename ) ;
3244
3345 if ( ext === '.js' || ext === '.mjs' || ext === '.cjs' ) {
3446 file = file . slice ( 0 , ext . length * - 1 ) ;
3547 }
3648
37- if ( ! root && ! dir ) {
49+ if ( ! dir ) {
3850 // No dirname whatsoever
3951 dir = '.' ;
4052 }
4153
42- let n = dir . lastIndexOf ( '/node_modules/ ' ) ;
54+ let n = dir . lastIndexOf ( '/node_modules' ) ;
4355 if ( n > - 1 ) {
44- // /node_modules/ is 14 chars
4556 return `${ dir . slice ( n + 14 ) . replace ( / \/ / g, '.' ) } :${ file } ` ;
4657 }
58+
4759 // Let's see if it's a part of the main module
4860 // To be a part of main module, it has to share the same base
4961 n = `${ dir } /` . lastIndexOf ( normalizedBase , 0 ) ;
50-
5162 if ( n === 0 ) {
5263 let moduleName = dir . slice ( normalizedBase . length ) . replace ( / \/ / g, '.' ) ;
64+
5365 if ( moduleName ) {
5466 moduleName += ':' ;
5567 }
5668 moduleName += file ;
69+
5770 return moduleName ;
5871 }
72+
5973 return file ;
6074}
0 commit comments