From 5ff121c431346e368e60f7ca256b720ddf06861d Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Tue, 24 Apr 2018 09:33:17 +0700 Subject: [PATCH] Update getPackageMain logic to retrieve the first entry that exists --- src/Resolver.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Resolver.js b/src/Resolver.js index 64a33ff1512..495ea1767a1 100644 --- a/src/Resolver.js +++ b/src/Resolver.js @@ -185,7 +185,7 @@ class Resolver { pkg = await this.readPackage(dir); // First try loading package.main as a file, then try as a directory. - let main = this.getPackageMain(pkg); + let main = await this.getPackageMain(pkg); let res = (await this.loadAsFile(main, extensions, pkg)) || (await this.loadDirectory(main, extensions, pkg)); @@ -217,19 +217,29 @@ class Resolver { return pkg; } - getPackageMain(pkg) { + async getPackageMain(pkg) { // libraries like d3.js specifies node.js specific files in the "main" which breaks the build // we use the "module" or "jsnext:main" field to get the full dependency tree if available - let main = [pkg.module, pkg['jsnext:main'], pkg.browser, pkg.main].find( - entry => typeof entry === 'string' - ); + const entries = [pkg.module, pkg['jsnext:main'], pkg.browser, pkg.main] + .filter(entry => typeof entry === 'string') + .map(entry => path.resolve(pkg.pkgdir, entry)); + + // main is the first entry that exists + for (let i = 0; i < entries.length; ++i) { + const entry = entries[i]; - // Default to index file if no main field find - if (!main || main === '.' || main === './') { - main = 'index'; + try { + const stat = await fs.stat(entry); + if (stat.isDirectory()) return path.resolve(entry, 'index'); + return entry; + } catch (e) { + // Entry doesn't exist + continue; + } } - return path.resolve(pkg.pkgdir, main); + // fallback to default index if no main can be found + return path.resolve(pkg.pkgdir, 'index'); } async loadAsFile(file, extensions, pkg) {