Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 517758d

Browse files
committed
feat: add mainFields api. Deprecate browser, jsnext, module, main
1 parent 73b01b1 commit 517758d

File tree

3 files changed

+104
-60
lines changed

3 files changed

+104
-60
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ export default {
2525
name: 'MyModule',
2626
plugins: [
2727
resolve({
28+
29+
// the fields to scan in a package.json to determine the entry point..
30+
mainFields: ['module', 'main'], // Default: ['module', 'main']
31+
32+
// DEPRECATED: use `mainFields` instead
2833
// use "module" field for ES6 module if possible
2934
module: true, // Default: true
3035

36+
// DEPRECATED: use `mainFields` instead
3137
// use "jsnext:main" if possible
3238
// legacy field pointing to ES6 module in third-party libraries,
3339
// deprecated in favor of "pkg.module":
3440
// - see: https://github.com/rollup/rollup/wiki/pkg.module
3541
jsnext: true, // Default: false
3642

43+
// DEPRECATED: use `mainFields` instead
3744
// use "main" field or index.js, even if it's not an ES6 module
3845
// (needs to be converted from CommonJS to ES6
3946
// – see https://github.com/rollup/rollup-plugin-commonjs
4047
main: true, // Default: true
4148

49+
// DEPRECATED: use `mainFields` instead
4250
// some package.json files have a `browser` field which
4351
// specifies alternative files to load for people bundling
4452
// for the browser. If that's you, use this option, otherwise

src/index.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,29 @@ function cachedIsFile (file, cb) {
3737
isFileCache[file].then(contents => cb(null, contents), cb);
3838
}
3939

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+
4052
const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents)));
4153

4254
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);
4663
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
4764
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
4865
const customResolveOptions = options.customResolveOptions || {};
@@ -59,8 +76,8 @@ export default function nodeResolve ( options = {} ) {
5976
throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' );
6077
}
6178

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` );
6481
}
6582

6683
let preserveSymlinks;
@@ -82,8 +99,8 @@ export default function nodeResolve ( options = {} ) {
8299

83100
const basedir = importer ? dirname( importer ) : process.cwd();
84101

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 );
87104
const browser = browserMapCache[importer];
88105
if (browser[importee] === false || browser[resolvedImportee] === false) {
89106
return ES6_BROWSER_EMPTY;
@@ -115,7 +132,7 @@ export default function nodeResolve ( options = {} ) {
115132
basedir,
116133
packageFilter ( pkg, pkgPath ) {
117134
const pkgRoot = dirname( pkgPath );
118-
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
135+
if (mainFields.indexOf('browser') !== -1 && typeof pkg[ 'browser' ] === 'object') {
119136
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
120137
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
121138
browser[ key ] = resolved;
@@ -133,13 +150,16 @@ export default function nodeResolve ( options = {} ) {
133150
}, {});
134151
}
135152

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 ) {
143163
disregardResult = true;
144164
}
145165
return pkg;
@@ -159,7 +179,7 @@ export default function nodeResolve ( options = {} ) {
159179
)
160180
.catch(() => false)
161181
.then(resolved => {
162-
if (options.browser && packageBrowserField) {
182+
if (mainFields.indexOf('browser') !== -1 && packageBrowserField) {
163183
if (packageBrowserField[ resolved ]) {
164184
resolved = packageBrowserField[ resolved ];
165185
}

0 commit comments

Comments
 (0)