Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup resolution bugs
  • Loading branch information
guybedford committed Aug 5, 2020
commit 5dc585dd669a9a0b959afb292d6f5658dc95bac2
52 changes: 32 additions & 20 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const manifest = getOptionValue('--experimental-policy') ?
require('internal/process/policy').manifest :
null;
const { compileFunction } = internalBinding('contextify');
const userConditions = (getOptionValue('--conditions') || '').split(',');
const userConditions = getOptionValue('--conditions') &&
getOptionValue('--conditions').split(',');

// Whether any user-provided CJS modules had been loaded (executed).
// Used for internal assertions.
Expand Down Expand Up @@ -498,8 +499,12 @@ function applyExports(basePath, expansion) {
if (typeof pkgExports === 'object') {
if (ObjectPrototypeHasOwnProperty(pkgExports, mappingKey)) {
const mapping = pkgExports[mappingKey];
return resolveExportsTarget(pathToFileURL(basePath + '/'), mapping, '',
mappingKey);
const resolved = resolveExportsTarget(
pathToFileURL(basePath + '/'), mapping, '', mappingKey);
if (resolved === null || resolved === undefined)
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
basePath, mappingKey);
return resolved;
}

let dirMatch = '';
Expand All @@ -516,6 +521,9 @@ function applyExports(basePath, expansion) {
const subpath = StringPrototypeSlice(mappingKey, dirMatch.length);
const resolved = resolveExportsTarget(pathToFileURL(basePath + '/'),
mapping, subpath, mappingKey);
if (resolved === null || resolved === undefined)
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
basePath, mappingKey + subpath);
// Extension searching for folder exports only
const rc = stat(resolved);
if (rc === 0) return resolved;
Expand Down Expand Up @@ -603,21 +611,29 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
throw new ERR_INVALID_MODULE_SPECIFIER(mappingKey + subpath, reason);
} else if (ArrayIsArray(target)) {
if (target.length === 0)
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
baseUrl.pathname, mappingKey + subpath);
return null;
let lastException;
for (const targetValue of target) {
let resolved;
try {
return resolveExportsTarget(baseUrl, targetValue, subpath, mappingKey);
resolved = resolveExportsTarget(baseUrl, targetValue, subpath,
mappingKey);
} catch (e) {
lastException = e;
if (e.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
e.code !== 'ERR_INVALID_PACKAGE_TARGET')
if (e.code !== 'ERR_INVALID_PACKAGE_TARGET')
throw e;
}
if (resolved === undefined)
continue;
if (resolved === null) {
lastException = null;
continue;
}
return resolved;
}
// Throw last fallback error
assert(lastException !== undefined);
if (lastException === undefined || lastException === null)
return lastException;
throw lastException;
} else if (typeof target === 'object' && target !== null) {
const keys = ObjectKeys(target);
Expand All @@ -627,20 +643,16 @@ function resolveExportsTarget(baseUrl, target, subpath, mappingKey) {
}
for (const p of keys) {
if (cjsConditions.has(p) || p === 'default') {
try {
return resolveExportsTarget(baseUrl, target[p], subpath,
mappingKey);
} catch (e) {
if (e.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') throw e;
}
break;
const resolved = resolveExportsTarget(baseUrl, target[p], subpath,
mappingKey);
if (resolved === undefined)
continue;
return resolved;
}
}
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
baseUrl.pathname, mappingKey + subpath);
return undefined;
} else if (target === null) {
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(
baseUrl.pathname, mappingKey + subpath);
return null;
}
throw new ERR_INVALID_PACKAGE_TARGET(baseUrl.pathname, mappingKey, target);
}
Expand Down
10 changes: 4 additions & 6 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const {
const { Module: CJSModule } = require('internal/modules/cjs/loader');

const packageJsonReader = require('internal/modules/package_json_reader');
const userConditions = (getOptionValue('--conditions') || '').split(',');
const userConditions = getOptionValue('--conditions') &&
getOptionValue('--conditions').split(',');
const DEFAULT_CONDITIONS = ObjectFreeze(['node', 'import', ...userConditions]);
const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);

Expand Down Expand Up @@ -360,12 +361,9 @@ function isArrayIndex(key) {
function resolvePackageTarget(
packageJSONUrl, target, subpath, packageSubpath, base, internal, conditions) {
if (typeof target === 'string') {
const resolved = resolvePackageTargetString(
return finalizeResolution(resolvePackageTargetString(
target, subpath, packageSubpath, packageJSONUrl, base, internal,
conditions);
if (resolved === null)
return null;
return finalizeResolution(resolved, base);
conditions), base);
} else if (ArrayIsArray(target)) {
if (target.length === 0)
return null;
Expand Down