From 4cc87da2e7f78d1d0d4890a27fa999b0f1d957b3 Mon Sep 17 00:00:00 2001 From: Thiago Date: Mon, 23 Sep 2024 12:42:25 -0300 Subject: [PATCH] fix: require relative path handling --- package-lock.json | 3 ++- src/emitter.ts | 2 +- src/module-exists.ts | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index ca19569..b5c2b34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,8 @@ "typescript": "^5.3.3" }, "peerDependencies": { - "@nestjs/cli": "^10.4.5", + "@nestjs/cli": "*", + "tsconfig-paths": "*", "typescript": "^5.3.3" } }, diff --git a/src/emitter.ts b/src/emitter.ts index af57cd0..b780a19 100644 --- a/src/emitter.ts +++ b/src/emitter.ts @@ -89,7 +89,7 @@ export function before() { }; const processImports = (node: ts.Node) => { try { - if (tsBinary.isImportClause(node) && mustImport.has(node) && moduleExists((node.parent.moduleSpecifier as any).text)) { + if (tsBinary.isImportClause(node) && mustImport.has(node) && moduleExists(sf, (node.parent.moduleSpecifier as any).text)) { const { namedBindings } = node; if (namedBindings) { // Hack: if a import is flagged as transient and has links.referenced = true, diff --git a/src/module-exists.ts b/src/module-exists.ts index bd1a789..6d57e06 100644 --- a/src/module-exists.ts +++ b/src/module-exists.ts @@ -1,9 +1,14 @@ import { existsSync } from 'fs'; +import { dirname } from 'path'; import { getModuleRealPath } from './ts-loader'; +import ts from 'typescript'; -export function moduleExists(moduleName: string) { +export function moduleExists(sf: ts.SourceFile, moduleName: string) { try { - const tsConfigTreatedModuleName = getModuleRealPath(moduleName); + let tsConfigTreatedModuleName = getModuleRealPath(moduleName); + if (!tsConfigTreatedModuleName && moduleName.startsWith('.')) { + tsConfigTreatedModuleName = `${dirname(sf.fileName)}/${moduleName}`; + } if (tsConfigTreatedModuleName && (existsSync(tsConfigTreatedModuleName) || existsSync(`${tsConfigTreatedModuleName}.ts`)