55 * Useful when bundling a NodeJS or an Electron app and you don't want to bundle
66 * node/npm modules with your own code but rather require() them at runtime.
77 */
8- import { resolve } from 'path'
98import { Plugin } from 'rollup'
109import builtinModules from 'builtin-modules'
10+ import { findPackagePaths , findDependencies } from './dependencies'
1111
1212export interface ExternalsOptions {
13- /** Path/to/your/package.json file. Defaults to the one in `process.cwd()`. */
14- packagePath ?: string
13+ /**
14+ * Path/to/your/package.json file (or array of paths).
15+ * Defaults to all package.json files found in parent directories recursively.
16+ * Won't got outside of a git repository.
17+ */
18+ packagePath ?: string | string [ ]
1519 /** Mark node built-in modules like `path`, `fs`... as external. Defaults to `true`. */
1620 builtins ?: boolean
1721 /** Mark dependencies as external. Defaults to `false`. */
@@ -41,7 +45,7 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
4145
4246 // Consolidate options
4347 const opts : Required < ExternalsOptions > = {
44- packagePath : resolve ( process . cwd ( ) , 'package.json' ) ,
48+ packagePath : [ ] ,
4549 builtins : true ,
4650 deps : false ,
4751 devDeps : true ,
@@ -83,33 +87,11 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
8387 // Filter NodeJS builtins
8488 const builtins = ( opts . builtins ? builtinModules : [ ] ) . filter ( f )
8589
86- // Filter deps from package.json
87- let pkg
88- try {
89- pkg = require ( opts . packagePath )
90- }
91- catch {
92- warnings . push ( "couldn't read package.json, please make sure it exists in the same directory as rollup.config.js or use the 'packagePath' option." )
93- pkg = Object . create ( null )
94- }
95- const dependencies : string [ ] = [
96- ...( opts . deps ? Object . keys ( pkg . dependencies || { } ) : [ ] ) ,
97- ...( opts . devDeps ? Object . keys ( pkg . devDependencies || { } ) : [ ] ) ,
98- ...( opts . peerDeps ? Object . keys ( pkg . peerDependencies || { } ) : [ ] ) ,
99- ...( opts . optDeps ? Object . keys ( pkg . optionalDependencies || { } ) : [ ] )
100- ] . filter ( f )
101-
102- // Build the final regexes, include potential import from a sub directory (e.g. 'lodash/map')
90+ // Normalize package paths
91+ let packagePaths : string [ ] = ( [ ] as string [ ] ) . concat ( opts . packagePath )
92+
93+ // Array of the final regexes, include potential import from a sub directory (e.g. 'lodash/map')
10394 const externals : RegExp [ ] = [ ]
104- if ( builtins . length > 0 ) {
105- externals . push ( new RegExp ( '^(?:' + builtins . join ( '|' ) + ')(\/.+)?$' ) )
106- }
107- if ( dependencies . length > 0 ) {
108- externals . push ( new RegExp ( '^(?:' + dependencies . join ( '|' ) + ')(\/.+)?$' ) )
109- }
110- if ( include . length > 0 ) {
111- externals . push ( ...include )
112- }
11395
11496 return {
11597 name : 'node-externals' ,
@@ -120,11 +102,34 @@ export default function externals(options: ExternalsOptions = {}): Plugin {
120102 return importer && ! / \0 / . test ( source ) && externals . some ( deps => deps . test ( source ) ) ? false : null
121103 } ,
122104
123- buildStart ( ) {
105+ async buildStart ( ) {
124106 let msg : string | undefined
125107 while ( msg = warnings . shift ( ) ) {
126108 this . warn ( msg )
127109 }
110+
111+ // Find and filter dependencies
112+ const dependencies = ( await findDependencies ( {
113+ packagePaths : packagePaths . length > 0 ? packagePaths : findPackagePaths ( ) ,
114+ keys : [
115+ opts . deps && 'dependencies' ,
116+ opts . devDeps && 'devDependencies' ,
117+ opts . peerDeps && 'peerDependencies' ,
118+ opts . optDeps && 'optionalDependencies'
119+ ] . filter ( Boolean ) as string [ ] ,
120+ warnings
121+ } ) ) . filter ( f )
122+
123+ // Build regexes
124+ if ( builtins . length > 0 ) {
125+ externals . push ( new RegExp ( '^(?:' + builtins . join ( '|' ) + ')(\/.+)?$' ) )
126+ }
127+ if ( dependencies . length > 0 ) {
128+ externals . push ( new RegExp ( '^(?:' + dependencies . join ( '|' ) + ')(\/.+)?$' ) )
129+ }
130+ if ( include . length > 0 ) {
131+ externals . push ( ...include )
132+ }
128133 }
129134 }
130135}
0 commit comments