From b5cec75f5ef49d2ed256ac68eee616668d3cbfd9 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 15:08:27 +0530 Subject: [PATCH 1/6] refactor: extract modules transformation to its own method --- .../__tests__/transform-modules.spec.ts | 52 +++++++ .../core/utils/src/common/define-config.ts | 140 ++++++++++-------- 2 files changed, 127 insertions(+), 65 deletions(-) create mode 100644 packages/core/utils/src/common/__tests__/transform-modules.spec.ts diff --git a/packages/core/utils/src/common/__tests__/transform-modules.spec.ts b/packages/core/utils/src/common/__tests__/transform-modules.spec.ts new file mode 100644 index 0000000000000..2f867afc8ad7c --- /dev/null +++ b/packages/core/utils/src/common/__tests__/transform-modules.spec.ts @@ -0,0 +1,52 @@ +import { MODULE_PACKAGE_NAMES, Modules } from "../../modules-sdk" +import { transformModules } from "../define-config" + +describe("transformModules", () => { + test("convert array of modules to an object", () => { + const modules = transformModules([ + { + resolve: require.resolve("../__fixtures__/define-config/github"), + options: { + apiKey: "test", + }, + }, + ]) + + expect(modules).toEqual({ + GithubModuleService: { + options: { + apiKey: "test", + }, + resolve: require.resolve("../__fixtures__/define-config/github"), + }, + }) + }) + + test("transform default module", () => { + const modules = transformModules([ + { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + }, + ]) + + expect(modules).toEqual({ + cache: { + resolve: "@medusajs/medusa/cache-inmemory", + }, + }) + }) + + test("remove module when its disabled at a later stage in the array", () => { + const modules = transformModules([ + { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + }, + { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + disable: true, + }, + ]) + + expect(modules).toEqual({}) + }) +}) diff --git a/packages/core/utils/src/common/define-config.ts b/packages/core/utils/src/common/define-config.ts index b0bbef36d384c..5f6f1c6b32d40 100644 --- a/packages/core/utils/src/common/define-config.ts +++ b/packages/core/utils/src/common/define-config.ts @@ -108,8 +108,80 @@ export function defineConfig(config: InputConfig = {}): ConfigModule { } /** - * The user API allow to use array of modules configuration. This method manage the loading of the user modules - * along side the default modules and re map them to an object. + * Transforms an array of modules into an object. The last module will + * take precedence in case of duplicate modules + */ +export function transformModules( + modules: Exclude +) { + const remappedModules = modules.reduce((acc, moduleConfig) => { + if (moduleConfig.scope === "external" && !moduleConfig.key) { + throw new Error( + "External modules configuration must have a 'key'. Please provide a key for the module." + ) + } + + if ("disable" in moduleConfig && "key" in moduleConfig) { + acc[moduleConfig.key!] = moduleConfig + } + + // TODO: handle external modules later + let serviceName: string = + "key" in moduleConfig && moduleConfig.key ? moduleConfig.key : "" + delete moduleConfig.key + + if (!serviceName && "resolve" in moduleConfig) { + if ( + isString(moduleConfig.resolve!) && + REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] + ) { + serviceName = REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] + acc[serviceName] = moduleConfig + return acc + } + + let resolution = isString(moduleConfig.resolve!) + ? normalizeImportPathWithSource(moduleConfig.resolve as string) + : moduleConfig.resolve + + const moduleExport = isString(resolution) + ? require(resolution) + : resolution + + const defaultExport = resolveExports(moduleExport).default + + const joinerConfig = + typeof defaultExport.service.prototype.__joinerConfig === "function" + ? defaultExport.service.prototype.__joinerConfig() ?? {} + : defaultExport.service.prototype.__joinerConfig ?? {} + + serviceName = joinerConfig.serviceName + + if (!serviceName) { + throw new Error( + `Module ${moduleConfig.resolve} doesn't have a serviceName. Please provide a 'key' for the module or check the service joiner config.` + ) + } + } + + acc[serviceName] = moduleConfig + + return acc + }, {}) + + // Remove any modules set to false + Object.keys(remappedModules).forEach((key) => { + if (remappedModules[key].disable) { + delete remappedModules[key] + } + }) + + return remappedModules as ConfigModule["modules"] +} + +/** + * The user API allow to use array of modules configuration. This method manage the loading of the + * user modules along side the default modules and re map them to an object. * * @param configModules */ @@ -225,67 +297,5 @@ function resolveModules( } } - const remappedModules = modules.reduce((acc, moduleConfig) => { - if (moduleConfig.scope === "external" && !moduleConfig.key) { - throw new Error( - "External modules configuration must have a 'key'. Please provide a key for the module." - ) - } - - if ("disable" in moduleConfig && "key" in moduleConfig) { - acc[moduleConfig.key!] = moduleConfig - } - - // TODO: handle external modules later - let serviceName: string = - "key" in moduleConfig && moduleConfig.key ? moduleConfig.key : "" - delete moduleConfig.key - - if (!serviceName && "resolve" in moduleConfig) { - if ( - isString(moduleConfig.resolve!) && - REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] - ) { - serviceName = REVERSED_MODULE_PACKAGE_NAMES[moduleConfig.resolve!] - acc[serviceName] = moduleConfig - return acc - } - - let resolution = isString(moduleConfig.resolve!) - ? normalizeImportPathWithSource(moduleConfig.resolve as string) - : moduleConfig.resolve - - const moduleExport = isString(resolution) - ? require(resolution) - : resolution - - const defaultExport = resolveExports(moduleExport).default - - const joinerConfig = - typeof defaultExport.service.prototype.__joinerConfig === "function" - ? defaultExport.service.prototype.__joinerConfig() ?? {} - : defaultExport.service.prototype.__joinerConfig ?? {} - - serviceName = joinerConfig.serviceName - - if (!serviceName) { - throw new Error( - `Module ${moduleConfig.resolve} doesn't have a serviceName. Please provide a 'key' for the module or check the service joiner config.` - ) - } - } - - acc[serviceName] = moduleConfig - - return acc - }, {}) - - // Remove any modules set to false - Object.keys(remappedModules).forEach((key) => { - if (remappedModules[key].disable) { - delete remappedModules[key] - } - }) - - return remappedModules as ConfigModule["modules"] + return transformModules(modules) } From f67d4032f91f78faf7f8431c5ef371fdab35fa0f Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 17:27:07 +0530 Subject: [PATCH 2/6] feat: merge plugin modules with config modules --- .../core/types/src/common/config-module.ts | 13 ++++--- .../core/utils/src/common/define-config.ts | 9 ++--- packages/core/utils/src/common/index.ts | 1 + .../utils/src/common/merge-plugin-modules.ts | 35 +++++++++++++++++++ packages/medusa/src/commands/db/generate.ts | 3 ++ packages/medusa/src/commands/db/migrate.ts | 7 +++- packages/medusa/src/commands/db/rollback.ts | 3 ++ packages/medusa/src/commands/db/sync-links.ts | 7 +++- packages/medusa/src/loaders/index.ts | 3 ++ 9 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 packages/core/utils/src/common/merge-plugin-modules.ts diff --git a/packages/core/types/src/common/config-module.ts b/packages/core/types/src/common/config-module.ts index 4dac6def09b6e..2796cf7d2fc58 100644 --- a/packages/core/types/src/common/config-module.ts +++ b/packages/core/types/src/common/config-module.ts @@ -944,6 +944,13 @@ type ExternalModuleDeclarationOverride = ExternalModuleDeclaration & { disable?: boolean } +/** + * Modules accepted by the defineConfig function + */ +export type InputConfigModules = Partial< + InternalModuleDeclarationOverride | ExternalModuleDeclarationOverride +>[] + /** * The configuration accepted by the "defineConfig" helper */ @@ -951,9 +958,7 @@ export type InputConfig = Partial< Omit & { admin: Partial modules: - | Partial< - InternalModuleDeclarationOverride | ExternalModuleDeclarationOverride - >[] + | InputConfigModules /** * @deprecated use the array instead */ @@ -967,5 +972,5 @@ export type PluginDetails = { id: string options: Record version: string - modules?: InputConfig["modules"] + modules?: InputConfigModules } diff --git a/packages/core/utils/src/common/define-config.ts b/packages/core/utils/src/common/define-config.ts index 5f6f1c6b32d40..53df6b4a34bd5 100644 --- a/packages/core/utils/src/common/define-config.ts +++ b/packages/core/utils/src/common/define-config.ts @@ -1,6 +1,7 @@ import { ConfigModule, InputConfig, + InputConfigModules, InternalModuleDeclaration, } from "@medusajs/types" import { @@ -112,8 +113,8 @@ export function defineConfig(config: InputConfig = {}): ConfigModule { * take precedence in case of duplicate modules */ export function transformModules( - modules: Exclude -) { + modules: InputConfigModules +): Exclude { const remappedModules = modules.reduce((acc, moduleConfig) => { if (moduleConfig.scope === "external" && !moduleConfig.key) { throw new Error( @@ -176,7 +177,7 @@ export function transformModules( } }) - return remappedModules as ConfigModule["modules"] + return remappedModules as Exclude } /** @@ -187,7 +188,7 @@ export function transformModules( */ function resolveModules( configModules: InputConfig["modules"] -): ConfigModule["modules"] { +): Exclude { /** * The default set of modules to always use. The end user can swap * the modules by providing an alternate implementation via their diff --git a/packages/core/utils/src/common/index.ts b/packages/core/utils/src/common/index.ts index f0421986ace0e..54585cb415f53 100644 --- a/packages/core/utils/src/common/index.ts +++ b/packages/core/utils/src/common/index.ts @@ -80,3 +80,4 @@ export * from "./trim-zeros" export * from "./upper-case-first" export * from "./validate-handle" export * from "./wrap-handler" +export * from "./merge-plugin-modules" diff --git a/packages/core/utils/src/common/merge-plugin-modules.ts b/packages/core/utils/src/common/merge-plugin-modules.ts new file mode 100644 index 0000000000000..71904069709e7 --- /dev/null +++ b/packages/core/utils/src/common/merge-plugin-modules.ts @@ -0,0 +1,35 @@ +import type { + PluginDetails, + ConfigModule, + InputConfigModules, +} from "@medusajs/types" +import { transformModules } from "./define-config" + +/** + * Mutates the configModules object and merges the plugin modules with + * the modules defined inside the user-config file + */ +export function mergePluginModules( + configModule: ConfigModule, + plugins: PluginDetails[] +) { + /** + * Create a flat array of all the modules exposed by the registered + * plugins + */ + const pluginsModules = plugins.reduce((result, plugin) => { + if (plugin.modules) { + result = result.concat(plugin.modules) + } + return result + }, [] as InputConfigModules) + + /** + * Merge plugin modules with the modules defined within the + * config file. + */ + configModule.modules = { + ...transformModules(pluginsModules), + ...configModule.modules, + } +} diff --git a/packages/medusa/src/commands/db/generate.ts b/packages/medusa/src/commands/db/generate.ts index 176fa7896ccb5..3380622738189 100644 --- a/packages/medusa/src/commands/db/generate.ts +++ b/packages/medusa/src/commands/db/generate.ts @@ -2,6 +2,7 @@ import { join } from "path" import { ContainerRegistrationKeys, MedusaError, + mergePluginModules, } from "@medusajs/framework/utils" import { LinkLoader } from "@medusajs/framework/links" import { logger } from "@medusajs/framework/logger" @@ -27,6 +28,8 @@ const main = async function ({ directory, modules }) { ) const plugins = await getResolvedPlugins(directory, configModule, true) + mergePluginModules(configModule, plugins) + const linksSourcePaths = plugins.map((plugin) => join(plugin.resolve, "links") ) diff --git a/packages/medusa/src/commands/db/migrate.ts b/packages/medusa/src/commands/db/migrate.ts index f7e7a4a356b97..ad0a897e0bf63 100644 --- a/packages/medusa/src/commands/db/migrate.ts +++ b/packages/medusa/src/commands/db/migrate.ts @@ -1,5 +1,8 @@ import { join } from "path" -import { ContainerRegistrationKeys } from "@medusajs/framework/utils" +import { + ContainerRegistrationKeys, + mergePluginModules, +} from "@medusajs/framework/utils" import { LinkLoader } from "@medusajs/framework/links" import { logger } from "@medusajs/framework/logger" import { MedusaAppLoader } from "@medusajs/framework" @@ -38,6 +41,8 @@ export async function migrate({ ) const plugins = await getResolvedPlugins(directory, configModule, true) + mergePluginModules(configModule, plugins) + const linksSourcePaths = plugins.map((plugin) => join(plugin.resolve, "links") ) diff --git a/packages/medusa/src/commands/db/rollback.ts b/packages/medusa/src/commands/db/rollback.ts index 2c09252ed2586..23038daf4b772 100644 --- a/packages/medusa/src/commands/db/rollback.ts +++ b/packages/medusa/src/commands/db/rollback.ts @@ -2,6 +2,7 @@ import { join } from "path" import { ContainerRegistrationKeys, MedusaError, + mergePluginModules, } from "@medusajs/framework/utils" import { LinkLoader } from "@medusajs/framework/links" import { logger } from "@medusajs/framework/logger" @@ -27,6 +28,8 @@ const main = async function ({ directory, modules }) { ) const plugins = await getResolvedPlugins(directory, configModule, true) + mergePluginModules(configModule, plugins) + const linksSourcePaths = plugins.map((plugin) => join(plugin.resolve, "links") ) diff --git a/packages/medusa/src/commands/db/sync-links.ts b/packages/medusa/src/commands/db/sync-links.ts index 59d6da7008460..7d91ab0c0112e 100644 --- a/packages/medusa/src/commands/db/sync-links.ts +++ b/packages/medusa/src/commands/db/sync-links.ts @@ -2,7 +2,10 @@ import boxen from "boxen" import chalk from "chalk" import { join } from "path" import checkbox from "@inquirer/checkbox" -import { ContainerRegistrationKeys } from "@medusajs/framework/utils" +import { + ContainerRegistrationKeys, + mergePluginModules, +} from "@medusajs/framework/utils" import { LinkMigrationsPlannerAction } from "@medusajs/framework/types" import { LinkLoader } from "@medusajs/framework/links" import { logger } from "@medusajs/framework/logger" @@ -188,6 +191,8 @@ const main = async function ({ directory, executeSafe, executeAll }) { const medusaAppLoader = new MedusaAppLoader() const plugins = await getResolvedPlugins(directory, configModule, true) + mergePluginModules(configModule, plugins) + const linksSourcePaths = plugins.map((plugin) => join(plugin.resolve, "links") ) diff --git a/packages/medusa/src/loaders/index.ts b/packages/medusa/src/loaders/index.ts index e8ea0b2afa594..4ea3deb44410f 100644 --- a/packages/medusa/src/loaders/index.ts +++ b/packages/medusa/src/loaders/index.ts @@ -7,6 +7,7 @@ import { import { ContainerRegistrationKeys, GraphQLSchema, + mergePluginModules, promiseAll, } from "@medusajs/framework/utils" import { asValue } from "awilix" @@ -147,6 +148,8 @@ export default async ({ ) const plugins = await getResolvedPlugins(rootDirectory, configModule, true) + mergePluginModules(configModule, plugins) + const linksSourcePaths = plugins.map((plugin) => join(plugin.resolve, "links") ) From 64ca205a6a188d96215ec0b965e556093e9c0c91 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 17:27:55 +0530 Subject: [PATCH 3/6] refactor: update default src path for plugins --- packages/medusa/src/loaders/helpers/resolve-plugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/loaders/helpers/resolve-plugins.ts b/packages/medusa/src/loaders/helpers/resolve-plugins.ts index 529c9b9ac5655..b2a200cda41e3 100644 --- a/packages/medusa/src/loaders/helpers/resolve-plugins.ts +++ b/packages/medusa/src/loaders/helpers/resolve-plugins.ts @@ -59,7 +59,7 @@ async function resolvePlugin( const name = pkgJSON.contents.name || pluginPath const srcDir = pkgJSON.contents.main ? path.dirname(pkgJSON.contents.main) - : "build" + : ".medusa/server/src" const resolve = path.join(resolvedPath, srcDir) const modules = await readDir(path.join(resolve, "modules"), { From 2feff80286a0de43cf9e01e60c58dd678565a9cd Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 18:09:24 +0530 Subject: [PATCH 4/6] refactor: use fixed output for plugin src --- .../__tests__/get-resolved-plugins.spec.ts | 25 +++++++++++++------ .../src/loaders/helpers/resolve-plugins.ts | 8 +++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/medusa/src/loaders/__tests__/get-resolved-plugins.spec.ts b/packages/medusa/src/loaders/__tests__/get-resolved-plugins.spec.ts index 2aa6cbd8f2423..7857045da09e1 100644 --- a/packages/medusa/src/loaders/__tests__/get-resolved-plugins.spec.ts +++ b/packages/medusa/src/loaders/__tests__/get-resolved-plugins.spec.ts @@ -33,7 +33,7 @@ describe("getResolvedPlugins | relative paths", () => { expect(plugins).toEqual([ { - resolve: path.join(fs.basePath, "./plugins/dummy/build"), + resolve: path.join(fs.basePath, "./plugins/dummy/.medusa/server/src"), name: "my-dummy-plugin", id: "my-dummy-plugin", options: { apiKey: "asecret" }, @@ -48,7 +48,10 @@ describe("getResolvedPlugins | relative paths", () => { name: "my-dummy-plugin", version: "1.0.0", }) - await fs.create("plugins/dummy/build/modules/blog/index.js", ``) + await fs.create( + "plugins/dummy/.medusa/server/src/modules/blog/index.js", + `` + ) const plugins = await getResolvedPlugins( fs.basePath, @@ -67,7 +70,7 @@ describe("getResolvedPlugins | relative paths", () => { expect(plugins).toEqual([ { - resolve: path.join(fs.basePath, "./plugins/dummy/build"), + resolve: path.join(fs.basePath, "./plugins/dummy/.medusa/server/src"), name: "my-dummy-plugin", id: "my-dummy-plugin", options: { apiKey: "asecret" }, @@ -77,7 +80,7 @@ describe("getResolvedPlugins | relative paths", () => { options: { apiKey: "asecret", }, - resolve: "./plugins/dummy/build/modules/blog", + resolve: "./plugins/dummy/.medusa/server/src/modules/blog", }, ], }, @@ -132,7 +135,10 @@ describe("getResolvedPlugins | package reference", () => { expect(plugins).toEqual([ { - resolve: path.join(fs.basePath, "node_modules/@plugins/dummy/build"), + resolve: path.join( + fs.basePath, + "node_modules/@plugins/dummy/.medusa/server/src" + ), name: "my-dummy-plugin", id: "my-dummy-plugin", options: { apiKey: "asecret" }, @@ -149,7 +155,7 @@ describe("getResolvedPlugins | package reference", () => { version: "1.0.0", }) await fs.create( - "node_modules/@plugins/dummy/build/modules/blog/index.js", + "node_modules/@plugins/dummy/.medusa/server/src/modules/blog/index.js", `` ) @@ -170,7 +176,10 @@ describe("getResolvedPlugins | package reference", () => { expect(plugins).toEqual([ { - resolve: path.join(fs.basePath, "node_modules/@plugins/dummy/build"), + resolve: path.join( + fs.basePath, + "node_modules/@plugins/dummy/.medusa/server/src" + ), name: "my-dummy-plugin", id: "my-dummy-plugin", options: { apiKey: "asecret" }, @@ -180,7 +189,7 @@ describe("getResolvedPlugins | package reference", () => { options: { apiKey: "asecret", }, - resolve: "@plugins/dummy/build/modules/blog", + resolve: "@plugins/dummy/.medusa/server/src/modules/blog", }, ], }, diff --git a/packages/medusa/src/loaders/helpers/resolve-plugins.ts b/packages/medusa/src/loaders/helpers/resolve-plugins.ts index b2a200cda41e3..8846dfd339098 100644 --- a/packages/medusa/src/loaders/helpers/resolve-plugins.ts +++ b/packages/medusa/src/loaders/helpers/resolve-plugins.ts @@ -4,6 +4,7 @@ import { isString, readDir } from "@medusajs/framework/utils" import { ConfigModule, PluginDetails } from "@medusajs/framework/types" const MEDUSA_APP_SOURCE_PATH = "src" +const MEDUSA_PLUGIN_SOURCE_PATH = ".medusa/server/src" export const MEDUSA_PROJECT_NAME = "project-plugin" function createPluginId(name: string): string { @@ -57,11 +58,8 @@ async function resolvePlugin( const resolvedPath = path.dirname(pkgJSON.path) const name = pkgJSON.contents.name || pluginPath - const srcDir = pkgJSON.contents.main - ? path.dirname(pkgJSON.contents.main) - : ".medusa/server/src" - const resolve = path.join(resolvedPath, srcDir) + const resolve = path.join(resolvedPath, MEDUSA_PLUGIN_SOURCE_PATH) const modules = await readDir(path.join(resolve, "modules"), { ignoreMissing: true, }) @@ -75,7 +73,7 @@ async function resolvePlugin( version: pkgJSON.contents.version || "0.0.0", modules: modules.map((mod) => { return { - resolve: `${pluginPath}/${srcDir}/modules/${mod.name}`, + resolve: `${pluginPath}/${MEDUSA_PLUGIN_SOURCE_PATH}/modules/${mod.name}`, options: pluginOptions, } }), From eecad8d2e64d46faeaefcf00d05a74b3286a3a6f Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 9 Jan 2025 20:14:32 +0530 Subject: [PATCH 5/6] Create thin-games-worry.md --- .changeset/thin-games-worry.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/thin-games-worry.md diff --git a/.changeset/thin-games-worry.md b/.changeset/thin-games-worry.md new file mode 100644 index 0000000000000..ce92c6957fd70 --- /dev/null +++ b/.changeset/thin-games-worry.md @@ -0,0 +1,7 @@ +--- +"@medusajs/medusa": patch +"@medusajs/types": patch +"@medusajs/utils": patch +--- + +Feat/merge plugin modules From 6dd164f783d632c927e0a48c50090dcf7387ccdd Mon Sep 17 00:00:00 2001 From: adrien2p Date: Fri, 10 Jan 2025 10:31:47 +0100 Subject: [PATCH 6/6] fix modules disabling mechanism --- packages/core/modules-sdk/src/medusa-app.ts | 3 ++- .../common/__tests__/define-config.spec.ts | 5 +++- .../__tests__/transform-modules.spec.ts | 27 +++++++++++++++++-- .../core/utils/src/common/define-config.ts | 7 ----- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/core/modules-sdk/src/medusa-app.ts b/packages/core/modules-sdk/src/medusa-app.ts index 05ea45d5f5737..7c9956f6c3cf7 100644 --- a/packages/core/modules-sdk/src/medusa-app.ts +++ b/packages/core/modules-sdk/src/medusa-app.ts @@ -106,7 +106,8 @@ export async function loadModules(args: { let declaration: any = {} let definition: Partial | undefined = undefined - if (mod === false) { + // TODO: We are keeping mod === false for backward compatibility for now + if (mod === false || (isObject(mod) && "disable" in mod && mod.disable)) { continue } diff --git a/packages/core/utils/src/common/__tests__/define-config.spec.ts b/packages/core/utils/src/common/__tests__/define-config.spec.ts index f49263b6065dc..2ceacf2f9488f 100644 --- a/packages/core/utils/src/common/__tests__/define-config.spec.ts +++ b/packages/core/utils/src/common/__tests__/define-config.spec.ts @@ -800,7 +800,7 @@ describe("defineConfig", function () { `) }) - it("should not include disabled modules", function () { + it("should include disabled modules", function () { expect( defineConfig({ projectConfig: { @@ -837,6 +837,9 @@ describe("defineConfig", function () { "cache": { "resolve": "@medusajs/medusa/cache-inmemory", }, + "cart": { + "disable": true, + }, "currency": { "resolve": "@medusajs/medusa/currency", }, diff --git a/packages/core/utils/src/common/__tests__/transform-modules.spec.ts b/packages/core/utils/src/common/__tests__/transform-modules.spec.ts index 2f867afc8ad7c..ff4295d63b05b 100644 --- a/packages/core/utils/src/common/__tests__/transform-modules.spec.ts +++ b/packages/core/utils/src/common/__tests__/transform-modules.spec.ts @@ -36,7 +36,7 @@ describe("transformModules", () => { }) }) - test("remove module when its disabled at a later stage in the array", () => { + test("should manage loading priority of modules when its disabled at a later stage in the array", () => { const modules = transformModules([ { resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], @@ -47,6 +47,29 @@ describe("transformModules", () => { }, ]) - expect(modules).toEqual({}) + expect(modules).toEqual({ + cache: { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + disable: true, + }, + }) + }) + + test("should manage loading priority of modules when its not disabled at a later stage in the array", () => { + const modules = transformModules([ + { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + disable: true, + }, + { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + }, + ]) + + expect(modules).toEqual({ + cache: { + resolve: MODULE_PACKAGE_NAMES[Modules.CACHE], + }, + }) }) }) diff --git a/packages/core/utils/src/common/define-config.ts b/packages/core/utils/src/common/define-config.ts index 53df6b4a34bd5..117f262f0abcd 100644 --- a/packages/core/utils/src/common/define-config.ts +++ b/packages/core/utils/src/common/define-config.ts @@ -170,13 +170,6 @@ export function transformModules( return acc }, {}) - // Remove any modules set to false - Object.keys(remappedModules).forEach((key) => { - if (remappedModules[key].disable) { - delete remappedModules[key] - } - }) - return remappedModules as Exclude }