Skip to content

Commit 5e8bf48

Browse files
authored
Fix auto-imports with --moduleResolution bundler and customConditions (#52423)
1 parent 0141d1d commit 5e8bf48

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ function getNodeResolutionFeatures(options: CompilerOptions) {
659659
return features;
660660
}
661661

662-
function getConditions(options: CompilerOptions, esmMode?: boolean) {
662+
/** @internal */
663+
export function getConditions(options: CompilerOptions, esmMode?: boolean) {
663664
// conditions are only used by the node16/nodenext/bundler resolvers - there's no priority order in the list,
664665
// it's essentially a set (priority is determined by object insertion order in the object we look at).
665666
const conditions = esmMode || getEmitModuleResolutionKind(options) === ModuleResolutionKind.Bundler

src/compiler/moduleSpecifiers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
forEachAncestorDirectory,
3434
getBaseFileName,
3535
GetCanonicalFileName,
36+
getConditions,
3637
getDirectoryPath,
3738
getEmitModuleResolutionKind,
3839
getModeForResolutionAtIndex,
@@ -46,6 +47,7 @@ import {
4647
getPathsBasePath,
4748
getRelativePathFromDirectory,
4849
getRelativePathToDirectoryOrUrl,
50+
getResolvePackageJsonExports,
4951
getSourceFileOfModule,
5052
getSupportedExtensions,
5153
getTextOfIdentifierOrLiteral,
@@ -945,8 +947,8 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
945947
if (typeof cachedPackageJson === "object" || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
946948
const packageJsonContent = cachedPackageJson?.contents.packageJsonContent || JSON.parse(host.readFile!(packageJsonPath)!);
947949
const importMode = overrideMode || importingSourceFile.impliedNodeFormat;
948-
if (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext) {
949-
const conditions = ["node", importMode === ModuleKind.ESNext ? "import" : "require", "types"];
950+
if (getResolvePackageJsonExports(options)) {
951+
const conditions = getConditions(options, importMode === ModuleKind.ESNext);
950952
const fromExports = packageJsonContent.exports && typeof packageJsonContent.name === "string"
951953
? tryGetModuleNameFromExports(options, path, packageRootPath, getPackageNameFromTypesPackageName(packageJsonContent.name), packageJsonContent.exports, conditions)
952954
: undefined;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @moduleResolution: bundler
5+
6+
// @Filename: /node_modules/dep/package.json
7+
//// {
8+
//// "name": "dep",
9+
//// "version": "1.0.0",
10+
//// "exports": {
11+
//// ".": "./dist/index.js"
12+
//// }
13+
//// }
14+
15+
// @Filename: /node_modules/dep/dist/index.d.ts
16+
//// export const dep: number;
17+
18+
// @Filename: /index.ts
19+
//// dep/**/
20+
21+
verify.importFixModuleSpecifiers("", ["dep"]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @moduleResolution: bundler
5+
// @customConditions: custom
6+
7+
// @Filename: /node_modules/dep/package.json
8+
//// {
9+
//// "name": "dep",
10+
//// "version": "1.0.0",
11+
//// "exports": {
12+
//// ".": {
13+
//// "custom": "./dist/index.js"
14+
//// }
15+
//// }
16+
//// }
17+
18+
// @Filename: /node_modules/dep/dist/index.d.ts
19+
//// export const dep: number;
20+
21+
// @Filename: /index.ts
22+
//// dep/**/
23+
24+
verify.importFixModuleSpecifiers("", ["dep"]);

0 commit comments

Comments
 (0)