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

Commit

Permalink
Use async fs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulf Nilsson committed Jun 26, 2019
1 parent 4b1e355 commit bb97e3f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/index.js
Expand Up @@ -13,8 +13,12 @@ const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
// which deploy both ESM .mjs and CommonJS .js files as ESM.
const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];

const existsAsync = file => new Promise(fulfil => fs.exists(file, fulfil));

const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));

const realpathAsync = file => new Promise((fulfil, reject) => fs.realpath(file, (err, contents) => err ? reject(err) : fulfil(contents)));

const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));

const cache = fn => {
Expand Down Expand Up @@ -306,11 +310,14 @@ export default function nodeResolve ( options = {} ) {
browserMapCache.set(resolved, packageBrowserField);
}

if ( hasPackageEntry && !preserveSymlinks && resolved ) {
return existsAsync( resolved )
.then(exists => exists ? realpathAsync( resolved ) : resolved);
}
return resolved;
})
.then(resolved => {
if ( hasPackageEntry ) {
if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) {
resolved = fs.realpathSync( resolved );
}

if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
return null;
} else if (importeeIsBuiltin && preferBuiltins) {
Expand Down

0 comments on commit bb97e3f

Please sign in to comment.