Fix auto-imports with --moduleResolution bundler and customConditions (#52423)

This commit is contained in:
Andrew Branch 2023-01-26 09:44:07 -08:00 committed by GitHub
parent 0141d1d5f2
commit 5e8bf480e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 3 deletions

View File

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

View File

@ -33,6 +33,7 @@ import {
forEachAncestorDirectory,
getBaseFileName,
GetCanonicalFileName,
getConditions,
getDirectoryPath,
getEmitModuleResolutionKind,
getModeForResolutionAtIndex,
@ -46,6 +47,7 @@ import {
getPathsBasePath,
getRelativePathFromDirectory,
getRelativePathToDirectoryOrUrl,
getResolvePackageJsonExports,
getSourceFileOfModule,
getSupportedExtensions,
getTextOfIdentifierOrLiteral,
@ -945,8 +947,8 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
if (typeof cachedPackageJson === "object" || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
const packageJsonContent = cachedPackageJson?.contents.packageJsonContent || JSON.parse(host.readFile!(packageJsonPath)!);
const importMode = overrideMode || importingSourceFile.impliedNodeFormat;
if (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext) {
const conditions = ["node", importMode === ModuleKind.ESNext ? "import" : "require", "types"];
if (getResolvePackageJsonExports(options)) {
const conditions = getConditions(options, importMode === ModuleKind.ESNext);
const fromExports = packageJsonContent.exports && typeof packageJsonContent.name === "string"
? tryGetModuleNameFromExports(options, path, packageRootPath, getPackageNameFromTypesPackageName(packageJsonContent.name), packageJsonContent.exports, conditions)
: undefined;

View File

@ -0,0 +1,21 @@
/// <reference path="fourslash.ts" />
// @module: esnext
// @moduleResolution: bundler
// @Filename: /node_modules/dep/package.json
//// {
//// "name": "dep",
//// "version": "1.0.0",
//// "exports": {
//// ".": "./dist/index.js"
//// }
//// }
// @Filename: /node_modules/dep/dist/index.d.ts
//// export const dep: number;
// @Filename: /index.ts
//// dep/**/
verify.importFixModuleSpecifiers("", ["dep"]);

View File

@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />
// @module: esnext
// @moduleResolution: bundler
// @customConditions: custom
// @Filename: /node_modules/dep/package.json
//// {
//// "name": "dep",
//// "version": "1.0.0",
//// "exports": {
//// ".": {
//// "custom": "./dist/index.js"
//// }
//// }
//// }
// @Filename: /node_modules/dep/dist/index.d.ts
//// export const dep: number;
// @Filename: /index.ts
//// dep/**/
verify.importFixModuleSpecifiers("", ["dep"]);