Skip to content

Commit

Permalink
always encapsulate on exports
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 19, 2019
1 parent 8cca5c9 commit ff11612
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
7 changes: 3 additions & 4 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_).
Expand Down
7 changes: 3 additions & 4 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down
19 changes: 5 additions & 14 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' +
Expand All @@ -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;
Expand Down
19 changes: 4 additions & 15 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,7 @@ Maybe<URL> PackageExportsResolve(Environment* env,
Maybe<URL> 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<URL>();
}
Expand All @@ -1173,15 +1172,11 @@ Maybe<URL> ResolveSelf(Environment* env,
}
}
if (!found_pjson || pcfg->name != pkg_name) return Nothing<URL>();
if (encapsulated && pcfg->exports.IsEmpty()) return Nothing<URL>();
if (pcfg->exports.IsEmpty()) return Nothing<URL>();
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);
}
}

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

Maybe<URL> self_url = ResolveSelf(env, pkg_name, pkg_subpath, base, true);
Maybe<URL> self_url = ResolveSelf(env, pkg_name, pkg_subpath, base);
if (self_url.IsJust()) {
ProcessEmitExperimentalWarning(env, "Package name self resolution");
return self_url;
Expand Down Expand Up @@ -1268,12 +1263,6 @@ Maybe<URL> 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());
Expand Down

0 comments on commit ff11612

Please sign in to comment.