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:
1 parent
b450628481
commit
fc7f5ff71a
5 files changed
+44
-26
No files matched your search
@@ -1,6 +1,11 @@
|
|||||||
import { MedusaContainer, ModuleProvider } from "@medusajs/types"
|
import { MedusaContainer, ModuleProvider } from "@medusajs/types"
|
||||||
import { isString, lowerCaseFirst, promiseAll } from "@medusajs/utils"
|
import {
|
||||||
import { Lifetime, asFunction } from "awilix"
|
isString,
|
||||||
|
lowerCaseFirst,
|
||||||
|
normalizeImportPathWithSource,
|
||||||
|
promiseAll,
|
||||||
|
} from "@medusajs/utils"
|
||||||
|
import { asFunction, Lifetime } from "awilix"
|
||||||
|
|
||||||
export async function moduleProviderLoader({
|
export async function moduleProviderLoader({
|
||||||
container,
|
container,
|
||||||
@@ -38,7 +43,8 @@ export async function loadModuleProvider(
|
|||||||
loadedProvider = provider.resolve
|
loadedProvider = provider.resolve
|
||||||
|
|
||||||
if (isString(provider.resolve)) {
|
if (isString(provider.resolve)) {
|
||||||
loadedProvider = await import(provider.resolve)
|
const normalizedPath = normalizeImportPathWithSource(provider.resolve)
|
||||||
|
loadedProvider = await import(normalizedPath)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|||||||
@@ -5,9 +5,12 @@ import {
|
|||||||
ModuleExports,
|
ModuleExports,
|
||||||
ModuleResolution,
|
ModuleResolution,
|
||||||
} from "@medusajs/types"
|
} 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 resolveCwd from "resolve-cwd"
|
||||||
import { ModulesDefinition } from "../definitions"
|
import { ModulesDefinition } from "../definitions"
|
||||||
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../types"
|
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "../types"
|
||||||
@@ -58,30 +61,11 @@ export const registerMedusaModule = (
|
|||||||
return moduleResolutions
|
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(
|
function getCustomModuleResolution(
|
||||||
key: string,
|
key: string,
|
||||||
moduleConfig: InternalModuleDeclaration | string
|
moduleConfig: InternalModuleDeclaration | string
|
||||||
): ModuleResolution {
|
): ModuleResolution {
|
||||||
const originalPath = normalizePath(
|
const originalPath = normalizeImportPathWithSource(
|
||||||
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||||
)
|
)
|
||||||
const resolutionPath = resolveCwd(originalPath)
|
const resolutionPath = resolveCwd(originalPath)
|
||||||
@@ -157,7 +141,7 @@ function getInternalModuleResolution(
|
|||||||
// If user added a module and it's overridable, we resolve that instead
|
// If user added a module and it's overridable, we resolve that instead
|
||||||
const isStr = isString(moduleConfig)
|
const isStr = isString(moduleConfig)
|
||||||
if (isStr || (isObj && moduleConfig.resolve)) {
|
if (isStr || (isObj && moduleConfig.resolve)) {
|
||||||
const originalPath = normalizePath(
|
const originalPath = normalizeImportPathWithSource(
|
||||||
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||||
)
|
)
|
||||||
resolutionPath = resolveCwd(originalPath)
|
resolutionPath = resolveCwd(originalPath)
|
||||||
|
|||||||
@@ -67,3 +67,4 @@ export * from "./upper-case-first"
|
|||||||
export * from "./validate-handle"
|
export * from "./validate-handle"
|
||||||
export * from "./wrap-handler"
|
export * from "./wrap-handler"
|
||||||
export * from "./define-config"
|
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"
|
? "src"
|
||||||
: "dist"
|
: "dist"
|
||||||
|
|
||||||
const extensionDirectory = path.join(rootDirectory, extensionDirectoryPath)
|
const extensionDirectory = path.join(rootDirectory, extensionDirectoryPath)
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user