@@ -107,7 +107,6 @@ const workspaceRootFiles = new Set([
107107
108108// Our defaults
109109type Config = Required < ExternalsOptions >
110-
111110const defaults : Config = {
112111 builtins : true ,
113112 builtinsPrefix : 'add' ,
@@ -129,11 +128,11 @@ const isString = (str: unknown): str is string =>
129128 */
130129function externals ( options : ExternalsOptions = { } ) : Plugin {
131130
132- const config : Config = Object . assign ( Object . create ( null ) , defaults , options )
131+ const config : Config = { ... defaults , ... options }
133132 let include : RegExp [ ] ,
134133 exclude : RegExp [ ]
135- const isIncluded = ( id : string ) => include . some ( rx => rx . test ( id ) )
136- const isExcluded = ( id : string ) => exclude . some ( rx => rx . test ( id ) )
134+ const isIncluded = ( id : string ) => include . some ( rx => rx . test ( id ) ) ,
135+ isExcluded = ( id : string ) => exclude . some ( rx => rx . test ( id ) )
137136
138137 return {
139138 name : 'node-externals' ,
@@ -144,43 +143,41 @@ function externals(options: ExternalsOptions = {}): Plugin {
144143 ( [ ] as Array < string | RegExp | null | undefined | false > )
145144 . concat ( config [ option ] )
146145 . reduce ( ( result , entry , index ) => {
147- if ( entry ) {
148- if ( entry instanceof RegExp )
149- result . push ( entry )
150- else if ( typeof entry === 'string' )
151- result . push ( new RegExp ( '^' + entry . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) + '$' ) )
152- else {
153- this . warn ( `Ignoring wrong entry type #${ index } in '${ option } ' option: ${ JSON . stringify ( entry ) } ` )
154- }
146+ if ( entry instanceof RegExp )
147+ result . push ( entry )
148+ else if ( isString ( entry ) )
149+ result . push ( new RegExp ( '^' + entry . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) + '$' ) )
150+ else if ( entry ) {
151+ this . warn ( `Ignoring wrong entry type #${ index } in '${ option } ' option: ${ JSON . stringify ( entry ) } ` )
155152 }
156153 return result
157154 } , [ ] as RegExp [ ] )
158155 )
159156
160- // Prepare npm dependencies list.
161- const packageFiles : string [ ] = Array . isArray ( config . packagePath )
157+ // Populate the packagePath option if not given by getting all package.json files
158+ // from cwd up to the root of the git repo, the root of the monorepo,
159+ // or the root of the volume, whichever comes first.
160+ const packagePaths : string [ ] = Array . isArray ( config . packagePath )
162161 ? config . packagePath . filter ( isString )
163162 : isString ( config . packagePath )
164163 ? [ config . packagePath ]
165164 : [ ]
166-
167- // Populate packagePaths if not given by getting all package.json files
168- // from cwd up to the root of the git repo, the root of the monorepo,
169- // or the root of the volume, whichever comes first.
170- if ( packageFiles . length === 0 ) {
165+ if ( packagePaths . length === 0 ) {
171166 for (
172167 let current = process . cwd ( ) , previous : string | undefined ;
173168 previous !== current ;
174169 previous = current , current = path . dirname ( current )
175170 ) {
176171 const entries = await fs . readdir ( current , { withFileTypes : true } )
172+
173+ // Gather package.json files
177174 if ( entries . some ( entry => entry . name === 'package.json' && entry . isFile ( ) ) )
178- packageFiles . push ( path . join ( current , 'package.json' ) )
175+ packagePaths . push ( path . join ( current , 'package.json' ) )
179176
180- // Break early if this is a git repo root or a pnpm/lerna workspace root.
177+ // Break early if this is a git repo root or there is a known monorepo root file .
181178 if ( entries . some ( entry =>
182- ( entry . name === '.git' && entry . isDirectory ( ) ) ||
183- ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
179+ ( entry . name === '.git' && entry . isDirectory ( ) )
180+ || ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
184181 ) ) {
185182 break
186183 }
@@ -189,9 +186,9 @@ function externals(options: ExternalsOptions = {}): Plugin {
189186
190187 // Gather dependencies names.
191188 const dependencies : Record < string , string > = { }
192- for ( const packageFile of packageFiles ) {
189+ for ( const packagePath of packagePaths ) {
193190 try {
194- const json = ( await fs . readFile ( packageFile ) ) . toString ( )
191+ const json = ( await fs . readFile ( packagePath ) ) . toString ( )
195192 try {
196193 const pkg : PackageJson = JSON . parse ( json )
197194 Object . assign ( dependencies ,
@@ -202,23 +199,22 @@ function externals(options: ExternalsOptions = {}): Plugin {
202199 )
203200
204201 // Watch the file.
205- if ( this . meta . watchMode )
206- this . addWatchFile ( packageFile )
202+ this . addWatchFile ( packagePath )
207203
208204 // Break early if this is a npm/yarn workspace root.
209- if ( 'workspaces' in pkg || 'packages' in pkg )
205+ if ( 'workspaces' in pkg )
210206 break
211207 }
212208 catch {
213209 this . error ( {
214- message : `File ${ JSON . stringify ( packageFile ) } does not look like a valid package.json file.` ,
210+ message : `File ${ JSON . stringify ( packagePath ) } does not look like a valid package.json file.` ,
215211 stack : undefined
216212 } )
217213 }
218214 }
219215 catch {
220216 this . error ( {
221- message : `Cannot read file ${ JSON . stringify ( packageFile ) } ` ,
217+ message : `Cannot read file ${ JSON . stringify ( packagePath ) } ` ,
222218 stack : undefined
223219 } )
224220 }
0 commit comments