@@ -41,6 +41,14 @@ async function chainWebpack (api, pluginOptions, config) {
41
41
// Find all the dependencies without a `main` property or with a `binary` property or set by user and add them as webpack externals
42
42
function getExternals ( api , pluginOptions ) {
43
43
const nodeModulesPath = pluginOptions . nodeModulesPath || './node_modules'
44
+ let nodeModulesPaths = [ ]
45
+ if ( Array . isArray ( nodeModulesPath ) ) {
46
+ // Set to user-defined array
47
+ nodeModulesPaths = nodeModulesPath
48
+ } else {
49
+ // Add path to list
50
+ nodeModulesPaths . push ( nodeModulesPath )
51
+ }
44
52
const userExternals = pluginOptions . externals || [ ]
45
53
const userExternalsWhitelist = [ ]
46
54
userExternals . forEach ( ( d , i ) => {
@@ -53,9 +61,20 @@ function getExternals (api, pluginOptions) {
53
61
const externalsList = Object . keys ( dependencies || { } ) . filter ( dep => {
54
62
// Return true if we want to add a dependency to externals
55
63
try {
56
- const pgkString = fs
57
- . readFileSync ( api . resolve ( `${ nodeModulesPath } /${ dep } /package.json` ) )
58
- . toString ( )
64
+ let pgkString
65
+ for ( const path of nodeModulesPaths ) {
66
+ // Check if package.json exists
67
+ if ( fs . existsSync ( api . resolve ( `${ path } /${ dep } /package.json` ) ) ) {
68
+ // If it does, read it and break
69
+ pgkString = fs
70
+ . readFileSync ( api . resolve ( `${ path } /${ dep } /package.json` ) )
71
+ . toString ( )
72
+ break
73
+ }
74
+ }
75
+ if ( ! pgkString ) {
76
+ throw new Error ( `Could not find a package.json for module ${ dep } ` )
77
+ }
59
78
const pkg = JSON . parse ( pgkString )
60
79
const fields = [ 'main' , 'module' , 'jsnext:main' , 'browser' ]
61
80
return (
0 commit comments