fix: Modules providers loading mechanism to infer the source dir (#8015)

* fix: Modules providers loading mechanism to infer the source dir

* finalize
This commit is contained in:
Adrien de Peretti
2024-07-08 14:11:45 +02:00
committed by GitHub
parent b450628481
commit fc7f5ff71a
5 changed files with 44 additions and 26 deletions

View File

@@ -67,3 +67,4 @@ export * from "./upper-case-first"
export * from "./validate-handle"
export * from "./wrap-handler"
export * from "./define-config"
export * from "./normalize-import-path-with-source"

View File

@@ -0,0 +1,26 @@
import { join } from "path"
/**
* Normalize the import path based on the project running on ts-node or not.
* @param path
*/
export function normalizeImportPathWithSource(
path: string | undefined
): string {
let normalizePath = path
/**
* If the project is running on ts-node all relative module resolution
* will target the src directory and otherwise the dist directory.
* If the path is not relative, then we can safely import from it and let the resolution
* happen under the hood.
*/
if (normalizePath?.startsWith("./")) {
const sourceDir = process[Symbol.for("ts-node.register.instance")]
? "src"
: "dist"
normalizePath = join(process.cwd(), sourceDir, normalizePath)
}
return normalizePath ?? ""
}