@@ -37,12 +37,29 @@ function cachedIsFile (file, cb) {
37
37
isFileCache [ file ] . then ( contents => cb ( null , contents ) , cb ) ;
38
38
}
39
39
40
+ function deprecatedMainField ( options , option , mainFields , field = option ) {
41
+ if ( option in options ) {
42
+ CONSOLE_WARN ( `node-resolve: setting options.${ option } is deprecated, please override options.mainFields instead` ) ;
43
+ if ( options [ option ] === false ) {
44
+ return mainFields . filter ( mainField => mainField === field ) ;
45
+ } else if ( options [ option ] === true && mainFields . indexOf ( field ) === - 1 ) {
46
+ return mainFields . concat ( [ field ] ) ;
47
+ }
48
+ }
49
+ return mainFields ;
50
+ }
51
+
40
52
const resolveIdAsync = ( file , opts ) => new Promise ( ( fulfil , reject ) => resolveId ( file , opts , ( err , contents ) => err ? reject ( err ) : fulfil ( contents ) ) ) ;
41
53
42
54
export default function nodeResolve ( options = { } ) {
43
- const useModule = options . module !== false ;
44
- const useMain = options . main !== false ;
45
- const useJsnext = options . jsnext === true ;
55
+ if ( 'mainFields' in options && ( 'module' in options || 'main' in options || 'jsnext' in options ) ) {
56
+ throw new Error ( `node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'` ) ;
57
+ }
58
+ let mainFields = options . mainFields || [ 'module' , 'main' ] ;
59
+ mainFields = deprecatedMainField ( options , 'browser' , mainFields ) ;
60
+ mainFields = deprecatedMainField ( options , 'module' , mainFields ) ;
61
+ mainFields = deprecatedMainField ( options , 'jsnext' , mainFields , 'jsnext:main' ) ;
62
+ mainFields = deprecatedMainField ( options , 'main' , mainFields ) ;
46
63
const isPreferBuiltinsSet = options . preferBuiltins === true || options . preferBuiltins === false ;
47
64
const preferBuiltins = isPreferBuiltinsSet ? options . preferBuiltins : true ;
48
65
const customResolveOptions = options . customResolveOptions || { } ;
@@ -59,8 +76,8 @@ export default function nodeResolve ( options = {} ) {
59
76
throw new Error ( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' ) ;
60
77
}
61
78
62
- if ( ! useModule && ! useMain && ! useJsnext ) {
63
- throw new Error ( `At least one of options.module, options.main or options.jsnext must be true ` ) ;
79
+ if ( ! mainFields . length ) {
80
+ throw new Error ( `Please ensure at least one 'mainFields' value is specified ` ) ;
64
81
}
65
82
66
83
let preserveSymlinks ;
@@ -82,8 +99,8 @@ export default function nodeResolve ( options = {} ) {
82
99
83
100
const basedir = importer ? dirname ( importer ) : process . cwd ( ) ;
84
101
85
- if ( options . browser && browserMapCache [ importer ] ) {
86
- const resolvedImportee = resolve ( basedir , importee ) ;
102
+ if ( mainFields . indexOf ( ' browser' ) !== - 1 && browserMapCache [ importer ] ) {
103
+ const resolvedImportee = resolve ( basedir , importee ) ;
87
104
const browser = browserMapCache [ importer ] ;
88
105
if ( browser [ importee ] === false || browser [ resolvedImportee ] === false ) {
89
106
return ES6_BROWSER_EMPTY ;
@@ -115,7 +132,7 @@ export default function nodeResolve ( options = {} ) {
115
132
basedir,
116
133
packageFilter ( pkg , pkgPath ) {
117
134
const pkgRoot = dirname ( pkgPath ) ;
118
- if ( options . browser && typeof pkg [ 'browser' ] === 'object' ) {
135
+ if ( mainFields . indexOf ( ' browser' ) !== - 1 && typeof pkg [ 'browser' ] === 'object' ) {
119
136
packageBrowserField = Object . keys ( pkg [ 'browser' ] ) . reduce ( ( browser , key ) => {
120
137
const resolved = pkg [ 'browser' ] [ key ] === false ? false : resolve ( pkgRoot , pkg [ 'browser' ] [ key ] ) ;
121
138
browser [ key ] = resolved ;
@@ -133,13 +150,16 @@ export default function nodeResolve ( options = {} ) {
133
150
} , { } ) ;
134
151
}
135
152
136
- if ( options . browser && typeof pkg [ 'browser' ] === 'string' ) {
137
- pkg [ 'main' ] = pkg [ 'browser' ] ;
138
- } else if ( useModule && pkg [ 'module' ] ) {
139
- pkg [ 'main' ] = pkg [ 'module' ] ;
140
- } else if ( useJsnext && pkg [ 'jsnext:main' ] ) {
141
- pkg [ 'main' ] = pkg [ 'jsnext:main' ] ;
142
- } else if ( ( useJsnext || useModule ) && ! useMain ) {
153
+ let overriddenMain = false ;
154
+ for ( const i in mainFields ) {
155
+ const field = mainFields [ i ] ;
156
+ if ( typeof pkg [ field ] === 'string' ) {
157
+ pkg [ 'main' ] = pkg [ field ] ;
158
+ overriddenMain = true ;
159
+ break ;
160
+ }
161
+ }
162
+ if ( overriddenMain === false && mainFields . indexOf ( 'main' ) === - 1 ) {
143
163
disregardResult = true ;
144
164
}
145
165
return pkg ;
@@ -159,7 +179,7 @@ export default function nodeResolve ( options = {} ) {
159
179
)
160
180
. catch ( ( ) => false )
161
181
. then ( resolved => {
162
- if ( options . browser && packageBrowserField ) {
182
+ if ( mainFields . indexOf ( ' browser' ) !== - 1 && packageBrowserField ) {
163
183
if ( packageBrowserField [ resolved ] ) {
164
184
resolved = packageBrowserField [ resolved ] ;
165
185
}
0 commit comments