chore: Allow modules to discover resources from ts/js and local directories (#7520)

**What**
Update the module resources discovery to account to the source directory to look into.

example:
```ts
user: { resolve: 'user' }
```
The above config will load the resources from the resolved path looking from the node modules
```ts
user: { resolve: './modules/user' }
```
The above config will load the resources from the local directory under dist if not run with ts node and under src otherwise
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2024-05-30 09:14:55 +00:00
1 parent 6698f3ab7b
commit 61977bd392
2 files changed
+40 -8

No files matched your search

@@ -5,6 +5,8 @@ import {
ModuleExports,
ModuleResolution,
} from "@medusajs/types"
import { join } from "path"
import { isObject, isString } from "@medusajs/utils"
import resolveCwd from "resolve-cwd"
import { ModulesDefinition } from "../definitions"
@@ -56,13 +58,33 @@ 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 resolutionPath = resolveCwd(
isString(moduleConfig) ? moduleConfig : (moduleConfig.resolve as string)
const originalPath = normalizePath(
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
)
const resolutionPath = resolveCwd(originalPath)
const conf = isObject(moduleConfig)
? moduleConfig
@@ -135,9 +157,10 @@ function getInternalModuleResolution(
// If user added a module and it's overridable, we resolve that instead
const isStr = isString(moduleConfig)
if (isStr || (isObj && moduleConfig.resolve)) {
resolutionPath = resolveCwd(
isStr ? moduleConfig : (moduleConfig.resolve as string)
const originalPath = normalizePath(
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
)
resolutionPath = resolveCwd(originalPath)
}
const moduleDeclaration = isObj ? moduleConfig : {}
@@ -208,9 +208,17 @@ export async function loadModuleMigrations(
async function importAllFromDir(path: string) {
let filesToLoad: string[] = []
const excludedExtensions = [".ts.map", ".js.map", ".d.ts"]
await readdir(path).then((files) => {
files.forEach((file) => {
if (file !== "index.js" && file.endsWith(".js")) {
if (
file.startsWith("index.") ||
excludedExtensions.some((ext) => file.endsWith(ext))
) {
return
}
const filePath = join(path, file)
const stats = statSync(filePath)
@@ -219,7 +227,6 @@ async function importAllFromDir(path: string) {
} else if (stats.isFile()) {
filesToLoad.push(filePath)
}
}
})
return filesToLoad
@@ -237,8 +244,10 @@ async function loadResources(
moduleResolution: ModuleResolution,
logger: Logger
): Promise<ModuleResource> {
const modulePath = moduleResolution.resolutionPath as string
let normalizedPath = modulePath.replace("index.js", "")
let modulePath = moduleResolution.resolutionPath as string
let normalizedPath = modulePath
.replace("index.js", "")
.replace("index.ts", "")
normalizedPath = resolve(normalizedPath)
try {