@@ -37,12 +37,41 @@ function cachedIsFile (file, cb) {
37
37
isFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
38
38
}
39
39
40
+ function getMainFields ( options ) {
41
+ let mainFields ;
42
+ if ( options . mainFields ) {
43
+ if ( 'module' in options || 'main' in options || 'jsnext' in options ) {
44
+ throw new Error ( `node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'` ) ;
45
+ }
46
+ mainFields = options . mainFields ;
47
+ } else {
48
+ mainFields = [ 'module' , 'main' ] ;
49
+ [ [ 'module' , 'module' ] , [ 'jsnext' , 'jsnext:main' ] , [ 'main' , 'main' ] ] . forEach ( ( [ option , field ] ) => {
50
+ if ( option in options ) {
51
+ // eslint-disable-next-line no-console
52
+ console . warn ( `node-resolve: setting options.${ option } is deprecated, please override options.mainFields instead` ) ;
53
+ if ( options [ option ] === false ) {
54
+ mainFields = mainFields . filter ( mainField => mainField === field ) ;
55
+ } else if ( options [ option ] === true && mainFields . indexOf ( field ) === - 1 ) {
56
+ mainFields . push ( field ) ;
57
+ }
58
+ }
59
+ } ) ;
60
+ }
61
+ if ( options . browser && mainFields . indexOf ( 'browser' ) === - 1 ) {
62
+ return [ 'browser' ] . concat ( mainFields ) ;
63
+ }
64
+ if ( ! mainFields . length ) {
65
+ throw new Error ( `Please ensure at least one 'mainFields' value is specified` ) ;
66
+ }
67
+ return mainFields ;
68
+ }
69
+
40
70
const resolveIdAsync = ( file , opts ) => new Promise ( ( fulfil , reject ) => resolveId ( file , opts , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
41
71
42
72
export default function nodeResolve ( options = { } ) {
43
- const useModule = options . module !== false ;
44
- const useMain = options . main !== false ;
45
- const useJsnext = options . jsnext === true ;
73
+ const mainFields = getMainFields ( options ) ;
74
+ const useBrowserOverrides = mainFields . indexOf ( 'browser' ) !== - 1 ;
46
75
const isPreferBuiltinsSet = options . preferBuiltins === true || options . preferBuiltins === false ;
47
76
const preferBuiltins = isPreferBuiltinsSet ? options . preferBuiltins : true ;
48
77
const customResolveOptions = options . customResolveOptions || { } ;
@@ -59,10 +88,6 @@ export default function nodeResolve ( options = {} ) {
59
88
throw new Error ( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' ) ;
60
89
}
61
90
62
- if ( ! useModule && ! useMain && ! useJsnext ) {
63
- throw new Error ( `At least one of options.module, options.main or options.jsnext must be true` ) ;
64
- }
65
-
66
91
let preserveSymlinks ;
67
92
68
93
return {
@@ -83,7 +108,7 @@ export default function nodeResolve ( options = {} ) {
83
108
const basedir = importer ? dirname ( importer ) : process . cwd ( ) ;
84
109
85
110
// https://github.com/defunctzombie/package-browser-field-spec
86
- if ( options . browser && browserMapCache [ importer ] ) {
111
+ if ( useBrowserOverrides && browserMapCache [ importer ] ) {
87
112
const resolvedImportee = resolve ( basedir , importee ) ;
88
113
const browser = browserMapCache [ importer ] ;
89
114
if ( browser [ importee ] === false || browser [ resolvedImportee ] === false ) {
@@ -115,7 +140,7 @@ export default function nodeResolve ( options = {} ) {
115
140
basedir,
116
141
packageFilter ( pkg , pkgPath ) {
117
142
const pkgRoot = dirname ( pkgPath ) ;
118
- if ( options . browser && typeof pkg [ 'browser' ] === 'object' ) {
143
+ if ( useBrowserOverrides && typeof pkg [ 'browser' ] === 'object' ) {
119
144
packageBrowserField = Object . keys ( pkg [ 'browser' ] ) . reduce ( ( browser , key ) => {
120
145
let resolved = pkg [ 'browser' ] [ key ] ;
121
146
if ( resolved && resolved [ 0 ] === '.' ) {
@@ -136,13 +161,16 @@ export default function nodeResolve ( options = {} ) {
136
161
} , { } ) ;
137
162
}
138
163
139
- if ( options . browser && typeof pkg [ 'browser' ] === 'string' ) {
140
- pkg [ 'main' ] = pkg [ 'browser' ] ;
141
- } else if ( useModule && pkg [ 'module' ] ) {
142
- pkg [ 'main' ] = pkg [ 'module' ] ;
143
- } else if ( useJsnext && pkg [ 'jsnext:main' ] ) {
144
- pkg [ 'main' ] = pkg [ 'jsnext:main' ] ;
145
- } else if ( ( useJsnext || useModule ) && ! useMain ) {
164
+ let overriddenMain = false ;
165
+ for ( let i = 0 ; i < mainFields . length ; i ++ ) {
166
+ const field = mainFields [ i ] ;
167
+ if ( typeof pkg [ field ] === 'string' ) {
168
+ pkg [ 'main' ] = pkg [ field ] ;
169
+ overriddenMain = true ;
170
+ break ;
171
+ }
172
+ }
173
+ if ( overriddenMain === false && mainFields . indexOf ( 'main' ) === - 1 ) {
146
174
disregardResult = true ;
147
175
}
148
176
return pkg ;
@@ -161,7 +189,7 @@ export default function nodeResolve ( options = {} ) {
161
189
Object . assign ( resolveOptions , customResolveOptions )
162
190
)
163
191
. then ( resolved => {
164
- if ( resolved && options . browser && packageBrowserField ) {
192
+ if ( resolved && useBrowserOverrides && packageBrowserField ) {
165
193
if ( packageBrowserField . hasOwnProperty ( resolved ) ) {
166
194
if ( ! packageBrowserField [ resolved ] ) {
167
195
browserMapCache [ resolved ] = packageBrowserField ;
0 commit comments