Skip to content

Commit 3894a73

Browse files
committed
fixup! fixup! fixup! fixup! fixup! loader: return package format from defaultResolve if known
1 parent 33a4572 commit 3894a73

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

lib/internal/modules/esm/resolve.js

+43-19
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,11 @@ function packageExportsResolve(
653653
const resolveResult = resolvePackageTarget(
654654
packageJSONUrl, target, '', packageSubpath, base, false, false, conditions
655655
);
656-
if (resolveResult === null || resolveResult === undefined)
656+
657+
if (resolveResult == null) {
657658
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
659+
}
660+
658661
return resolveResult;
659662
}
660663

@@ -691,12 +694,19 @@ function packageExportsResolve(
691694

692695
if (bestMatch) {
693696
const target = exports[bestMatch];
694-
const resolveResult = resolvePackageTarget(packageJSONUrl, target,
695-
bestMatchSubpath, bestMatch,
696-
base, true, false,
697-
conditions);
698-
if (resolveResult === null || resolveResult === undefined)
697+
const resolveResult = resolvePackageTarget(
698+
packageJSONUrl,
699+
target,
700+
bestMatchSubpath,
701+
bestMatch,
702+
base,
703+
true,
704+
false,
705+
conditions);
706+
707+
if (resolveResult == null) {
699708
throwExportsNotFound(packageSubpath, packageJSONUrl, base);
709+
}
700710
return resolveResult;
701711
}
702712

@@ -740,8 +750,9 @@ function packageImportsResolve(name, base, conditions) {
740750
const resolveResult = resolvePackageTarget(
741751
packageJSONUrl, imports[name], '', name, base, false, true, conditions
742752
);
743-
if (resolveResult !== null && resolveResult !== undefined)
753+
if (resolveResult != null) {
744754
return resolveResult.resolved;
755+
}
745756
} else {
746757
let bestMatch = '';
747758
let bestMatchSubpath;
@@ -771,8 +782,9 @@ function packageImportsResolve(name, base, conditions) {
771782
bestMatchSubpath,
772783
bestMatch, base, true,
773784
true, conditions);
774-
if (resolveResult !== null && resolveResult !== undefined)
785+
if (resolveResult != null) {
775786
return resolveResult.resolved;
787+
}
776788
}
777789
}
778790
}
@@ -875,12 +887,14 @@ function packageResolve(specifier, base, conditions) {
875887
}
876888
if (packageSubpath === '.') {
877889
return {
878-
resolved: legacyMainResolve(packageJSONUrl,
879-
packageConfig,
880-
base),
890+
resolved: legacyMainResolve(
891+
packageJSONUrl,
892+
packageConfig,
893+
base),
881894
...(packageConfig.type !== 'none') && { format: packageConfig.type }
882895
};
883896
}
897+
884898
return {
885899
resolved: new URL(packageSubpath, packageJSONUrl),
886900
...(packageConfig.type !== 'none') && { format: packageConfig.type }
@@ -927,7 +941,8 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
927941
function moduleResolve(specifier, base, conditions, preserveSymlinks) {
928942
// Order swapped from spec for minor perf gain.
929943
// Ok since relative URLs cannot parse as URLs.
930-
let resolved, format;
944+
let resolved;
945+
let format;
931946
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
932947
resolved = new URL(specifier, base);
933948
} else if (specifier[0] === '#') {
@@ -939,11 +954,15 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
939954
({ resolved, format } = packageResolve(specifier, base, conditions));
940955
}
941956
}
942-
if (resolved.protocol !== 'file:')
943-
return { url: resolved };
957+
if (resolved.protocol !== 'file:') {
958+
return {
959+
url: resolved
960+
};
961+
}
962+
944963
return {
945964
url: finalizeResolution(resolved, base, preserveSymlinks),
946-
...(format !== undefined && format !== null) && { format: format }
965+
...(format != null) && { format }
947966
};
948967
}
949968

@@ -1032,11 +1051,16 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
10321051
}
10331052

10341053
conditions = getConditionsSet(conditions);
1035-
let url, format;
1054+
let url;
1055+
let format;
10361056
try {
10371057
({ url, format } =
1038-
moduleResolve(specifier, parentURL, conditions,
1039-
isMain ? preserveSymlinksMain : preserveSymlinks));
1058+
moduleResolve(
1059+
specifier,
1060+
parentURL,
1061+
conditions,
1062+
isMain ? preserveSymlinksMain : preserveSymlinks
1063+
));
10401064
} catch (error) {
10411065
// Try to give the user a hint of what would have been the
10421066
// resolved CommonJS module
@@ -1066,7 +1090,7 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
10661090

10671091
return {
10681092
url: `${url}`,
1069-
...(format !== undefined && format !== null) && { format: format }
1093+
...(format != null) && { format }
10701094
};
10711095
}
10721096

0 commit comments

Comments
 (0)