@@ -8,62 +8,51 @@ function normalizeWindowsPath(path: string): string {
88 . replace ( / \\ / g, '/' ) ; // replace all `\` instances with `/`
99}
1010
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-
24- /** Gets the module from a filename */
25- export function getModuleFromFilename (
26- filename : string | undefined ,
27- basePath : string = getBasePath ( ) ,
11+ /** Creates a function that gets the module name from a filename */
12+ export function createGetModuleFromFilename (
13+ basePath : string = dirname ( process . argv [ 1 ] ) ,
2814 isWindows : boolean = sep === '\\' ,
29- ) : string | undefined {
30- if ( ! filename ) {
31- return ;
32- }
33-
15+ ) : ( filename : string | undefined ) => string | undefined {
3416 const normalizedBase = isWindows ? normalizeWindowsPath ( basePath ) : basePath ;
35- const normalizedFilename = isWindows ? normalizeWindowsPath ( filename ) : filename ;
3617
37- // eslint-disable-next-line prefer-const
38- let { dir, base : file , ext } = posix . parse ( normalizedFilename ) ;
18+ return ( filename : string | undefined ) => {
19+ if ( ! filename ) {
20+ return ;
21+ }
3922
40- if ( ext === '.js' || ext === '.mjs' || ext === '.cjs' ) {
41- file = file . slice ( 0 , ext . length * - 1 ) ;
42- }
23+ const normalizedFilename = isWindows ? normalizeWindowsPath ( filename ) : filename ;
4324
44- if ( ! dir ) {
45- // No dirname whatsoever
46- dir = '.' ;
47- }
25+ // eslint-disable-next-line prefer-const
26+ let { dir, base : file , ext } = posix . parse ( normalizedFilename ) ;
4827
49- let n = dir . lastIndexOf ( '/node_modules' ) ;
50- if ( n > - 1 ) {
51- return `${ dir . slice ( n + 14 ) . replace ( / \/ / g, '.' ) } :${ file } ` ;
52- }
28+ if ( ext === '.js' || ext === '.mjs' || ext === '.cjs' ) {
29+ file = file . slice ( 0 , ext . length * - 1 ) ;
30+ }
5331
54- // Let's see if it's a part of the main module
55- // To be a part of main module, it has to share the same base
56- n = `${ dir } /` . lastIndexOf ( normalizedBase , 0 ) ;
57- if ( n === 0 ) {
58- let moduleName = dir . slice ( normalizedBase . length ) . replace ( / \/ / g, '.' ) ;
32+ if ( ! dir ) {
33+ // No dirname whatsoever
34+ dir = '.' ;
35+ }
5936
60- if ( moduleName ) {
61- moduleName += ':' ;
37+ let n = dir . lastIndexOf ( '/node_modules' ) ;
38+ if ( n > - 1 ) {
39+ return `${ dir . slice ( n + 14 ) . replace ( / \/ / g, '.' ) } :${ file } ` ;
6240 }
63- moduleName += file ;
6441
65- return moduleName ;
66- }
42+ // Let's see if it's a part of the main module
43+ // To be a part of main module, it has to share the same base
44+ n = `${ dir } /` . lastIndexOf ( normalizedBase , 0 ) ;
45+ if ( n === 0 ) {
46+ let moduleName = dir . slice ( normalizedBase . length ) . replace ( / \/ / g, '.' ) ;
47+
48+ if ( moduleName ) {
49+ moduleName += ':' ;
50+ }
51+ moduleName += file ;
52+
53+ return moduleName ;
54+ }
6755
68- return file ;
56+ return file ;
57+ } ;
6958}
0 commit comments