fix: load custom modules by path (#6312)

What:
medusa-config.js loading custom modules by their path.

```typescript
{
  modules: {
    internalModule: {
      scope: MODULE_SCOPE.INTERNAL,
      resources: MODULE_RESOURCE_TYPE.SHARED,
      resolve: "./internal_module",
      definition: {
        key: "internalModule",
        registrationName: "internalModule",
      },
    },
  }
}
```
This commit is contained in:
Carlos R. L. Rodrigues
2024-02-04 14:26:34 +00:00
committed by GitHub
parent 58baead34e
commit 96ba49329b
13 changed files with 14 additions and 35 deletions
@@ -18,7 +18,6 @@ describe("module definitions loader", () => {
label: "TestService",
isLegacy: true,
isRequired: false,
canOverride: true,
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
@@ -94,7 +94,7 @@ function getInternalModuleResolution(
// If user added a module and it's overridable, we resolve that instead
const isString = typeof moduleConfig === "string"
if (definition.canOverride && (isString || (isObj && moduleConfig.resolve))) {
if (isString || (isObj && moduleConfig.resolve)) {
resolutionPath = !moduleExports
? resolveCwd(isString ? moduleConfig : (moduleConfig.resolve as string))
: // Explicitly assign an empty string, later, we will check if the value is exactly false.
@@ -29,12 +29,12 @@ export async function loadInternalModule(
// the exports. This is useful when a package export an initialize function which will bootstrap itself and therefore
// does not need to import the package that is currently being loaded as it would create a
// circular reference.
const path = resolution.resolutionPath as string
const modulePath = resolution.resolutionPath as string
if (resolution.moduleExports) {
loadedModule = resolution.moduleExports
} else {
loadedModule = await import(path)
loadedModule = await import(modulePath)
loadedModule = (loadedModule as any).default
}
} catch (error) {