forked from electron-userland/electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
59 lines (52 loc) · 1.69 KB
/
resolve.ts
File metadata and controls
59 lines (52 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { log } from "builder-util/out/log"
import debug from "debug"
import * as path from "path"
import * as requireMaybe from "../../helpers/dynamic-import"
import { resolveFromProject } from "./projectModuleResolver"
export async function resolveModule<T>(type: string | undefined, name: string): Promise<T> {
try {
return requireMaybe.dynamicImportMaybe(name)
} catch (error: any) {
log.error({ moduleName: name, message: error.message ?? error.stack }, "Unable to dynamically `import` or `require`")
throw error
}
}
export async function resolveFunction<T>(type: string | undefined, executor: T | string, name: string, projectDir?: string): Promise<T> {
if (executor == null || typeof executor !== "string") {
// is already function or explicitly ignored by user
return executor
}
let p = executor as string
if (p.startsWith(".")) {
p = path.resolve(projectDir || process.cwd(), p)
}
try {
// First try project context resolution (for pnpm compatibility)
if (projectDir && !path.isAbsolute(p)) {
const resolved = resolveFromProject({
projectDir,
moduleSpecifier: p,
optional: true,
})
if (resolved !== null) {
p = resolved
}
}
// Fallback to standard resolution
if (!path.isAbsolute(p)) {
p = require.resolve(p)
}
} catch (e: any) {
debug(e)
p = path.resolve(projectDir || process.cwd(), p)
}
const m: any = await resolveModule(type, p)
const namedExport = m[name]
if (namedExport == null) {
return m.default || m
} else {
return namedExport
}
}
// Re-export for convenience
export { resolveFromProject, moduleExistsInProject } from "./projectModuleResolver"