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

Commit

Permalink
Fix pkg.browser mappings issue by specifying a value of false
Browse files Browse the repository at this point in the history
  • Loading branch information
allex committed Feb 20, 2019
1 parent 73b01b1 commit e4ed418
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function cachedIsFile (file, cb) {

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

const FALSE = {};

export default function nodeResolve ( options = {} ) {
const useModule = options.module !== false;
const useMain = options.main !== false;
Expand Down Expand Up @@ -82,6 +84,7 @@ export default function nodeResolve ( options = {} ) {

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

// https://github.com/defunctzombie/package-browser-field-spec
if (options.browser && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
const browser = browserMapCache[importer];
Expand Down Expand Up @@ -157,16 +160,28 @@ export default function nodeResolve ( options = {} ) {
importee,
Object.assign( resolveOptions, customResolveOptions )
)
.catch(() => false)
.catch(() => FALSE)
.then(resolved => {
if (options.browser && packageBrowserField) {
if (packageBrowserField[ resolved ]) {
resolved = packageBrowserField[ resolved ];
if (resolved !== FALSE && options.browser && packageBrowserField) {
let l;
const hasLink = packageBrowserField.hasOwnProperty(resolved);
if (hasLink) {
l = packageBrowserField[ resolved ];
}
browserMapCache[l || resolved] = packageBrowserField;
if (hasLink) {
resolved = l;
}
if (resolved === false) {
return ES6_BROWSER_EMPTY;
}
browserMapCache[resolved] = packageBrowserField;
}

if ( !disregardResult && resolved !== false ) {
if (resolved === FALSE) {
return null;
}

if ( !disregardResult ) {
if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) {
resolved = fs.realpathSync( resolved );
}
Expand All @@ -190,7 +205,7 @@ export default function nodeResolve ( options = {} ) {
if ( resolved && options.modulesOnly ) {
return readFileAsync( resolved, 'utf-8').then(code => isModule( code ) ? resolved : null);
} else {
return resolved === false ? null : resolved;
return resolved;
}
});
}
Expand Down

0 comments on commit e4ed418

Please sign in to comment.