-
Notifications
You must be signed in to change notification settings - Fork 30k
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
esm: resolve main from package.json when importing from directory #32612
Closed
lingsamuel
wants to merge
3
commits into
nodejs:master
from
lingsamuel:fix-experimental-specifier-resolution-import-dir
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
2 changes: 2 additions & 0 deletions
2
test/fixtures/es-module-specifiers/package-with-exports/exports_main.js
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const identifier = 'package-with-exports'; | ||
module.exports = identifier; |
6 changes: 6 additions & 0 deletions
6
test/fixtures/es-module-specifiers/package-with-exports/package.json
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"main": "main.js", | ||
"exports": { | ||
".": "./exports_main.js" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const identifier = 'package-with-main'; | ||
module.exports = identifier; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-module-specifiers/package-with-main/package.json
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"main": "main.js" | ||
} |
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.
I think this should probably combined into the
finalizeResolution
function above rather to maintain full equivalence with the LOAD_AS_FILE | LOAD_AS_DIRECTORY checks per the module resolution spec.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.
The first version I wrote puts this part in
finalizeResolution
function indeed. But I thinkfinalizeResolution
isLOAD_AS_FILE
routine, it is called by many other resolve functions as final return call and shouldn't add anyresolve
functionality. So I move this code pieces after the relative path check to fit this rule: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.
It is ok for
finalizeResolution
to include theLOAD_AS_FILE | LOAD_AS_DIRECTORY
path - the reason for this is that all resolutions eventually end in this check in CommonJS.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.
Sorry for my late reply.
I don't think it's a good idea...
finalizeResolution
is pretty simple now, and keep it simple is better to my mind. Merge package resolve logic to it makes it a bit dirty, the call path does not straight anymore.But if more people think merge logic is better, I will change it...
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.
@lingsamuel let me restate my feedback then. This PR only fixes one case, when there are two other test cases not being covered by this PR that you should make pass as well as part of this fix:
import 'pkg/x'
should load/path/to/node_modules/pkg/x/pjson-main.js
with package.json main checksimport 'pkg/x'
wherepkg/package.json
contains{ "exports": { "./x": "./z" }
should load/path/to/node_modules/pkg/z/pjson-main.js