From 61977bd3929b4317b3bf385dfc3078318e3cb206 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 30 May 2024 11:14:55 +0200 Subject: [PATCH] chore: Allow modules to discover resources from ts/js and local directories (#7520) **What** Update the module resources discovery to account to the source directory to look into. example: ```ts user: { resolve: 'user' } ``` The above config will load the resources from the resolved path looking from the node modules ```ts user: { resolve: './modules/user' } ``` The above config will load the resources from the local directory under dist if not run with ts node and under src otherwise --- .../src/loaders/register-modules.ts | 31 ++++++++++++++++--- .../src/loaders/utils/load-internal.ts | 29 +++++++++++------ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/packages/core/modules-sdk/src/loaders/register-modules.ts b/packages/core/modules-sdk/src/loaders/register-modules.ts index d22bde9cca..618dce01b6 100644 --- a/packages/core/modules-sdk/src/loaders/register-modules.ts +++ b/packages/core/modules-sdk/src/loaders/register-modules.ts @@ -5,6 +5,8 @@ import { ModuleExports, ModuleResolution, } from "@medusajs/types" +import { join } from "path" + import { isObject, isString } from "@medusajs/utils" import resolveCwd from "resolve-cwd" import { ModulesDefinition } from "../definitions" @@ -56,13 +58,33 @@ export const registerMedusaModule = ( return moduleResolutions } +function normalizePath(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 ?? "" +} + function getCustomModuleResolution( key: string, moduleConfig: InternalModuleDeclaration | string ): ModuleResolution { - const resolutionPath = resolveCwd( - isString(moduleConfig) ? moduleConfig : (moduleConfig.resolve as string) + const originalPath = normalizePath( + (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string ) + const resolutionPath = resolveCwd(originalPath) const conf = isObject(moduleConfig) ? moduleConfig @@ -135,9 +157,10 @@ function getInternalModuleResolution( // If user added a module and it's overridable, we resolve that instead const isStr = isString(moduleConfig) if (isStr || (isObj && moduleConfig.resolve)) { - resolutionPath = resolveCwd( - isStr ? moduleConfig : (moduleConfig.resolve as string) + const originalPath = normalizePath( + (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string ) + resolutionPath = resolveCwd(originalPath) } const moduleDeclaration = isObj ? moduleConfig : {} diff --git a/packages/core/modules-sdk/src/loaders/utils/load-internal.ts b/packages/core/modules-sdk/src/loaders/utils/load-internal.ts index e17215e693..6459ee9858 100644 --- a/packages/core/modules-sdk/src/loaders/utils/load-internal.ts +++ b/packages/core/modules-sdk/src/loaders/utils/load-internal.ts @@ -208,17 +208,24 @@ export async function loadModuleMigrations( async function importAllFromDir(path: string) { let filesToLoad: string[] = [] + const excludedExtensions = [".ts.map", ".js.map", ".d.ts"] + await readdir(path).then((files) => { files.forEach((file) => { - if (file !== "index.js" && file.endsWith(".js")) { - const filePath = join(path, file) - const stats = statSync(filePath) + if ( + file.startsWith("index.") || + excludedExtensions.some((ext) => file.endsWith(ext)) + ) { + return + } - if (stats.isDirectory()) { - // TODO: should we handle that? dont think so but I put that here for discussion - } else if (stats.isFile()) { - filesToLoad.push(filePath) - } + const filePath = join(path, file) + const stats = statSync(filePath) + + if (stats.isDirectory()) { + // TODO: should we handle that? dont think so but I put that here for discussion + } else if (stats.isFile()) { + filesToLoad.push(filePath) } }) @@ -237,8 +244,10 @@ async function loadResources( moduleResolution: ModuleResolution, logger: Logger ): Promise { - const modulePath = moduleResolution.resolutionPath as string - let normalizedPath = modulePath.replace("index.js", "") + let modulePath = moduleResolution.resolutionPath as string + let normalizedPath = modulePath + .replace("index.js", "") + .replace("index.ts", "") normalizedPath = resolve(normalizedPath) try {