Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
forEachAncestorDirectory,
getBaseFileName,
GetCanonicalFileName,
getConditions,
getDirectoryPath,
getEmitModuleResolutionKind,
getModeForResolutionAtIndex,
Expand All @@ -46,6 +47,7 @@ import {
getPathsBasePath,
getRelativePathFromDirectory,
getRelativePathToDirectoryOrUrl,
getResolvePackageJsonExports,
getSourceFileOfModule,
getSupportedExtensions,
getTextOfIdentifierOrLiteral,
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/autoImportBundlerExports.ts
Original file line number Diff line number Diff line change
@@ -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"]);
24 changes: 24 additions & 0 deletions tests/cases/fourslash/autoImportsCustomConditions.ts
Original file line number Diff line number Diff line change
@@ -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"]);