module: fix main lookup regression from #18728
PR-URL: https://github.com/nodejs/node/pull/18788 Refs: https://github.com/nodejs/node/pull/18728 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
61e3e6d56f
commit
7748865cd3
@ -544,7 +544,7 @@ const PackageConfig& GetPackageConfig(Environment* env,
|
||||
}
|
||||
|
||||
auto entry = env->package_json_cache.emplace(path,
|
||||
PackageConfig { Exists::Yes, IsValid::Yes, has_main, "" });
|
||||
PackageConfig { Exists::Yes, IsValid::Yes, has_main, main_std });
|
||||
return entry.first->second;
|
||||
}
|
||||
|
||||
@ -585,13 +585,15 @@ Maybe<URL> ResolveMain(Environment* env, const URL& search) {
|
||||
GetPackageConfig(env, pkg.ToFilePath());
|
||||
// Note invalid package.json should throw in resolver
|
||||
// currently we silently ignore which is incorrect
|
||||
if (!pjson.exists || !pjson.is_valid || !pjson.has_main) {
|
||||
if (pjson.exists == Exists::No ||
|
||||
pjson.is_valid == IsValid::No ||
|
||||
pjson.has_main == HasMain::No) {
|
||||
return Nothing<URL>();
|
||||
}
|
||||
if (!ShouldBeTreatedAsRelativeOrAbsolutePath(pjson.main)) {
|
||||
return Resolve(env, "./" + pjson.main, search);
|
||||
return Resolve(env, "./" + pjson.main, search, IgnoreMain);
|
||||
}
|
||||
return Resolve(env, pjson.main, search);
|
||||
return Resolve(env, pjson.main, search, IgnoreMain);
|
||||
}
|
||||
|
||||
Maybe<URL> ResolveModule(Environment* env,
|
||||
@ -602,7 +604,7 @@ Maybe<URL> ResolveModule(Environment* env,
|
||||
do {
|
||||
dir = parent;
|
||||
Maybe<URL> check =
|
||||
Resolve(env, "./node_modules/" + specifier, dir, IgnoreMain);
|
||||
Resolve(env, "./node_modules/" + specifier, dir, CheckMain);
|
||||
if (!check.IsNothing()) {
|
||||
const size_t limit = specifier.find('/');
|
||||
const size_t spec_len =
|
||||
|
6
test/es-module/test-esm-main-lookup.mjs
Normal file
6
test/es-module/test-esm-main-lookup.mjs
Normal file
@ -0,0 +1,6 @@
|
||||
// Flags: --experimental-modules
|
||||
/* eslint-disable required-modules */
|
||||
import assert from 'assert';
|
||||
import main from '../fixtures/es-modules/pjson-main';
|
||||
|
||||
assert.strictEqual(main, 'main');
|
1
test/fixtures/es-modules/pjson-main/main.js
vendored
Normal file
1
test/fixtures/es-modules/pjson-main/main.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
module.exports = 'main';
|
3
test/fixtures/es-modules/pjson-main/package.json
vendored
Normal file
3
test/fixtures/es-modules/pjson-main/package.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"main": "main.js"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user