Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getPackageMain logic to retrieve the first entry that exists #1237

Closed
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
28 changes: 19 additions & 9 deletions src/Resolver.js
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of doing the actual resolution here (and then duplicating it again later), we could just have getPackageMain return an array of possible paths. Then, loadDirectory could just loop through them and try each one: https://github.com/parcel-bundler/parcel/blob/master/src/Resolver.js#L188-L191

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but I'm a little bit busy right now. I'll come back and take a deeper look into this next month.

if (stat.isDirectory()) return path.resolve(entry, 'index');
return entry;
} catch (e) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just blow-up here if an entry record declared in package.json doesn't exists instead of handling it.

// 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) {
Expand Down