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

Use async fs methods #230

Merged
merged 1 commit into from Jun 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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