dsherret / dsherret/ts-morph

Project doesn't resolve ESM only exports reported by getExportedDeclarations

Open
#1,667 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 27.0.2

`DocumentRegistry` in `@ts-morph/common` passes `compilationSettings.target` (a bare `ScriptTarget`) to `ts.createLanguageServiceSourceFile` instead of a `CreateSourceFileOptions` with `impliedNodeFormat`. This means source files never know their module format.

**To Reproduce**

`node_modules/packageA/package.json`:
```json
{
"name": "packageA",
"exports": { ".": { "import": "./lib/index.js" } }
}
```

`node_modules/packageA/lib/index.d.ts`:
```ts
export type Foo = "Foo";
```

`package.json`:
```json
{
"type": "module"
}
```

`tsconfig.json`:
```json
{ "compilerOptions": { "module": "Node16" } }
```

`test.ts`:
```ts
export type { Foo } from "packageA";
```

usage:
```ts
import { Project } from "ts-morph";

const project = new Project({
tsConfigFilePath: "./tsconfig.json",
skipAddingFilesFromTsConfig: true,
});
const sourceFile = project.addSourceFileAtPath("test.ts");

const exports = sourceFile.getExportedDeclarations();

assert(exports.get("Foo")!.length > 0);
```

**Workaround**

```ts
const pkgJson = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
const isESM = pkgJson.type === "module";
const project = new Project({
tsConfigFilePath: "./tsconfig.json",
skipAddingFilesFromTsConfig: true,
compilerOptions: {
customConditions: [isESM ? "import" : "require"],
},
});
```

**Expected behavior**

ts-morph should call `ts.getImpliedNodeFormatForFile()` and pass the result via `CreateSourceFileOptions`, matching `tsc` behavior.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.