@@ -95,14 +95,14 @@ interface PackageJson {
95
95
const { name, version } = createRequire ( import . meta. url ) ( '#package.json' ) as PackageJson
96
96
97
97
// Files that mark the root of a monorepo
98
- const workspaceRootFiles = new Set ( [
98
+ const workspaceRootFiles = [
99
99
'pnpm-workspace.yaml' , // pnpm
100
100
'lerna.json' , // Lerna
101
101
// Note: is there any interest in the following?
102
102
// 'workspace.jsonc', // Bit
103
103
// 'nx.json', // Nx
104
104
// 'rush.json', // Rush
105
- ] )
105
+ ]
106
106
107
107
// Our defaults.
108
108
type Config = Required < ExternalsOptions >
@@ -165,39 +165,38 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
165
165
. filter ( isString )
166
166
. map ( packagePath => path . resolve ( packagePath ) )
167
167
if ( packagePaths . length === 0 ) {
168
- for (
168
+ loop: for (
169
169
let current = process . cwd ( ) , previous : string | undefined = undefined ;
170
170
previous !== current ;
171
171
previous = current , current = path . dirname ( current )
172
172
) {
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
-
181
173
// 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 ( ) )
189
183
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
190
189
}
191
190
}
192
191
}
193
192
194
193
// Gather dependencies names.
195
194
const dependencies : Record < string , string > = { }
196
195
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 ) {
199
198
return this . error ( {
200
- message : `Cannot read file ${ JSON . stringify ( packagePath ) } , error: ${ buffer || 'UNKNOWN' } .` ,
199
+ message : `Cannot read file ${ packagePath } , error: ${ buffer . code } .` ,
201
200
stack : undefined
202
201
} )
203
202
}
@@ -215,7 +214,7 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
215
214
this . addWatchFile ( packagePath )
216
215
217
216
// 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 ) )
219
218
break
220
219
}
221
220
catch {
0 commit comments