-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
esm: resolve main from package.json when importing from directory #32612
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -587,12 +587,35 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |
| return false; | ||
| } | ||
|
|
||
| function resolveRelativePackage(specifier /* string */, base /* URL */) { | ||
| const resolved = new URL(specifier, base); | ||
| const stat = tryStatSync(resolved); | ||
| if (stat.isDirectory()) { | ||
| let packageJSONPath; | ||
| if (!StringPrototypeEndsWith(resolved.pathname, '/')) { | ||
| packageJSONPath = `${resolved.pathname}/package.json`; | ||
| } else { | ||
| packageJSONPath = `${resolved.pathname}package.json`; | ||
| } | ||
| const packageJSONUrl = new URL(packageJSONPath, base); | ||
| packageJSONPath = fileURLToPath(packageJSONUrl); | ||
| const packageConfig = getPackageConfig(packageJSONPath, base); | ||
| return packageMainResolve(packageJSONUrl, packageConfig, base); | ||
| } | ||
| return new URL(specifier, base); | ||
| } | ||
|
Contributor
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 this should probably combined into the
Contributor
Author
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. The first version I wrote puts this part in
Contributor
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. It is ok for
Contributor
Author
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. Sorry for my late reply.
Contributor
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. @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:
|
||
|
|
||
| function moduleResolve(specifier /* string */, base /* URL */) { /* -> URL */ | ||
| // Order swapped from spec for minor perf gain. | ||
| // Ok since relative URLs cannot parse as URLs. | ||
| let resolved; | ||
| if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | ||
| resolved = new URL(specifier, base); | ||
| if (getOptionValue('--experimental-specifier-resolution') === 'node') { | ||
| // experimental-specifier-resolution also allows importing from directory | ||
| resolved = resolveRelativePackage(specifier, base); | ||
| } else { | ||
| resolved = new URL(specifier, base); | ||
| } | ||
| } else { | ||
| try { | ||
| resolved = new URL(specifier); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const identifier = 'package-with-exports'; | ||
| console.log(identifier); | ||
|
lingsamuel marked this conversation as resolved.
Outdated
|
||
| module.exports = identifier; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "main": "main.js", | ||
| "exports": { | ||
| ".": "./exports_main.js" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const identifier = 'package-with-main'; | ||
| console.log(identifier); | ||
| module.exports = identifier; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "main": "main.js" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.