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

Commit

Permalink
feat: add mainFields api. Deprecate browser, jsnext, module, …
Browse files Browse the repository at this point in the history
…`main`
  • Loading branch information
keithamus committed Jan 31, 2019
1 parent 73b01b1 commit 517758d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 60 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -25,20 +25,28 @@ export default {
name: 'MyModule',
plugins: [
resolve({

// the fields to scan in a package.json to determine the entry point..
mainFields: ['module', 'main'], // Default: ['module', 'main']

// DEPRECATED: use `mainFields` instead
// use "module" field for ES6 module if possible
module: true, // Default: true

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

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

// DEPRECATED: use `mainFields` instead
// some package.json files have a `browser` field which
// specifies alternative files to load for people bundling
// for the browser. If that's you, use this option, otherwise
Expand Down
52 changes: 36 additions & 16 deletions src/index.js
Expand Up @@ -37,12 +37,29 @@ function cachedIsFile (file, cb) {
isFileCache[file].then(contents => cb(null, contents), cb);
}

function deprecatedMainField (options, option, mainFields, field = option) {
if (option in options) {
CONSOLE_WARN(`node-resolve: setting options.${option} is deprecated, please override options.mainFields instead`);
if (options[option] === false) {
return mainFields.filter(mainField => mainField === field);
} else if (options[option] === true && mainFields.indexOf(field) === -1) {
return mainFields.concat([field]);
}
}
return mainFields;
}

const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents)));

export default function nodeResolve ( options = {} ) {
const useModule = options.module !== false;
const useMain = options.main !== false;
const useJsnext = options.jsnext === true;
if ('mainFields' in options && ('module' in options || 'main' in options || 'jsnext' in options)) {
throw new Error(`node-resolve: do not use deprecated 'module', 'main', 'jsnext' options with 'mainFields'`);
}
let mainFields = options.mainFields || ['module', 'main'];
mainFields = deprecatedMainField(options, 'browser', mainFields);
mainFields = deprecatedMainField(options, 'module', mainFields);
mainFields = deprecatedMainField(options, 'jsnext', mainFields, 'jsnext:main');
mainFields = deprecatedMainField(options, 'main', mainFields);
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
Expand All @@ -59,8 +76,8 @@ export default function nodeResolve ( options = {} ) {
throw new Error( 'options.skip is no longer supported — you should use the main Rollup `external` option instead' );
}

if ( !useModule && !useMain && !useJsnext ) {
throw new Error( `At least one of options.module, options.main or options.jsnext must be true` );
if ( !mainFields.length ) {
throw new Error( `Please ensure at least one 'mainFields' value is specified` );
}

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

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

if (options.browser && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
if (mainFields.indexOf('browser') !== -1 && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
const browser = browserMapCache[importer];
if (browser[importee] === false || browser[resolvedImportee] === false) {
return ES6_BROWSER_EMPTY;
Expand Down Expand Up @@ -115,7 +132,7 @@ export default function nodeResolve ( options = {} ) {
basedir,
packageFilter ( pkg, pkgPath ) {
const pkgRoot = dirname( pkgPath );
if (options.browser && typeof pkg[ 'browser' ] === 'object') {
if (mainFields.indexOf('browser') !== -1 && typeof pkg[ 'browser' ] === 'object') {
packageBrowserField = Object.keys(pkg[ 'browser' ]).reduce((browser, key) => {
const resolved = pkg[ 'browser' ][ key ] === false ? false : resolve( pkgRoot, pkg[ 'browser' ][ key ] );
browser[ key ] = resolved;
Expand All @@ -133,13 +150,16 @@ export default function nodeResolve ( options = {} ) {
}, {});
}

if (options.browser && typeof pkg[ 'browser' ] === 'string') {
pkg[ 'main' ] = pkg[ 'browser' ];
} else if ( useModule && pkg[ 'module' ] ) {
pkg[ 'main' ] = pkg[ 'module' ];
} else if ( useJsnext && pkg[ 'jsnext:main' ] ) {
pkg[ 'main' ] = pkg[ 'jsnext:main' ];
} else if ( ( useJsnext || useModule ) && !useMain ) {
let overriddenMain = false;
for ( const i in mainFields ) {
const field = mainFields[i];
if ( typeof pkg[ field ] === 'string' ) {
pkg[ 'main' ] = pkg[ field ];
overriddenMain = true;
break;
}
}
if ( overriddenMain === false && mainFields.indexOf( 'main' ) === -1 ) {
disregardResult = true;
}
return pkg;
Expand All @@ -159,7 +179,7 @@ export default function nodeResolve ( options = {} ) {
)
.catch(() => false)
.then(resolved => {
if (options.browser && packageBrowserField) {
if (mainFields.indexOf('browser') !== -1 && packageBrowserField) {
if (packageBrowserField[ resolved ]) {
resolved = packageBrowserField[ resolved ];
}
Expand Down

0 comments on commit 517758d

Please sign in to comment.