Skip to content

Commit ff11612

Browse files
committed
always encapsulate on exports
1 parent 8cca5c9 commit ff11612

File tree

4 files changed

+15
-37
lines changed

4 files changed

+15
-37
lines changed

doc/api/esm.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,15 +1237,14 @@ _defaultEnv_ is the conditional environment name priority array,
12371237
> 1. If _selfUrl_ isn't empty, return _selfUrl_.
12381238
> 1. Throw a _Module Not Found_ error.
12391239
1240-
**SELF_REFERENCE_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_,
1241-
_encapsulated_)
1240+
**SELF_REFERENCE_RESOLVE**(_packageName_, _packageSubpath_, _parentURL_)
12421241
12431242
> 1. Let _packageURL_ be the result of **READ_PACKAGE_SCOPE**(_parentURL_).
12441243
> 1. If _packageURL_ is **null**, then
12451244
> 1. Return **undefined**.
12461245
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_packageURL_).
1247-
> 1. If _encapsulated_ is **true** and _pjson_ does not include an
1248-
> _"exports"_ property, then return **undefined**.
1246+
> 1. If _pjson_ does not include an _"exports"_ property, then
1247+
> 1. Return **undefined**.
12491248
> 1. If _pjson.name_ is equal to _packageName_, then
12501249
> 1. If _packageSubpath_ is _undefined_, then
12511250
> 1. Return the result of **PACKAGE_MAIN_RESOLVE**(_packageURL_, _pjson_).

doc/api/modules.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,8 @@ require(X) from module at path Y
160160
a. LOAD_AS_FILE(Y + X)
161161
b. LOAD_AS_DIRECTORY(Y + X)
162162
c. THROW "not found"
163-
4. LOAD_SELF_REFERENCE(X, dirname(Y), true)
163+
4. LOAD_SELF_REFERENCE(X, dirname(Y))
164164
5. LOAD_NODE_MODULES(X, dirname(Y))
165-
6. LOAD_SELF_REFERENCE(X, dirname(Y), false)
166165
7. THROW "not found"
167166
168167
LOAD_AS_FILE(X)
@@ -204,10 +203,10 @@ NODE_MODULES_PATHS(START)
204203
d. let I = I - 1
205204
5. return DIRS
206205
207-
LOAD_SELF_REFERENCE(X, START, ENCAPSULATED)
206+
LOAD_SELF_REFERENCE(X, START)
208207
1. Find the closest package scope to START.
209208
2. If no scope was found, return.
210-
3. If ENCAPSULATED is true and the `package.json` has no "exports", return.
209+
3. If the `package.json` has no "exports", return.
211210
4. If the name in `package.json` isn't a prefix of X, throw "not found".
212211
5. Otherwise, resolve the remainder of X relative to this package as if it
213212
was loaded via `LOAD_NODE_MODULES` with a name in `package.json`.

lib/internal/modules/cjs/loader.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,13 @@ function resolveBasePath(basePath, exts, isMain, trailingSlash, request) {
431431
return filename;
432432
}
433433

434-
function trySelf(parentPath, isMain, request, encapsulated) {
434+
function trySelf(parentPath, isMain, request) {
435435
if (!experimentalSelf) {
436436
return false;
437437
}
438438

439439
const { data: pkg, path: basePath } = readPackageScope(parentPath) || {};
440-
if (!pkg || (encapsulated && 'exports' in pkg === false)) return false;
440+
if (!pkg || 'exports' in pkg === false) return false;
441441
if (typeof pkg.name !== 'string') return false;
442442

443443
let expansion;
@@ -1002,9 +1002,8 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10021002
paths = Module._resolveLookupPaths(request, parent);
10031003
}
10041004

1005-
// Look up the filename first, since that's the cache key.
10061005
if (parent && parent.filename) {
1007-
const filename = trySelf(parent.filename, isMain, request, true);
1006+
const filename = trySelf(parent.filename, isMain, request);
10081007
if (filename) {
10091008
emitExperimentalWarning('Package name self resolution');
10101009
const cacheKey = request + '\x00' +
@@ -1013,18 +1012,10 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10131012
return filename;
10141013
}
10151014
}
1015+
1016+
// Look up the filename first, since that's the cache key.
10161017
const filename = Module._findPath(request, paths, isMain, false);
10171018
if (filename) return filename;
1018-
if (parent && parent.filename) {
1019-
const filename = trySelf(parent.filename, isMain, request);
1020-
if (filename) {
1021-
emitExperimentalWarning('Package name self resolution');
1022-
const cacheKey = request + '\x00' +
1023-
(paths.length === 1 ? paths[0] : paths.join('\x00'));
1024-
Module._pathCache[cacheKey] = filename;
1025-
return filename;
1026-
}
1027-
}
10281019
const requireStack = [];
10291020
for (let cursor = parent;
10301021
cursor;

src/module_wrap.cc

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,7 @@ Maybe<URL> PackageExportsResolve(Environment* env,
11521152
Maybe<URL> ResolveSelf(Environment* env,
11531153
const std::string& pkg_name,
11541154
const std::string& pkg_subpath,
1155-
const URL& base,
1156-
bool encapsulated) {
1155+
const URL& base) {
11571156
if (!env->options()->experimental_resolve_self) {
11581157
return Nothing<URL>();
11591158
}
@@ -1173,15 +1172,11 @@ Maybe<URL> ResolveSelf(Environment* env,
11731172
}
11741173
}
11751174
if (!found_pjson || pcfg->name != pkg_name) return Nothing<URL>();
1176-
if (encapsulated && pcfg->exports.IsEmpty()) return Nothing<URL>();
1175+
if (pcfg->exports.IsEmpty()) return Nothing<URL>();
11771176
if (!pkg_subpath.length()) {
11781177
return PackageMainResolve(env, pjson_url, *pcfg, base);
11791178
} else {
1180-
if (!pcfg->exports.IsEmpty()) {
1181-
return PackageExportsResolve(env, pjson_url, pkg_subpath, *pcfg, base);
1182-
} else {
1183-
return FinalizeResolution(env, URL(pkg_subpath, pjson_url), base);
1184-
}
1179+
return PackageExportsResolve(env, pjson_url, pkg_subpath, *pcfg, base);
11851180
}
11861181
}
11871182

@@ -1229,7 +1224,7 @@ Maybe<URL> PackageResolve(Environment* env,
12291224
pkg_subpath = "." + specifier.substr(sep_index);
12301225
}
12311226

1232-
Maybe<URL> self_url = ResolveSelf(env, pkg_name, pkg_subpath, base, true);
1227+
Maybe<URL> self_url = ResolveSelf(env, pkg_name, pkg_subpath, base);
12331228
if (self_url.IsJust()) {
12341229
ProcessEmitExperimentalWarning(env, "Package name self resolution");
12351230
return self_url;
@@ -1268,12 +1263,6 @@ Maybe<URL> PackageResolve(Environment* env,
12681263
// Cross-platform root check.
12691264
} while (pjson_path.length() != last_path.length());
12701265

1271-
self_url = ResolveSelf(env, pkg_name, pkg_subpath, base, false);
1272-
if (self_url.IsJust()) {
1273-
ProcessEmitExperimentalWarning(env, "Package name self resolution");
1274-
return self_url;
1275-
}
1276-
12771266
std::string msg = "Cannot find package '" + pkg_name +
12781267
"' imported from " + base.ToFilePath();
12791268
node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());

0 commit comments

Comments
 (0)