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:
1 parent
6698f3ab7b
commit
61977bd392
2 files changed
+40
-8
No files matched your search
@@ -5,6 +5,8 @@ import {
|
|||||||
ModuleExports,
|
ModuleExports,
|
||||||
ModuleResolution,
|
ModuleResolution,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
import { join } from "path"
|
||||||
|
|
||||||
import { isObject, isString } 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"
|
||||||
@@ -56,13 +58,33 @@ export const registerMedusaModule = (
|
|||||||
return moduleResolutions
|
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(
|
function getCustomModuleResolution(
|
||||||
key: string,
|
key: string,
|
||||||
moduleConfig: InternalModuleDeclaration | string
|
moduleConfig: InternalModuleDeclaration | string
|
||||||
): ModuleResolution {
|
): ModuleResolution {
|
||||||
const resolutionPath = resolveCwd(
|
const originalPath = normalizePath(
|
||||||
isString(moduleConfig) ? moduleConfig : (moduleConfig.resolve as string)
|
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||||
)
|
)
|
||||||
|
const resolutionPath = resolveCwd(originalPath)
|
||||||
|
|
||||||
const conf = isObject(moduleConfig)
|
const conf = isObject(moduleConfig)
|
||||||
? moduleConfig
|
? moduleConfig
|
||||||
@@ -135,9 +157,10 @@ function getInternalModuleResolution(
|
|||||||
// 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 isStr = isString(moduleConfig)
|
const isStr = isString(moduleConfig)
|
||||||
if (isStr || (isObj && moduleConfig.resolve)) {
|
if (isStr || (isObj && moduleConfig.resolve)) {
|
||||||
resolutionPath = resolveCwd(
|
const originalPath = normalizePath(
|
||||||
isStr ? moduleConfig : (moduleConfig.resolve as string)
|
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||||
)
|
)
|
||||||
|
resolutionPath = resolveCwd(originalPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
const moduleDeclaration = isObj ? moduleConfig : {}
|
const moduleDeclaration = isObj ? moduleConfig : {}
|
||||||
|
|||||||
@@ -208,9 +208,17 @@ export async function loadModuleMigrations(
|
|||||||
async function importAllFromDir(path: string) {
|
async function importAllFromDir(path: string) {
|
||||||
let filesToLoad: string[] = []
|
let filesToLoad: string[] = []
|
||||||
|
|
||||||
|
const excludedExtensions = [".ts.map", ".js.map", ".d.ts"]
|
||||||
|
|
||||||
await readdir(path).then((files) => {
|
await readdir(path).then((files) => {
|
||||||
files.forEach((file) => {
|
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 filePath = join(path, file)
|
||||||
const stats = statSync(filePath)
|
const stats = statSync(filePath)
|
||||||
|
|
||||||
@@ -219,7 +227,6 @@ async function importAllFromDir(path: string) {
|
|||||||
} else if (stats.isFile()) {
|
} else if (stats.isFile()) {
|
||||||
filesToLoad.push(filePath)
|
filesToLoad.push(filePath)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return filesToLoad
|
return filesToLoad
|
||||||
@@ -237,8 +244,10 @@ async function loadResources(
|
|||||||
moduleResolution: ModuleResolution,
|
moduleResolution: ModuleResolution,
|
||||||
logger: Logger
|
logger: Logger
|
||||||
): Promise<ModuleResource> {
|
): Promise<ModuleResource> {
|
||||||
const modulePath = moduleResolution.resolutionPath as string
|
let modulePath = moduleResolution.resolutionPath as string
|
||||||
let normalizedPath = modulePath.replace("index.js", "")
|
let normalizedPath = modulePath
|
||||||
|
.replace("index.js", "")
|
||||||
|
.replace("index.ts", "")
|
||||||
normalizedPath = resolve(normalizedPath)
|
normalizedPath = resolve(normalizedPath)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in new issue
Block a user