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
rkt2spc
wants to merge
1
commit into
parcel-bundler:master
from
rkt2spc:feature/change-get-package-main-logic
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-L191There was a problem hiding this comment.
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.