Skip to content
Open
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
12 changes: 6 additions & 6 deletions packages/type-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import {
} from './reflection-ast.js';
import { SourceFile } from './ts-types.js';
import { MappedModifier, ReflectionOp, TypeNumberBrand } from '@deepkit/type-spec';
import { Resolver } from './resolver.js';
import { readNearestPackageJson, Resolver } from './resolver.js';
import { knownLibFilesForCompilerOptions } from '@typescript/vfs';
import { debug, debug2 } from './debug.js';
import { ConfigResolver, getConfigResolver, MatchResult, ReflectionConfig, ReflectionConfigCache, reflectionModeMatcher, ResolvedConfig } from './config.js';
Expand Down Expand Up @@ -1081,12 +1081,12 @@ export class ReflectionTransformer implements CustomTransformer {
return this.sourceFile;
}

protected getModuleType(): 'cjs' | 'esm' {
protected getModuleType(): 'esm' | 'cjs' {
if (this.compilerOptions.module === ts.ModuleKind.Node16 || this.compilerOptions.module === ts.ModuleKind.NodeNext) {
if (this.sourceFile.impliedNodeFormat === ts.ModuleKind.ESNext) {
return 'esm';
}
return 'cjs';
if (this.sourceFile.fileName.endsWith('.mjs')) return 'esm';
if (this.sourceFile.fileName.endsWith('.cjs')) return 'cjs';
const pkg = readNearestPackageJson(this.sourceFile.fileName);
return pkg?.type === 'module' ? 'esm' : 'cjs';
}
return this.compilerOptions.module === ts.ModuleKind.CommonJS ? 'cjs' : 'esm';
}
Expand Down
23 changes: 23 additions & 0 deletions packages/type-compiler/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,32 @@ import {
StringLiteral,
} from 'typescript';
import ts from 'typescript';
import { parse, join, dirname } from 'node:path';
import { existsSync, readFileSync } from 'node:fs';

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

const packageJsonCache: { dir: string, content: any | null }[] = [];

export function readNearestPackageJson(filePath: string): any {
const cachedEntry = packageJsonCache
.filter(entry => filePath.startsWith(entry.dir))
.sort((a, b) => b.dir.length - a.dir.length)[0];

if (cachedEntry) return cachedEntry.content;

let dir = dirname(filePath);
while (dir !== parse(dir).root) {
const packageJsonPath = join(dir, 'package.json');
if (existsSync(packageJsonPath)) {
const content = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
packageJsonCache.push({ dir, content });
return content;
}
dir = dirname(dir);
}
}

export function patternMatch(path: string, patterns: string[], base?: string): boolean {
const include = patterns.filter(pattern => pattern[0] !== '!');

Expand Down
2 changes: 2 additions & 0 deletions packages/type-compiler/src/ts-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface SourceFile extends TSSourceFile {
*/
redirectInfo?: any;

fileName: string;

scriptKind?: ScriptKind;

externalModuleIndicator?: Node;
Expand Down
Loading
Loading