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:
@@ -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,17 +208,24 @@ 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")) {
|
||||
const filePath = join(path, file)
|
||||
const stats = statSync(filePath)
|
||||
if (
|
||||
file.startsWith("index.") ||
|
||||
excludedExtensions.some((ext) => file.endsWith(ext))
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
// TODO: should we handle that? dont think so but I put that here for discussion
|
||||
} else if (stats.isFile()) {
|
||||
filesToLoad.push(filePath)
|
||||
}
|
||||
const filePath = join(path, file)
|
||||
const stats = statSync(filePath)
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
// TODO: should we handle that? dont think so but I put that here for discussion
|
||||
} else if (stats.isFile()) {
|
||||
filesToLoad.push(filePath)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user