Skip to content

Commit 35ff3b9

Browse files
committed
fix(compiler): module type for nodenext
1 parent fc22a03 commit 35ff3b9

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

packages/type-compiler/src/compiler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ import {
9494
} from './reflection-ast.js';
9595
import { SourceFile } from './ts-types.js';
9696
import { MappedModifier, ReflectionOp, TypeNumberBrand } from '@deepkit/type-spec';
97-
import { Resolver } from './resolver.js';
97+
import { readNearestPackageJson, Resolver } from './resolver.js';
9898
import { knownLibFilesForCompilerOptions } from '@typescript/vfs';
9999
import { debug, debug2 } from './debug.js';
100100
import { ConfigResolver, getConfigResolver, MatchResult, ReflectionConfig, ReflectionConfigCache, reflectionModeMatcher, ResolvedConfig } from './config.js';
@@ -1081,12 +1081,12 @@ export class ReflectionTransformer implements CustomTransformer {
10811081
return this.sourceFile;
10821082
}
10831083

1084-
protected getModuleType(): 'cjs' | 'esm' {
1084+
protected getModuleType(): 'esm' | 'cjs' {
10851085
if (this.compilerOptions.module === ts.ModuleKind.Node16 || this.compilerOptions.module === ts.ModuleKind.NodeNext) {
1086-
if (this.sourceFile.impliedNodeFormat === ts.ModuleKind.ESNext) {
1087-
return 'esm';
1088-
}
1089-
return 'cjs';
1086+
if (this.sourceFile.fileName.endsWith('.mjs')) return 'esm';
1087+
if (this.sourceFile.fileName.endsWith('.cjs')) return 'cjs';
1088+
const pkg = readNearestPackageJson(this.sourceFile.fileName);
1089+
return pkg?.type === 'module' ? 'esm' : 'cjs';
10901090
}
10911091
return this.compilerOptions.module === ts.ModuleKind.CommonJS ? 'cjs' : 'esm';
10921092
}

packages/type-compiler/src/resolver.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,32 @@ import {
1010
StringLiteral,
1111
} from 'typescript';
1212
import ts from 'typescript';
13+
import { parse, join, dirname } from 'node:path';
14+
import { existsSync, readFileSync } from 'node:fs';
1315

1416
const { createSourceFile, resolveModuleName, isStringLiteral, JSDocParsingMode, ScriptTarget } = ts;
1517

18+
const packageJsonCache: { dir: string, content: any | null }[] = [];
19+
20+
export function readNearestPackageJson(filePath: string): any {
21+
const cachedEntry = packageJsonCache
22+
.filter(entry => filePath.startsWith(entry.dir))
23+
.sort((a, b) => b.dir.length - a.dir.length)[0];
24+
25+
if (cachedEntry) return cachedEntry.content;
26+
27+
let dir = dirname(filePath);
28+
while (dir !== parse(dir).root) {
29+
const packageJsonPath = join(dir, 'package.json');
30+
if (existsSync(packageJsonPath)) {
31+
const content = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
32+
packageJsonCache.push({ dir, content });
33+
return content;
34+
}
35+
dir = dirname(dir);
36+
}
37+
}
38+
1639
export function patternMatch(path: string, patterns: string[], base?: string): boolean {
1740
const include = patterns.filter(pattern => pattern[0] !== '!');
1841

packages/type-compiler/src/ts-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface SourceFile extends TSSourceFile {
1515
*/
1616
redirectInfo?: any;
1717

18+
fileName: string;
19+
1820
scriptKind?: ScriptKind;
1921

2022
externalModuleIndicator?: Node;

0 commit comments

Comments
 (0)