From fc7f5ff71abb90e96326a4cc3b27488f6bbfbaca Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Mon, 8 Jul 2024 14:11:45 +0200 Subject: [PATCH] fix: Modules providers loading mechanism to infer the source dir (#8015) * fix: Modules providers loading mechanism to infer the source dir * finalize --- .../src/loaders/module-provider-loader.ts | 12 ++++++-- .../src/loaders/register-modules.ts | 30 +++++-------------- packages/core/utils/src/common/index.ts | 1 + .../normalize-import-path-with-source.ts | 26 ++++++++++++++++ .../src/loaders/helpers/resolve-plugins.ts | 1 + 5 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 packages/core/utils/src/common/normalize-import-path-with-source.ts diff --git a/packages/core/modules-sdk/src/loaders/module-provider-loader.ts b/packages/core/modules-sdk/src/loaders/module-provider-loader.ts index b966541eee..00a18f7cd7 100644 --- a/packages/core/modules-sdk/src/loaders/module-provider-loader.ts +++ b/packages/core/modules-sdk/src/loaders/module-provider-loader.ts @@ -1,6 +1,11 @@ import { MedusaContainer, ModuleProvider } from "@medusajs/types" -import { isString, lowerCaseFirst, promiseAll } from "@medusajs/utils" -import { Lifetime, asFunction } from "awilix" +import { + isString, + lowerCaseFirst, + normalizeImportPathWithSource, + promiseAll, +} from "@medusajs/utils" +import { asFunction, Lifetime } from "awilix" export async function moduleProviderLoader({ container, @@ -38,7 +43,8 @@ export async function loadModuleProvider( loadedProvider = provider.resolve if (isString(provider.resolve)) { - loadedProvider = await import(provider.resolve) + const normalizedPath = normalizeImportPathWithSource(provider.resolve) + loadedProvider = await import(normalizedPath) } } catch (error) { throw new Error( diff --git a/packages/core/modules-sdk/src/loaders/register-modules.ts b/packages/core/modules-sdk/src/loaders/register-modules.ts index 618dce01b6..7a66df3fe7 100644 --- a/packages/core/modules-sdk/src/loaders/register-modules.ts +++ b/packages/core/modules-sdk/src/loaders/register-modules.ts @@ -5,9 +5,12 @@ import { ModuleExports, ModuleResolution, } from "@medusajs/types" -import { join } from "path" -import { isObject, isString } from "@medusajs/utils" +import { + isObject, + isString, + normalizeImportPathWithSource, +} from "@medusajs/utils" import resolveCwd from "resolve-cwd" import { ModulesDefinition } from "../definitions" import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../types" @@ -58,30 +61,11 @@ 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 originalPath = normalizePath( + const originalPath = normalizeImportPathWithSource( (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string ) const resolutionPath = resolveCwd(originalPath) @@ -157,7 +141,7 @@ function getInternalModuleResolution( // If user added a module and it's overridable, we resolve that instead const isStr = isString(moduleConfig) if (isStr || (isObj && moduleConfig.resolve)) { - const originalPath = normalizePath( + const originalPath = normalizeImportPathWithSource( (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string ) resolutionPath = resolveCwd(originalPath) diff --git a/packages/core/utils/src/common/index.ts b/packages/core/utils/src/common/index.ts index ddd15cfda7..3653434693 100644 --- a/packages/core/utils/src/common/index.ts +++ b/packages/core/utils/src/common/index.ts @@ -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" diff --git a/packages/core/utils/src/common/normalize-import-path-with-source.ts b/packages/core/utils/src/common/normalize-import-path-with-source.ts new file mode 100644 index 0000000000..03b278f063 --- /dev/null +++ b/packages/core/utils/src/common/normalize-import-path-with-source.ts @@ -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 ?? "" +} diff --git a/packages/medusa/src/loaders/helpers/resolve-plugins.ts b/packages/medusa/src/loaders/helpers/resolve-plugins.ts index 0f173ab8eb..ac1072d8e5 100644 --- a/packages/medusa/src/loaders/helpers/resolve-plugins.ts +++ b/packages/medusa/src/loaders/helpers/resolve-plugins.ts @@ -112,6 +112,7 @@ export function getResolvedPlugins( ] ? "src" : "dist" + const extensionDirectory = path.join(rootDirectory, extensionDirectoryPath) return [ {