@@ -79,6 +79,7 @@ export interface ExternalsOptions {
79
79
// Fields of interest in package.json
80
80
interface PackageJson {
81
81
version : string
82
+ workspaces ?: string [ ]
82
83
dependencies ?: Record < string , string >
83
84
devDependencies ?: Record < string , string >
84
85
peerDependencies ?: Record < string , string >
@@ -130,8 +131,8 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
130
131
let include : RegExp [ ] ,
131
132
exclude : RegExp [ ]
132
133
133
- const isIncluded = ( id : string ) => include . length === 0 || include . some ( rx => rx . test ( id ) ) ,
134
- isExcluded = ( id : string ) => exclude . length > 0 && exclude . some ( rx => rx . test ( id ) )
134
+ const isIncluded = ( id : string ) => include . length > 0 && include . some ( rx => rx . test ( id ) ) ,
135
+ isExcluded = ( id : string ) => exclude . length > 0 && exclude . some ( rx => rx . test ( id ) )
135
136
136
137
return {
137
138
name : 'node-externals' ,
@@ -169,15 +170,19 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
169
170
previous = current , current = path . dirname ( current )
170
171
) {
171
172
const entries = await fs . readdir ( current , { withFileTypes : true } ) . catch ( ( ) => null )
172
- if ( entries === null )
173
- this . error ( `Could not read contents of directory ${ JSON . stringify ( current ) } .` )
173
+ if ( entries === null ) {
174
+ return this . error ( {
175
+ message : `Could not read contents of directory ${ JSON . stringify ( current ) } .` ,
176
+ stack : undefined
177
+ } )
178
+ }
174
179
175
180
// Gather package.json files.
176
- if ( entries ! . some ( entry => entry . name === 'package.json' && entry . isFile ( ) ) )
181
+ if ( entries . some ( entry => entry . name === 'package.json' && entry . isFile ( ) ) )
177
182
packagePaths . push ( path . join ( current , 'package.json' ) )
178
183
179
184
// Break early if this is a git repo root or there is a known workspace root file.
180
- if ( entries ! . some ( entry =>
185
+ if ( entries . some ( entry =>
181
186
( entry . name === '.git' && entry . isDirectory ( ) )
182
187
|| ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
183
188
) ) {
@@ -189,9 +194,8 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
189
194
// Gather dependencies names.
190
195
const dependencies : Record < string , string > = { }
191
196
for ( const packagePath of packagePaths ) {
192
- this . addWatchFile ( packagePath )
193
197
try {
194
- const json = ( await fs . readFile ( packagePath ) ) . toString ( )
198
+ const json = await fs . readFile ( packagePath ) . then ( buffer => buffer . toString ( ) )
195
199
try {
196
200
const pkg = JSON . parse ( json ) as PackageJson
197
201
Object . assign ( dependencies ,
@@ -201,8 +205,11 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
201
205
config . optDeps ? pkg . optionalDependencies : undefined
202
206
)
203
207
208
+ // Watch this package.json
209
+ this . addWatchFile ( packagePath )
210
+
204
211
// Break early if this is a npm/yarn workspace root.
205
- if ( ' workspaces' in pkg )
212
+ if ( Array . isArray ( pkg . workspaces ) && pkg . workspaces . length > 0 )
206
213
break
207
214
}
208
215
catch {
@@ -226,33 +233,36 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
226
233
include . push ( new RegExp ( '^(?:' + names . join ( '|' ) + ')(?:/.+)?$' ) )
227
234
} ,
228
235
229
- async resolveId ( specifier , importer ) {
230
- if (
231
- ! importer // Ignore entry points (they should always be resolved)
232
- || path . isAbsolute ( specifier ) // Ignore already resolved ids
233
- || / ^ (?: \0 | \. { 1 , 2 } \/ ) / . test ( specifier ) // Ignore virtual modules and relative imports
234
- ) {
235
- return null
236
- }
236
+ resolveId : {
237
+ order : 'pre' ,
238
+ async handler ( specifier , importer ) {
239
+ if (
240
+ ! importer // Ignore entry points (they should always be resolved)
241
+ || path . isAbsolute ( specifier ) // Ignore already resolved ids
242
+ || / ^ (?: \0 | \. { 1 , 2 } \/ ) / . test ( specifier ) // Ignore virtual modules and relative imports
243
+ ) {
244
+ return null
245
+ }
237
246
238
- // Handle node builtins.
239
- if ( isBuiltin ( specifier ) ) {
240
- const stripped = specifier . replace ( nodePrefixRx , '' )
241
- return {
242
- id : config . builtinsPrefix === 'ignore'
243
- ? specifier
244
- : config . builtinsPrefix === 'add' || ! isBuiltin ( stripped )
245
- ? nodePrefix + stripped
246
- : stripped ,
247
- external : ( config . builtins || isIncluded ( specifier ) ) && ! isExcluded ( specifier ) ,
248
- moduleSideEffects : false
247
+ // Handle node builtins.
248
+ if ( isBuiltin ( specifier ) ) {
249
+ const stripped = specifier . replace ( nodePrefixRx , '' )
250
+ return {
251
+ id : config . builtinsPrefix === 'ignore'
252
+ ? specifier
253
+ : config . builtinsPrefix === 'add' || ! isBuiltin ( stripped )
254
+ ? nodePrefix + stripped
255
+ : stripped ,
256
+ external : ( config . builtins || isIncluded ( specifier ) ) && ! isExcluded ( specifier ) ,
257
+ moduleSideEffects : false
258
+ }
249
259
}
250
- }
251
260
252
- // Handle npm dependencies.
253
- return isIncluded ( specifier ) && ! isExcluded ( specifier )
254
- ? false // external
255
- : null // normal handling
261
+ // Handle npm dependencies.
262
+ return isIncluded ( specifier ) && ! isExcluded ( specifier )
263
+ ? false // external
264
+ : null // normal handling
265
+ }
256
266
}
257
267
}
258
268
}
0 commit comments