@@ -34,11 +34,8 @@ export interface ExternalsOptions {
34
34
*/
35
35
export default function externals ( options : ExternalsOptions = { } ) : Plugin {
36
36
37
- // Store eventual warnings until we can display them
38
- const warnings : string [ ] = [ ]
39
-
40
37
// Consolidate options
41
- const opts : Required < ExternalsOptions > = {
38
+ const consolidatedOptions : Required < ExternalsOptions > = {
42
39
packagePath : [ ] ,
43
40
builtins : true ,
44
41
deps : false ,
@@ -50,9 +47,12 @@ export interface ExternalsOptions {
50
47
...options
51
48
}
52
49
50
+ // This will store all eventual warnings until we can display them
51
+ const warnings : string [ ] = [ ]
52
+
53
53
// Map the include and exclude options to arrays of regexes
54
- const [ include , exclude ] = [ 'include' , 'exclude' ] . map ( option => new Array ( )
55
- . concat ( ( opts as any ) [ option ] )
54
+ const [ include , exclude ] = [ 'include' , 'exclude' ] . map ( optionName => new Array ( )
55
+ . concat ( ( consolidatedOptions as any ) [ optionName ] )
56
56
. map ( ( entry : string | RegExp , index : number ) : RegExp => {
57
57
if ( entry instanceof RegExp ) {
58
58
return entry
@@ -62,68 +62,69 @@ export interface ExternalsOptions {
62
62
}
63
63
else {
64
64
if ( ! ! entry ) {
65
- warnings . push ( `Ignoring wrong entry type #${ index } in '${ option } ' option: '${ entry } '` )
65
+ warnings . push ( `Ignoring wrong entry type #${ index } in '${ optionName } ' option: '${ entry } '` )
66
66
}
67
67
return / (? = n o ) m a t c h /
68
68
}
69
69
} )
70
70
)
71
71
72
- // Build a function to filter out unwanted dependencies
73
- const filterFn = ( dep : string ) => ! exclude . some ( rx => rx . test ( dep ) )
72
+ // Build a function to keep only non excluded dependencies
73
+ const isNotExcluded = ( id : string ) => ! exclude . some ( rx => rx . test ( id ) )
74
74
75
- // Filter NodeJS builtins
76
- const builtins = ( opts . builtins ? builtinModules : [ ] ) . filter ( filterFn )
77
-
78
- // Normalize package paths
79
- let packagePaths : string [ ] = ( [ ] as string [ ] ) . concat ( opts . packagePath )
80
-
81
- // Array of the final regexes, include potential import from a sub directory (e.g. 'lodash/map')
75
+ // Array of the final regexes
82
76
const externals : RegExp [ ] = [ ]
77
+ const isExternal = ( id : string ) => externals . some ( deps => deps . test ( id ) )
83
78
84
79
return {
85
80
name : 'node-externals' ,
86
81
87
82
async buildStart ( ) {
88
83
89
- // Find and filter dependencies
84
+ // 1) Filter NodeJS builtins, supporting potential import from a sub directory (e.g. 'fs/promises')
85
+ const builtins = ( consolidatedOptions . builtins ? builtinModules : [ ] ) . filter ( isNotExcluded )
86
+ if ( builtins . length > 0 ) {
87
+ externals . push ( new RegExp ( '^(?:' + builtins . join ( '|' ) + ')(\/.+)?$' ) )
88
+ }
89
+
90
+ // 2) Find and filter dependencies, supporting potential import from a sub directory (e.g. 'lodash/map')
91
+ const packagePaths : string [ ] = ( [ ] as string [ ] ) . concat ( consolidatedOptions . packagePath )
90
92
const dependencies = ( await findDependencies ( {
91
93
packagePaths : packagePaths . length > 0 ? packagePaths : findPackagePaths ( ) ,
92
94
keys : [
93
- opts . deps && 'dependencies' ,
94
- opts . devDeps && 'devDependencies' ,
95
- opts . peerDeps && 'peerDependencies' ,
96
- opts . optDeps && 'optionalDependencies'
95
+ consolidatedOptions . deps && 'dependencies' ,
96
+ consolidatedOptions . devDeps && 'devDependencies' ,
97
+ consolidatedOptions . peerDeps && 'peerDependencies' ,
98
+ consolidatedOptions . optDeps && 'optionalDependencies'
97
99
] . filter ( Boolean ) as string [ ] ,
98
100
warnings
99
- } ) ) . filter ( filterFn )
100
-
101
- // Issue the warnings we may have collected
102
- let msg : string | undefined
103
- while ( msg = warnings . shift ( ) ) {
104
- this . warn ( msg )
105
- }
106
-
107
- // Build regexes
108
- if ( builtins . length > 0 ) {
109
- externals . push ( new RegExp ( '^(?:' + builtins . join ( '|' ) + ')(\/.+)?$' ) )
110
- }
101
+ } ) ) . filter ( isNotExcluded )
111
102
112
103
if ( dependencies . length > 0 ) {
113
104
externals . push ( new RegExp ( '^(?:' + dependencies . join ( '|' ) + ')(\/.+)?$' ) )
114
105
}
115
106
107
+ // 3) Add the include option
116
108
if ( include . length > 0 ) {
117
109
externals . push ( ...include )
118
110
}
111
+
112
+ // All done. Issue the warnings we may have collected
113
+ let msg : string | undefined
114
+ while ( msg = warnings . shift ( ) ) {
115
+ this . warn ( msg )
116
+ }
119
117
} ,
120
118
121
119
resolveId ( id , importer ) {
120
+ // Ignore entry chunks & don't mess with other plugins
121
+ if ( ! importer ?. charCodeAt ( 0 ) || ! id . charCodeAt ( 0 ) ) {
122
+ return null
123
+ }
124
+
122
125
// Return `false` if importee should be treated as an external module,
123
- // otherwise return `null` to let Rollup and other plugins handle it.
124
- return importer && ! / \0 / . test ( id ) && externals . some ( deps => deps . test ( id ) ) && filterFn ( id )
125
- ? false
126
- : null
126
+ // otherwise return `null` to let Rollup and other plugins handle it
127
+ return isExternal ( id ) && isNotExcluded ( id ) ? false : null
127
128
}
128
129
}
129
130
}
0 commit comments