@@ -95,14 +95,14 @@ interface PackageJson {
9595const { name, version } = createRequire ( import . meta. url ) ( '#package.json' ) as PackageJson
9696
9797// Files that mark the root of a monorepo
98- const workspaceRootFiles = new Set ( [
98+ const workspaceRootFiles = [
9999 'pnpm-workspace.yaml' , // pnpm
100100 'lerna.json' , // Lerna
101101 // Note: is there any interest in the following?
102102 // 'workspace.jsonc', // Bit
103103 // 'nx.json', // Nx
104104 // 'rush.json', // Rush
105- ] )
105+ ]
106106
107107// Our defaults.
108108type Config = Required < ExternalsOptions >
@@ -165,39 +165,38 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
165165 . filter ( isString )
166166 . map ( packagePath => path . resolve ( packagePath ) )
167167 if ( packagePaths . length === 0 ) {
168- for (
168+ loop: for (
169169 let current = process . cwd ( ) , previous : string | undefined = undefined ;
170170 previous !== current ;
171171 previous = current , current = path . dirname ( current )
172172 ) {
173- const entries = await fs . readdir ( current , { withFileTypes : true } ) . catch ( ( { code } : NodeJS . ErrnoException ) => code )
174- if ( isString ( entries ) || ! entries ) {
175- return this . error ( {
176- message : `Could not read directory ${ JSON . stringify ( current ) } , error: ${ entries || 'UNKNOWN' } .` ,
177- stack : undefined
178- } )
179- }
180-
181173 // Gather package.json files.
182- if ( entries . some ( entry => entry . name === 'package.json' && entry . isFile ( ) ) )
183- packagePaths . push ( path . join ( current , 'package.json' ) )
184-
185- // Break early if this is a git repo root or there is a known workspace root file.
186- if ( entries . some ( entry =>
187- ( entry . name === '.git' && entry . isDirectory ( ) ) || ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
188- ) ) {
174+ let name = path . join ( current , 'package.json' )
175+ let stat = await fs . stat ( name ) . catch ( ( ) => null )
176+ if ( stat ?. isFile ( ) )
177+ packagePaths . push ( name )
178+
179+ // Break early is this is a git repo or there is a known workspace root file.
180+ name = path . join ( current , '.git' )
181+ stat = await fs . stat ( name ) . catch ( ( ) => null )
182+ if ( stat ?. isDirectory ( ) )
189183 break
184+ for ( const file of workspaceRootFiles ) {
185+ name = path . join ( current , file )
186+ stat = await fs . stat ( name ) . catch ( ( ) => null )
187+ if ( stat ?. isFile ( ) )
188+ break loop
190189 }
191190 }
192191 }
193192
194193 // Gather dependencies names.
195194 const dependencies : Record < string , string > = { }
196195 for ( const packagePath of packagePaths ) {
197- const buffer = await fs . readFile ( packagePath ) . catch ( ( { code } : NodeJS . ErrnoException ) => code )
198- if ( isString ( buffer ) || ! buffer ) {
196+ const buffer = await fs . readFile ( packagePath ) . catch ( ( err : NodeJS . ErrnoException ) => err )
197+ if ( buffer instanceof Error ) {
199198 return this . error ( {
200- message : `Cannot read file ${ JSON . stringify ( packagePath ) } , error: ${ buffer || 'UNKNOWN' } .` ,
199+ message : `Cannot read file ${ packagePath } , error: ${ buffer . code } .` ,
201200 stack : undefined
202201 } )
203202 }
@@ -215,7 +214,7 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
215214 this . addWatchFile ( packagePath )
216215
217216 // Break early if this is an npm/yarn workspace root.
218- if ( Array . isArray ( pkg . workspaces ) && pkg . workspaces . length > 0 )
217+ if ( Array . isArray ( pkg . workspaces ) )
219218 break
220219 }
221220 catch {
0 commit comments