@@ -145,7 +145,10 @@ async function parseImportsForFile(file: string, rootPath: string) {
145145 * Parses a set of functions and returns a list of specifiers that correspond
146146 * to npm modules.
147147 */
148- const getNPMSpecifiers = async ( { basePath, functions, importMap, environment, rootPath } : GetNPMSpecifiersOptions ) => {
148+ const getNPMSpecifiers = async (
149+ { basePath, functions, importMap, environment, rootPath } : GetNPMSpecifiersOptions ,
150+ alreadySeenPaths = new Set < string > ( ) ,
151+ ) => {
149152 const baseURL = pathToFileURL ( basePath )
150153 const npmSpecifiers : { specifier : string ; types ?: string } [ ] = [ ]
151154 for ( const func of functions ) {
@@ -157,15 +160,29 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, environment, r
157160 const specifier = i . moduleSpecifier . isConstant ? i . moduleSpecifier . value ! : i . moduleSpecifier . code
158161 switch ( i . moduleSpecifier . type ) {
159162 case 'absolute' : {
163+ if ( alreadySeenPaths . has ( specifier ) ) {
164+ break
165+ }
166+ alreadySeenPaths . add ( specifier )
160167 npmSpecifiers . push (
161- ...( await getNPMSpecifiers ( { basePath, functions : [ specifier ] , importMap, environment, rootPath } ) ) ,
168+ ...( await getNPMSpecifiers (
169+ { basePath, functions : [ specifier ] , importMap, environment, rootPath } ,
170+ alreadySeenPaths ,
171+ ) ) ,
162172 )
163173 break
164174 }
165175 case 'relative' : {
166176 const filePath = path . join ( path . dirname ( func ) , specifier )
177+ if ( alreadySeenPaths . has ( filePath ) ) {
178+ break
179+ }
180+ alreadySeenPaths . add ( filePath )
167181 npmSpecifiers . push (
168- ...( await getNPMSpecifiers ( { basePath, functions : [ filePath ] , importMap, environment, rootPath } ) ) ,
182+ ...( await getNPMSpecifiers (
183+ { basePath, functions : [ filePath ] , importMap, environment, rootPath } ,
184+ alreadySeenPaths ,
185+ ) ) ,
169186 )
170187 break
171188 }
@@ -180,8 +197,15 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, environment, r
180197 if ( matched ) {
181198 if ( resolvedImport . protocol === 'file:' ) {
182199 const newSpecifier = fileURLToPath ( resolvedImport ) . replace ( / \\ / g, '/' )
200+ if ( alreadySeenPaths . has ( newSpecifier ) ) {
201+ break
202+ }
203+ alreadySeenPaths . add ( newSpecifier )
183204 npmSpecifiers . push (
184- ...( await getNPMSpecifiers ( { basePath, functions : [ newSpecifier ] , importMap, environment, rootPath } ) ) ,
205+ ...( await getNPMSpecifiers (
206+ { basePath, functions : [ newSpecifier ] , importMap, environment, rootPath } ,
207+ alreadySeenPaths ,
208+ ) ) ,
185209 )
186210 }
187211 } else if ( ! resolvedImport ?. protocol ?. startsWith ( 'http' ) ) {
0 commit comments