Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! loader: return package format from…
Browse files Browse the repository at this point in the history
… defaultResolve if known
  • Loading branch information
dygabo committed Dec 5, 2021
1 parent 33a4572 commit 3894a73
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,11 @@ function packageExportsResolve(
const resolveResult = resolvePackageTarget(
packageJSONUrl, target, '', packageSubpath, base, false, false, conditions
);
if (resolveResult === null || resolveResult === undefined)

if (resolveResult == null) {
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
}

return resolveResult;
}

Expand Down Expand Up @@ -691,12 +694,19 @@ function packageExportsResolve(

if (bestMatch) {
const target = exports[bestMatch];
const resolveResult = resolvePackageTarget(packageJSONUrl, target,
bestMatchSubpath, bestMatch,
base, true, false,
conditions);
if (resolveResult === null || resolveResult === undefined)
const resolveResult = resolvePackageTarget(
packageJSONUrl,
target,
bestMatchSubpath,
bestMatch,
base,
true,
false,
conditions);

if (resolveResult == null) {
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
}
return resolveResult;
}

Expand Down Expand Up @@ -740,8 +750,9 @@ function packageImportsResolve(name, base, conditions) {
const resolveResult = resolvePackageTarget(
packageJSONUrl, imports[name], '', name, base, false, true, conditions
);
if (resolveResult !== null && resolveResult !== undefined)
if (resolveResult != null) {
return resolveResult.resolved;
}
} else {
let bestMatch = '';
let bestMatchSubpath;
Expand Down Expand Up @@ -771,8 +782,9 @@ function packageImportsResolve(name, base, conditions) {
bestMatchSubpath,
bestMatch, base, true,
true, conditions);
if (resolveResult !== null && resolveResult !== undefined)
if (resolveResult != null) {
return resolveResult.resolved;
}
}
}
}
Expand Down Expand Up @@ -875,12 +887,14 @@ function packageResolve(specifier, base, conditions) {
}
if (packageSubpath === '.') {
return {
resolved: legacyMainResolve(packageJSONUrl,
packageConfig,
base),
resolved: legacyMainResolve(
packageJSONUrl,
packageConfig,
base),
...(packageConfig.type !== 'none') && { format: packageConfig.type }
};
}

return {
resolved: new URL(packageSubpath, packageJSONUrl),
...(packageConfig.type !== 'none') && { format: packageConfig.type }
Expand Down Expand Up @@ -927,7 +941,8 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
function moduleResolve(specifier, base, conditions, preserveSymlinks) {
// Order swapped from spec for minor perf gain.
// Ok since relative URLs cannot parse as URLs.
let resolved, format;
let resolved;
let format;
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
resolved = new URL(specifier, base);
} else if (specifier[0] === '#') {
Expand All @@ -939,11 +954,15 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
({ resolved, format } = packageResolve(specifier, base, conditions));
}
}
if (resolved.protocol !== 'file:')
return { url: resolved };
if (resolved.protocol !== 'file:') {
return {
url: resolved
};
}

return {
url: finalizeResolution(resolved, base, preserveSymlinks),
...(format !== undefined && format !== null) && { format: format }
...(format != null) && { format }
};
}

Expand Down Expand Up @@ -1032,11 +1051,16 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
}

conditions = getConditionsSet(conditions);
let url, format;
let url;
let format;
try {
({ url, format } =
moduleResolve(specifier, parentURL, conditions,
isMain ? preserveSymlinksMain : preserveSymlinks));
moduleResolve(
specifier,
parentURL,
conditions,
isMain ? preserveSymlinksMain : preserveSymlinks
));
} catch (error) {
// Try to give the user a hint of what would have been the
// resolved CommonJS module
Expand Down Expand Up @@ -1066,7 +1090,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {

return {
url: `${url}`,
...(format !== undefined && format !== null) && { format: format }
...(format != null) && { format }
};
}

Expand Down

0 comments on commit 3894a73

Please sign in to comment.