Skip to content

Commit

Permalink
fix: Modules providers loading mechanism to infer the source dir (#8015)
Browse files Browse the repository at this point in the history
* fix: Modules providers loading mechanism to infer the source dir

* finalize
  • Loading branch information
adrien2p authored Jul 8, 2024
1 parent b450628 commit fc7f5ff
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
12 changes: 9 additions & 3 deletions packages/core/modules-sdk/src/loaders/module-provider-loader.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 7 additions & 23 deletions packages/core/modules-sdk/src/loaders/register-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/core/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -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 ?? ""
}
1 change: 1 addition & 0 deletions packages/medusa/src/loaders/helpers/resolve-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function getResolvedPlugins(
]
? "src"
: "dist"

const extensionDirectory = path.join(rootDirectory, extensionDirectoryPath)
return [
{
Expand Down

0 comments on commit fc7f5ff

Please sign in to comment.