@@ -8,62 +8,51 @@ function normalizeWindowsPath(path: string): string {
8
8
. replace ( / \\ / g, '/' ) ; // replace all `\` instances with `/`
9
9
}
10
10
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 ] ) ,
28
14
isWindows : boolean = sep === '\\' ,
29
- ) : string | undefined {
30
- if ( ! filename ) {
31
- return ;
32
- }
33
-
15
+ ) : ( filename : string | undefined ) => string | undefined {
34
16
const normalizedBase = isWindows ? normalizeWindowsPath ( basePath ) : basePath ;
35
- const normalizedFilename = isWindows ? normalizeWindowsPath ( filename ) : filename ;
36
17
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
+ }
39
22
40
- if ( ext === '.js' || ext === '.mjs' || ext === '.cjs' ) {
41
- file = file . slice ( 0 , ext . length * - 1 ) ;
42
- }
23
+ const normalizedFilename = isWindows ? normalizeWindowsPath ( filename ) : filename ;
43
24
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 ) ;
48
27
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
+ }
53
31
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
+ }
59
36
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 } ` ;
62
40
}
63
- moduleName += file ;
64
41
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
+ }
67
55
68
- return file ;
56
+ return file ;
57
+ } ;
69
58
}
0 commit comments