@@ -5,18 +5,13 @@ import { init, parse } from 'es-module-lexer';
5
5
6
6
import {
7
7
isBareModuleSpecifier ,
8
- splitPath ,
9
- traverseUp
10
8
} from './utils.js' ;
11
9
12
- const require = createRequire ( import . meta. url ) ;
13
-
14
10
/**
15
11
*
16
12
* @param {string[] } paths
17
13
* @param {{
18
14
* nodeModulesDepth?: number,
19
- * basePath?: string,
20
15
* }} options
21
16
* @returns {Promise<string[]> }
22
17
*/
@@ -25,25 +20,21 @@ export async function findDependencies(paths, options = {}) {
25
20
const dependencies = new Set ( ) ;
26
21
27
22
const nodeModulesDepth = options ?. nodeModulesDepth ?? 3 ;
28
- const basePath = options ?. basePath ?? process . cwd ( ) ;
29
23
30
24
/** Init es-module-lexer wasm */
31
25
await init ;
32
26
33
- paths . forEach ( path => {
34
- const source = fs . readFileSync ( path ) . toString ( ) ;
27
+ paths . forEach ( filePath => {
28
+ const source = fs . readFileSync ( filePath ) . toString ( ) ;
35
29
const [ imports ] = parse ( source ) ;
36
30
31
+ const pathRequire = createRequire ( path . resolve ( filePath ) ) ;
32
+
37
33
imports ?. forEach ( i => {
38
34
/** Skip built-in modules like fs, path, etc */
39
35
if ( builtinModules . includes ( i . n ) ) return ;
40
36
try {
41
- const pathToDependency = require . resolve ( i . n , { paths : [
42
- /** Current project's node_modules */
43
- basePath ,
44
- /** Monorepo, look upwards in filetree n times */
45
- ...traverseUp ( nodeModulesDepth )
46
- ] } ) ;
37
+ const pathToDependency = pathRequire . resolve ( i . n ) ;
47
38
48
39
importsToScan . add ( pathToDependency ) ;
49
40
dependencies . add ( pathToDependency ) ;
@@ -60,24 +51,18 @@ export async function findDependencies(paths, options = {}) {
60
51
const source = fs . readFileSync ( dep ) . toString ( ) ;
61
52
const [ imports ] = parse ( source ) ;
62
53
54
+ const depRequire = createRequire ( dep ) ;
55
+
63
56
imports ?. forEach ( i => {
64
57
/** Skip built-in modules like fs, path, etc */
65
58
if ( builtinModules . includes ( i . n ) ) return ;
66
- const { packageRoot } = splitPath ( dep ) ;
67
59
const fileToFind = isBareModuleSpecifier ( i . n ) ? i . n : path . join ( path . dirname ( dep ) , i . n ) ;
68
60
try {
69
61
/**
70
62
* First check in the dependencies' node_modules, then in the project's node_modules,
71
63
* then up, and up, and up
72
64
*/
73
- const pathToDependency = require . resolve ( fileToFind , { paths : [
74
- /** Nested node_modules */
75
- packageRoot ,
76
- /** Current project's node_modules */
77
- basePath ,
78
- /** Monorepo, look upwards in filetree n times */
79
- ...traverseUp ( nodeModulesDepth )
80
- ] } ) ;
65
+ const pathToDependency = depRequire . resolve ( fileToFind ) ;
81
66
/**
82
67
* Don't add dependencies we've already scanned, also avoids circular dependencies
83
68
* and multiple modules importing from the same module
@@ -94,4 +79,4 @@ export async function findDependencies(paths, options = {}) {
94
79
}
95
80
96
81
return [ ...dependencies ] ;
97
- }
82
+ }
0 commit comments