diff options
author | Guy Bedford <guybedford@gmail.com> | 2019-12-17 09:58:19 +0300 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2020-01-03 18:22:04 +0300 |
commit | bd047e827719805a709f62bddcf627634ef64440 (patch) | |
tree | 94d81211073aa2154d7a58eaaa3b3a8c58dcb5a9 /src/module_wrap.cc | |
parent | 4f32bbb8160434a6f7ccc09b56d3d87ec25db7f9 (diff) |
module: self resolve bug fix and esm ordering
PR-URL: https://github.com/nodejs/node/pull/31009
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Diffstat (limited to 'src/module_wrap.cc')
-rw-r--r-- | src/module_wrap.cc | 49 |
1 files changed, 14 insertions, 35 deletions
diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 568e1ad2fdb..454476a1e97 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -1150,12 +1150,12 @@ Maybe<URL> PackageExportsResolve(Environment* env, } Maybe<URL> ResolveSelf(Environment* env, - const std::string& specifier, + const std::string& pkg_name, + const std::string& pkg_subpath, const URL& base) { if (!env->options()->experimental_resolve_self) { return Nothing<URL>(); } - const PackageConfig* pcfg; if (GetPackageScopeConfig(env, base, base).To(&pcfg) && pcfg->exists == Exists::Yes) { @@ -1171,34 +1171,12 @@ Maybe<URL> ResolveSelf(Environment* env, found_pjson = true; } } - - if (!found_pjson) { - return Nothing<URL>(); - } - - // "If specifier starts with pcfg name" - std::string subpath; - if (specifier.rfind(pcfg->name, 0)) { - // We know now: specifier is either equal to name or longer. - if (specifier == subpath) { - subpath = ""; - } else if (specifier[pcfg->name.length()] == '/') { - // Return everything after the slash - subpath = "." + specifier.substr(pcfg->name.length() + 1); - } else { - // The specifier is neither the name of the package nor a subpath of it - return Nothing<URL>(); - } - } - - if (found_pjson && !subpath.length()) { + if (!found_pjson || pcfg->name != pkg_name) return Nothing<URL>(); + if (pcfg->exports.IsEmpty()) return Nothing<URL>(); + if (!pkg_subpath.length()) { return PackageMainResolve(env, pjson_url, *pcfg, base); - } else if (found_pjson) { - if (!pcfg->exports.IsEmpty()) { - return PackageExportsResolve(env, pjson_url, subpath, *pcfg, base); - } else { - return FinalizeResolution(env, URL(subpath, pjson_url), base); - } + } else { + return PackageExportsResolve(env, pjson_url, pkg_subpath, *pcfg, base); } } @@ -1245,6 +1223,13 @@ Maybe<URL> PackageResolve(Environment* env, } else { pkg_subpath = "." + specifier.substr(sep_index); } + + Maybe<URL> self_url = ResolveSelf(env, pkg_name, pkg_subpath, base); + if (self_url.IsJust()) { + ProcessEmitExperimentalWarning(env, "Package name self resolution"); + return self_url; + } + URL pjson_url("./node_modules/" + pkg_name + "/package.json", &base); std::string pjson_path = pjson_url.ToFilePath(); std::string last_path; @@ -1278,12 +1263,6 @@ Maybe<URL> PackageResolve(Environment* env, // Cross-platform root check. } while (pjson_path.length() != last_path.length()); - Maybe<URL> self_url = ResolveSelf(env, specifier, base); - if (self_url.IsJust()) { - ProcessEmitExperimentalWarning(env, "Package name self resolution"); - return self_url; - } - std::string msg = "Cannot find package '" + pkg_name + "' imported from " + base.ToFilePath(); node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str()); |