diff --git a/doc/api/esm.md b/doc/api/esm.md index be8f867cbea16e..d149c743989974 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -1237,15 +1237,14 @@ _defaultEnv_ is the conditional environment name priority array, > 1. If _selfUrl_ isn't empty, return _selfUrl_. > 1. Throw a _Module Not Found_ error. -**SELF_REFERENCE_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_, - _encapsulated_) +**SELF_REFERENCE_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_) > 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_). > 1. If _packageURL_ is **null**, then > 1. Return **undefined**. > 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_). -> 1. If _encapsulated_ is **true** and _pjson_ does not include an -> _"exports"_ property, then return **undefined**. +> 1. If _pjson_ does not include an _"exports"_ property, then +> 1. Return **undefined**. > 1. If _pjson.name_ is equal to _packageName_, then > 1. If _packageSubpath_ is _undefined_, then > 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_). diff --git a/doc/api/modules.md b/doc/api/modules.md index 3864749e90b92f..b57e5890379d18 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -160,9 +160,8 @@ require(X) from module at path Y a. LOAD_AS_FILE(Y + X) b. LOAD_AS_DIRECTORY(Y + X) c. THROW "not found" -4. LOAD_SELF_REFERENCE(X, dirname(Y), true) +4. LOAD_SELF_REFERENCE(X, dirname(Y)) 5. LOAD_NODE_MODULES(X, dirname(Y)) -6. LOAD_SELF_REFERENCE(X, dirname(Y), false) 7. THROW "not found" LOAD_AS_FILE(X) @@ -204,10 +203,10 @@ NODE_MODULES_PATHS(START) d. let I = I - 1 5. return DIRS -LOAD_SELF_REFERENCE(X, START, ENCAPSULATED) +LOAD_SELF_REFERENCE(X, START) 1. Find the closest package scope to START. 2. If no scope was found, return. -3. If ENCAPSULATED is true and the `package.json` has no "exports", return. +3. If the `package.json` has no "exports", return. 4. If the name in `package.json` isn't a prefix of X, throw "not found". 5. Otherwise, resolve the remainder of X relative to this package as if it was loaded via `LOAD_NODE_MODULES` with a name in `package.json`. diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index e2eb83dade0bf9..19cbc7ab54d653 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -431,13 +431,13 @@ function resolveBasePath(basePath, exts, isMain, trailingSlash, request) { return filename; } -function trySelf(parentPath, isMain, request, encapsulated) { +function trySelf(parentPath, isMain, request) { if (!experimentalSelf) { return false; } const { data: pkg, path: basePath } = readPackageScope(parentPath) || {}; - if (!pkg || (encapsulated && 'exports' in pkg === false)) return false; + if (!pkg || 'exports' in pkg === false) return false; if (typeof pkg.name !== 'string') return false; let expansion; @@ -1002,9 +1002,8 @@ Module._resolveFilename = function(request, parent, isMain, options) { paths = Module._resolveLookupPaths(request, parent); } - // Look up the filename first, since that's the cache key. if (parent && parent.filename) { - const filename = trySelf(parent.filename, isMain, request, true); + const filename = trySelf(parent.filename, isMain, request); if (filename) { emitExperimentalWarning('Package name self resolution'); const cacheKey = request + '\x00' + @@ -1013,18 +1012,10 @@ Module._resolveFilename = function(request, parent, isMain, options) { return filename; } } + + // Look up the filename first, since that's the cache key. const filename = Module._findPath(request, paths, isMain, false); if (filename) return filename; - if (parent && parent.filename) { - const filename = trySelf(parent.filename, isMain, request); - if (filename) { - emitExperimentalWarning('Package name self resolution'); - const cacheKey = request + '\x00' + - (paths.length === 1 ? paths[0] : paths.join('\x00')); - Module._pathCache[cacheKey] = filename; - return filename; - } - } const requireStack = []; for (let cursor = parent; cursor; diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 66a1e33633656d..454476a1e97815 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -1152,8 +1152,7 @@ Maybe PackageExportsResolve(Environment* env, Maybe ResolveSelf(Environment* env, const std::string& pkg_name, const std::string& pkg_subpath, - const URL& base, - bool encapsulated) { + const URL& base) { if (!env->options()->experimental_resolve_self) { return Nothing(); } @@ -1173,15 +1172,11 @@ Maybe ResolveSelf(Environment* env, } } if (!found_pjson || pcfg->name != pkg_name) return Nothing(); - if (encapsulated && pcfg->exports.IsEmpty()) return Nothing(); + if (pcfg->exports.IsEmpty()) return Nothing(); if (!pkg_subpath.length()) { return PackageMainResolve(env, pjson_url, *pcfg, base); } else { - if (!pcfg->exports.IsEmpty()) { - return PackageExportsResolve(env, pjson_url, pkg_subpath, *pcfg, base); - } else { - return FinalizeResolution(env, URL(pkg_subpath, pjson_url), base); - } + return PackageExportsResolve(env, pjson_url, pkg_subpath, *pcfg, base); } } @@ -1229,7 +1224,7 @@ Maybe PackageResolve(Environment* env, pkg_subpath = "." + specifier.substr(sep_index); } - Maybe self_url = ResolveSelf(env, pkg_name, pkg_subpath, base, true); + Maybe self_url = ResolveSelf(env, pkg_name, pkg_subpath, base); if (self_url.IsJust()) { ProcessEmitExperimentalWarning(env, "Package name self resolution"); return self_url; @@ -1268,12 +1263,6 @@ Maybe PackageResolve(Environment* env, // Cross-platform root check. } while (pjson_path.length() != last_path.length()); - self_url = ResolveSelf(env, pkg_name, pkg_subpath, base, false); - 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());