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

esm: resolve main from package.json when importing from directory #32612

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 resolved;
}
Copy link
Contributor

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.

Copy link
Contributor Author

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 think finalizeResolution is LOAD_AS_FILE routine, it is called by many other resolve functions as final return call and shouldn't add any resolve functionality. So I move this code pieces after the relative path check to fit this rule:

  1. If X begins with './' or '/' or '../'
    a. LOAD_AS_FILE(Y + X)
    b. LOAD_AS_DIRECTORY(Y + X)`

Copy link
Contributor

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 the LOAD_AS_FILE | LOAD_AS_DIRECTORY path - the reason for this is that all resolutions eventually end in this check in CommonJS.

Copy link
Contributor Author

@lingsamuel lingsamuel Apr 13, 2020

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...

Copy link
Contributor

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:

  1. import 'pkg/x' should load /path/to/node_modules/pkg/x/pjson-main.js with package.json main checks
  2. import 'pkg/x' where pkg/package.json contains { "exports": { "./x": "./z" } should load /path/to/node_modules/pkg/z/pjson-main.js


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);
Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-esm-specifiers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import { fileURLToPath } from 'url';
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
// esm index.js
import module from '../fixtures/es-module-specifiers/package-type-module';
// esm main.js
import pkg_main from '../fixtures/es-module-specifiers/package-with-main';
// esm exports_main.js
import pkg_export from '../fixtures/es-module-specifiers/package-with-exports';
// Notice the trailing slash
import success, { explicit, implicit, implicitModule, getImplicitCommonjs }
from '../fixtures/es-module-specifiers/';

assert.strictEqual(commonjs, 'commonjs');
assert.strictEqual(module, 'module');
assert.strictEqual(pkg_main, 'package-with-main');
assert.strictEqual(pkg_export, 'package-with-exports');
assert.strictEqual(success, 'success');
assert.strictEqual(explicit, 'esm');
assert.strictEqual(implicit, 'cjs');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const identifier = 'package-with-exports';
module.exports = identifier;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"main": "main.js",
"exports": {
".": "./exports_main.js"
}
}
2 changes: 2 additions & 0 deletions test/fixtures/es-module-specifiers/package-with-main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const identifier = 'package-with-main';
module.exports = identifier;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "main.js"
}