@@ -78,6 +78,7 @@ export interface ExternalsOptions {
7878
7979// Fields of interest in package.json
8080interface PackageJson {
81+ name : string
8182 version : string
8283 workspaces ?: string [ ]
8384 dependencies ?: Record < string , string >
@@ -86,12 +87,8 @@ interface PackageJson {
8687 optionalDependencies ?: Record < string , string >
8788}
8889
89- // Get our own version
90- const { version } = createRequire ( import . meta. url ) ( '../package.json' ) as PackageJson
91-
92- // Node built-in prefix handling
93- const nodePrefix = 'node:'
94- const nodePrefixRx = / ^ n o d e : /
90+ // Get our own name and version
91+ const { name, version } = createRequire ( import . meta. url ) ( '../package.json' ) as PackageJson
9592
9693// Files that mark the root of a workspace
9794const workspaceRootFiles = new Set ( [
@@ -135,25 +132,24 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
135132 isExcluded = ( id : string ) => exclude . length > 0 && exclude . some ( rx => rx . test ( id ) )
136133
137134 return {
138- name : 'node-externals' ,
135+ name : name . replace ( / ^ r o l l u p - p l u g i n - / , '' ) ,
139136 version,
140137
141138 async buildStart ( ) {
142139
143140 // Map the include and exclude options to arrays of regexes.
144- [ include , exclude ] = ( [ 'include' , 'exclude' ] as const ) . map ( option =>
145- ( [ ] as Array < string | RegExp | null | undefined | false > )
146- . concat ( config [ option ] )
147- . reduce ( ( result , entry , index ) => {
148- if ( entry instanceof RegExp )
149- result . push ( entry )
150- else if ( isString ( entry ) )
151- result . push ( new RegExp ( '^' + entry . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) + '$' ) )
152- else if ( entry )
153- this . warn ( `Ignoring wrong entry type #${ index } in '${ option } ' option: ${ JSON . stringify ( entry ) } ` )
154-
155- return result
156- } , [ ] as RegExp [ ] )
141+ [ include , exclude ] = ( [ 'include' , 'exclude' ] as const ) . map ( option => ( [ ] as Array < MaybeFalsy < string | RegExp > > )
142+ . concat ( config [ option ] )
143+ . reduce ( ( result , entry , index ) => {
144+ if ( entry instanceof RegExp )
145+ result . push ( entry )
146+ else if ( isString ( entry ) )
147+ result . push ( new RegExp ( '^' + entry . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) + '$' ) )
148+ else if ( entry )
149+ this . warn ( `Ignoring wrong entry type #${ index } in '${ option } ' option: ${ JSON . stringify ( entry ) } ` )
150+
151+ return result
152+ } , [ ] as RegExp [ ] )
157153 )
158154
159155 // Populate the packagePath option if not given by getting all package.json files
@@ -169,10 +165,10 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
169165 previous !== current ;
170166 previous = current , current = path . dirname ( current )
171167 ) {
172- const entries = await fs . readdir ( current , { withFileTypes : true } ) . catch ( ( ) => null )
173- if ( entries === null ) {
168+ const entries = await fs . readdir ( current , { withFileTypes : true } ) . catch ( ( { code } : NodeJS . ErrnoException ) => code )
169+ if ( isString ( entries ) || ! entries ) {
174170 return this . error ( {
175- message : `Could not read contents of directory ${ JSON . stringify ( current ) } .` ,
171+ message : `Could not read directory ${ JSON . stringify ( current ) } , error: ${ entries || 'UNKNOWN' } .` ,
176172 stack : undefined
177173 } )
178174 }
@@ -183,8 +179,7 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
183179
184180 // Break early if this is a git repo root or there is a known workspace root file.
185181 if ( entries . some ( entry =>
186- ( entry . name === '.git' && entry . isDirectory ( ) )
187- || ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
182+ ( entry . name === '.git' && entry . isDirectory ( ) ) || ( workspaceRootFiles . has ( entry . name ) && entry . isFile ( ) )
188183 ) ) {
189184 break
190185 }
@@ -194,34 +189,33 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
194189 // Gather dependencies names.
195190 const dependencies : Record < string , string > = { }
196191 for ( const packagePath of packagePaths ) {
192+ const buffer = await fs . readFile ( packagePath ) . catch ( ( { code } : NodeJS . ErrnoException ) => code )
193+ if ( isString ( buffer ) || ! buffer ) {
194+ return this . error ( {
195+ message : `Cannot read file ${ JSON . stringify ( packagePath ) } , error: ${ buffer || 'UNKNOWN' } .` ,
196+ stack : undefined
197+ } )
198+ }
199+
197200 try {
198- const json = await fs . readFile ( packagePath ) . then ( buffer => buffer . toString ( ) )
199- try {
200- const pkg = JSON . parse ( json ) as PackageJson
201- Object . assign ( dependencies ,
202- config . deps ? pkg . dependencies : undefined ,
203- config . devDeps ? pkg . devDependencies : undefined ,
204- config . peerDeps ? pkg . peerDependencies : undefined ,
205- config . optDeps ? pkg . optionalDependencies : undefined
206- )
207-
208- // Watch this package.json
209- this . addWatchFile ( packagePath )
210-
211- // Break early if this is a npm/yarn workspace root.
212- if ( Array . isArray ( pkg . workspaces ) && pkg . workspaces . length > 0 )
213- break
214- }
215- catch {
216- this . error ( {
217- message : `File ${ JSON . stringify ( packagePath ) } does not look like a valid package.json file.` ,
218- stack : undefined
219- } )
220- }
201+ const pkg = JSON . parse ( buffer . toString ( ) ) as PackageJson
202+ Object . assign ( dependencies ,
203+ config . deps ? pkg . dependencies : undefined ,
204+ config . devDeps ? pkg . devDependencies : undefined ,
205+ config . peerDeps ? pkg . peerDependencies : undefined ,
206+ config . optDeps ? pkg . optionalDependencies : undefined
207+ )
208+
209+ // Watch this package.json
210+ this . addWatchFile ( packagePath )
211+
212+ // Break early if this is an npm/yarn workspace root.
213+ if ( Array . isArray ( pkg . workspaces ) && pkg . workspaces . length > 0 )
214+ break
221215 }
222216 catch {
223217 this . error ( {
224- message : `Cannot read file ${ JSON . stringify ( packagePath ) } ` ,
218+ message : `File ${ JSON . stringify ( packagePath ) } does not look like a valid package.json file. ` ,
225219 stack : undefined
226220 } )
227221 }
@@ -235,23 +229,23 @@ function nodeExternals(options: ExternalsOptions = {}): Plugin {
235229
236230 resolveId : {
237231 order : 'pre' ,
238- async handler ( specifier , importer ) {
232+ async handler ( specifier , importer , { isEntry } ) {
239233 if (
240- ! importer // Ignore entry points (they should always be resolved)
241- || path . isAbsolute ( specifier ) // Ignore already resolved ids
234+ isEntry // Ignore entry points (they should always be resolved)
242235 || / ^ (?: \0 | \. { 1 , 2 } \/ ) / . test ( specifier ) // Ignore virtual modules and relative imports
236+ || path . isAbsolute ( specifier ) // Ignore already resolved ids
243237 ) {
244238 return null
245239 }
246240
247241 // Handle node builtins.
248242 if ( isBuiltin ( specifier ) ) {
249- const stripped = specifier . replace ( nodePrefixRx , '' )
243+ const stripped = specifier . replace ( / ^ n o d e : / , '' )
250244 return {
251245 id : config . builtinsPrefix === 'ignore'
252246 ? specifier
253247 : config . builtinsPrefix === 'add' || ! isBuiltin ( stripped )
254- ? nodePrefix + stripped
248+ ? 'node:' + stripped
255249 : stripped ,
256250 external : ( config . builtins || isIncluded ( specifier ) ) && ! isExcluded ( specifier ) ,
257251 moduleSideEffects : false
0 commit comments