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:
committed by
GitHub
parent
b450628481
commit
fc7f5ff71a
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 ?? ""
|
||||
}
|
||||
@@ -112,6 +112,7 @@ export function getResolvedPlugins(
|
||||
]
|
||||
? "src"
|
||||
: "dist"
|
||||
|
||||
const extensionDirectory = path.join(rootDirectory, extensionDirectoryPath)
|
||||
return [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user