fix(modules-sdk): load custom modules options (#7176)
This commit is contained in:
1 parent
edcafa140c
commit
1eeb1e9de3
3 files changed
+55
-19
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Load custom modules config
|
||||||
@@ -55,6 +55,32 @@ describe("module definitions loader", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("Resolves a custom module without pre-defined definition", () => {
|
||||||
|
const res = registerMedusaModule("customModulesABC", {
|
||||||
|
options: {
|
||||||
|
test: 123,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res).toEqual({
|
||||||
|
customModulesABC: expect.objectContaining({
|
||||||
|
resolutionPath: "@medusajs/test-service-resolved",
|
||||||
|
definition: expect.objectContaining({
|
||||||
|
key: "customModulesABC",
|
||||||
|
label: "Custom: customModulesABC",
|
||||||
|
registrationName: "customModulesABC",
|
||||||
|
}),
|
||||||
|
moduleDeclaration: {
|
||||||
|
resources: "shared",
|
||||||
|
scope: "internal",
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
test: 123,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("boolean config", () => {
|
describe("boolean config", () => {
|
||||||
it("Resolves module with no resolution path when given false", () => {
|
it("Resolves module with no resolution path when given false", () => {
|
||||||
Object.assign(ModulesDefinition, {
|
Object.assign(ModulesDefinition, {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
ModuleResolution,
|
ModuleResolution,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
import { isObject } from "@medusajs/utils"
|
import { isObject, isString } from "@medusajs/utils"
|
||||||
import resolveCwd from "resolve-cwd"
|
import resolveCwd from "resolve-cwd"
|
||||||
import { ModulesDefinition } from "../definitions"
|
import { ModulesDefinition } from "../definitions"
|
||||||
|
|
||||||
@@ -25,14 +25,6 @@ export const registerMedusaModule = (
|
|||||||
|
|
||||||
const modDefinition = definition ?? ModulesDefinition[moduleKey]
|
const modDefinition = definition ?? ModulesDefinition[moduleKey]
|
||||||
|
|
||||||
if (modDefinition === undefined) {
|
|
||||||
moduleResolutions[moduleKey] = getCustomModuleResolution(
|
|
||||||
moduleKey,
|
|
||||||
moduleDeclaration as InternalModuleDeclaration
|
|
||||||
)
|
|
||||||
return moduleResolutions
|
|
||||||
}
|
|
||||||
|
|
||||||
const modDeclaration =
|
const modDeclaration =
|
||||||
moduleDeclaration ??
|
moduleDeclaration ??
|
||||||
(modDefinition?.defaultModuleDeclaration as InternalModuleDeclaration)
|
(modDefinition?.defaultModuleDeclaration as InternalModuleDeclaration)
|
||||||
@@ -49,6 +41,14 @@ export const registerMedusaModule = (
|
|||||||
throw new Error("External Modules are not supported yet.")
|
throw new Error("External Modules are not supported yet.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (modDefinition === undefined) {
|
||||||
|
moduleResolutions[moduleKey] = getCustomModuleResolution(
|
||||||
|
moduleKey,
|
||||||
|
moduleDeclaration as InternalModuleDeclaration
|
||||||
|
)
|
||||||
|
return moduleResolutions
|
||||||
|
}
|
||||||
|
|
||||||
moduleResolutions[moduleKey] = getInternalModuleResolution(
|
moduleResolutions[moduleKey] = getInternalModuleResolution(
|
||||||
modDefinition,
|
modDefinition,
|
||||||
moduleDeclaration as InternalModuleDeclaration,
|
moduleDeclaration as InternalModuleDeclaration,
|
||||||
@@ -62,11 +62,16 @@ function getCustomModuleResolution(
|
|||||||
key: string,
|
key: string,
|
||||||
moduleConfig: InternalModuleDeclaration | string
|
moduleConfig: InternalModuleDeclaration | string
|
||||||
): ModuleResolution {
|
): ModuleResolution {
|
||||||
const isString = typeof moduleConfig === "string"
|
|
||||||
const resolutionPath = resolveCwd(
|
const resolutionPath = resolveCwd(
|
||||||
isString ? moduleConfig : (moduleConfig.resolve as string)
|
isString(moduleConfig) ? moduleConfig : (moduleConfig.resolve as string)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const conf = isObject(moduleConfig)
|
||||||
|
? moduleConfig
|
||||||
|
: ({} as InternalModuleDeclaration)
|
||||||
|
|
||||||
|
const dependencies = conf?.dependencies ?? []
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolutionPath,
|
resolutionPath,
|
||||||
definition: {
|
definition: {
|
||||||
@@ -74,7 +79,7 @@ function getCustomModuleResolution(
|
|||||||
label: `Custom: ${key}`,
|
label: `Custom: ${key}`,
|
||||||
isRequired: false,
|
isRequired: false,
|
||||||
defaultPackage: "",
|
defaultPackage: "",
|
||||||
dependencies: [],
|
dependencies,
|
||||||
registrationName: key,
|
registrationName: key,
|
||||||
defaultModuleDeclaration: {
|
defaultModuleDeclaration: {
|
||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||||
@@ -82,11 +87,11 @@ function getCustomModuleResolution(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
moduleDeclaration: {
|
moduleDeclaration: {
|
||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: conf?.resources ?? MODULE_RESOURCE_TYPE.SHARED,
|
||||||
scope: MODULE_SCOPE.INTERNAL,
|
scope: MODULE_SCOPE.INTERNAL,
|
||||||
},
|
},
|
||||||
dependencies: [],
|
dependencies,
|
||||||
options: {},
|
options: conf?.options ?? {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,14 +131,14 @@ function getInternalModuleResolution(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isObj = typeof moduleConfig === "object"
|
const isObj = isObject(moduleConfig)
|
||||||
let resolutionPath = definition.defaultPackage
|
let resolutionPath = definition.defaultPackage
|
||||||
|
|
||||||
// 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 isString = typeof moduleConfig === "string"
|
const isStr = isString(moduleConfig)
|
||||||
if (isString || (isObj && moduleConfig.resolve)) {
|
if (isStr || (isObj && moduleConfig.resolve)) {
|
||||||
resolutionPath = !moduleExports
|
resolutionPath = !moduleExports
|
||||||
? resolveCwd(isString ? moduleConfig : (moduleConfig.resolve as string))
|
? resolveCwd(isStr ? moduleConfig : (moduleConfig.resolve as string))
|
||||||
: // Explicitly assign an empty string, later, we will check if the value is exactly false.
|
: // Explicitly assign an empty string, later, we will check if the value is exactly false.
|
||||||
// This allows to continue the module loading while using the module exports instead of re importing the module itself during the process.
|
// This allows to continue the module loading while using the module exports instead of re importing the module itself during the process.
|
||||||
""
|
""
|
||||||
|
|||||||
Reference in new issue
Block a user