@@ -3,16 +3,16 @@ import { builtinModules } from 'module'
33import { findPackagePaths , findDependencies } from './dependencies'
44
55export interface ExternalsOptions {
6+ /** Mark node built-in modules like `path`, `fs`... as external. Defaults to `true`. */
7+ builtins ?: boolean
8+ /** How to treat prefixed builtins. Defaults to `true` (prefixed are considered the same as unprefixed). */
9+ prefixedBuiltins ?: boolean | 'strip'
610 /**
711 * Path/to/your/package.json file (or array of paths).
812 * Defaults to all package.json files found in parent directories recursively.
913 * Won't got outside of a git repository.
1014 */
1115 packagePath ?: string | string [ ]
12- /** Mark node built-in modules like `path`, `fs`... as external. Defaults to `true`. */
13- builtins ?: boolean
14- /** Treat prefixed builtins as their unprefixed counterpart. Optional. Default: true */
15- prefixedBuiltins ?: boolean | 'strip'
1616 /** Mark dependencies as external. Defaults to `true`. */
1717 deps ?: boolean
1818 /** Mark devDependencies as external. Defaults to `true`. */
@@ -27,7 +27,7 @@ export interface ExternalsOptions {
2727 exclude ?: string | RegExp | ( string | RegExp ) [ ]
2828}
2929
30- type IncludeExclude = Extract < keyof ExternalsOptions , 'include' | 'exclude' >
30+ type IncludeExclude = Pick < ExternalsOptions , 'include' | 'exclude' >
3131
3232/**
3333 * A Rollup plugin that automatically declares NodeJS built-in modules,
@@ -37,9 +37,9 @@ function externals(options: ExternalsOptions = {}): Plugin {
3737
3838 // Consolidate options
3939 const config : Required < ExternalsOptions > = {
40- packagePath : [ ] ,
4140 builtins : true ,
4241 prefixedBuiltins : 'strip' ,
42+ packagePath : [ ] ,
4343 deps : true ,
4444 devDeps : true ,
4545 peerDeps : true ,
@@ -49,32 +49,31 @@ function externals(options: ExternalsOptions = {}): Plugin {
4949 ...options
5050 }
5151
52- // This will store all eventual warnings until we can display them
52+ // This will store all eventual warnings until we can display them.
5353 const warnings : string [ ] = [ ]
5454
55- // Map the include and exclude options to arrays of regexes
56- const [ include , exclude ] = ( [ 'include' , 'exclude' ] as IncludeExclude [ ] ) . map ( optionName => ( [ ] as ( string | RegExp ) [ ] )
57- . concat ( config [ optionName ] )
55+ // Map the include and exclude options to arrays of regexes.
56+ const [ include , exclude ] = [ 'include' , 'exclude' ] . map ( option => new Array < string | RegExp > ( )
57+ . concat ( config [ option as keyof IncludeExclude ] )
5858 . map ( ( entry , index ) => {
59- if ( entry instanceof RegExp ) {
59+ if ( entry instanceof RegExp )
6060 return entry
61- }
62- else if ( typeof entry === 'string' ) {
61+
62+ if ( typeof entry === 'string' )
6363 return new RegExp ( '^' + entry . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) + '$' )
64+
65+ if ( entry ) {
66+ warnings . push ( `Ignoring wrong entry type #${ index } in '${ option } ' option: '${ entry } '` )
6467 }
65- else {
66- if ( entry ) {
67- warnings . push ( `Ignoring wrong entry type #${ index } in '${ optionName } ' option: '${ entry } '` )
68- }
69- return / (? = n o ) m a t c h /
70- }
68+
69+ return / (? = n o ) m a t c h /
7170 } )
7271 )
7372
74- // Filter function to keep only non excluded dependencies
73+ // A filter function to keep only non excluded dependencies.
7574 const isNotExcluded = ( id : string ) => ! exclude . some ( rx => rx . test ( id ) )
7675
77- // Array of the final regexes
76+ // The array of the final regexes.
7877 const externals : RegExp [ ] = [ ]
7978 const isExternal = ( id : string ) => externals . some ( rx => rx . test ( id ) )
8079
@@ -83,21 +82,21 @@ function externals(options: ExternalsOptions = {}): Plugin {
8382
8483 async buildStart ( ) {
8584
86- // 1) Filter NodeJS builtins, supporting potential import from a sub directory (e.g. 'fs/promises')
85+ // 1) Filter NodeJS builtins, supporting potential import from a sub directory (e.g. 'fs/promises').
8786 const builtins = ( config . builtins ? builtinModules : [ ] ) . filter ( isNotExcluded )
8887 if ( builtins . length > 0 ) {
8988 externals . push ( new RegExp ( `^(?:${ builtins . join ( '|' ) } )(?:/.+)?$` ) )
9089 }
9190
92- // 2) Find and filter dependencies, supporting potential import from a sub directory (e.g. 'lodash/map')
91+ // 2) Find and filter dependencies, supporting potential import from a sub directory (e.g. 'lodash/map').
9392 const packagePaths : string [ ] = ( [ ] as string [ ] ) . concat ( config . packagePath )
9493 const dependencies = ( await findDependencies ( {
9594 packagePaths : packagePaths . length > 0 ? packagePaths : findPackagePaths ( ) ,
9695 keys : [
97- config . deps && 'dependencies' ,
98- config . devDeps && 'devDependencies' ,
96+ config . deps && 'dependencies' ,
97+ config . devDeps && 'devDependencies' ,
9998 config . peerDeps && 'peerDependencies' ,
100- config . optDeps && 'optionalDependencies'
99+ config . optDeps && 'optionalDependencies'
101100 ] . filter ( Boolean ) as string [ ] ,
102101 warnings
103102 } ) ) . filter ( isNotExcluded )
@@ -106,12 +105,12 @@ function externals(options: ExternalsOptions = {}): Plugin {
106105 externals . push ( new RegExp ( `^(?:${ dependencies . join ( '|' ) } )(?:/.+)?$` ) )
107106 }
108107
109- // 3) Add the include option
108+ // 3) Add the include option.
110109 if ( include . length > 0 ) {
111110 externals . push ( ...include )
112111 }
113112
114- // All done. Issue the warnings we may have collected
113+ // All done. Issue the warnings we may have collected.
115114 while ( warnings . length > 0 ) {
116115 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117116 this . warn ( warnings . shift ( ) ! )
@@ -120,12 +119,12 @@ function externals(options: ExternalsOptions = {}): Plugin {
120119
121120 resolveId ( importee , importer , { isEntry } ) {
122121
123- // Ignore entry chunks & don't mess with other plugins
122+ // Ignore entry chunks & don't mess with other plugins.
124123 if ( isEntry || ! importee . charCodeAt ( 0 ) || ! importer ?. charCodeAt ( 0 ) ) {
125124 return null
126125 }
127126
128- // Remove node:/nodejs: prefix so builtins resolve to their unprefixed equivalent
127+ // Remove node:/nodejs: prefix so builtins resolve to their unprefixed equivalent.
129128 let stripped = importee
130129 if ( config . prefixedBuiltins ) {
131130 if ( importee . startsWith ( 'node:' ) ) {
@@ -137,7 +136,7 @@ function externals(options: ExternalsOptions = {}): Plugin {
137136 }
138137
139138 // Return object if importee should be treated as an external module,
140- // otherwise return `null` to let Rollup and other plugins handle it
139+ // otherwise return `null` to let Rollup and other plugins handle it.
141140 return isExternal ( stripped ) && isNotExcluded ( stripped )
142141 ? { id : config . prefixedBuiltins === 'strip' ? stripped : importee , external : true }
143142 : null
0 commit comments