-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathresolve.ts
More file actions
76 lines (63 loc) · 2.07 KB
/
resolve.ts
File metadata and controls
76 lines (63 loc) · 2.07 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve'
import { expiringMap } from './expiring-map'
const fileSystem = new CachedInputFileSystem(fs, 30_000)
const esmResolver = ResolverFactory.createResolver({
fileSystem,
useSyncFileSystemCalls: true,
extensions: ['.mjs', '.js'],
mainFields: ['module'],
conditionNames: ['node', 'import'],
})
const cjsResolver = ResolverFactory.createResolver({
fileSystem,
useSyncFileSystemCalls: true,
extensions: ['.js', '.cjs'],
mainFields: ['main'],
conditionNames: ['node', 'require'],
})
const cssResolver = ResolverFactory.createResolver({
fileSystem,
useSyncFileSystemCalls: true,
extensions: ['.css'],
mainFields: ['style'],
conditionNames: ['style'],
})
// This is a long-lived cache for resolved modules whether they exist or not
// Because we're compatible with a large number of plugins, we need to check
// for the existence of a module before attempting to import it. This cache
// is used to mitigate the cost of that check because Node.js does not cache
// failed module resolutions making repeated checks very expensive.
const resolveCache = expiringMap<string, string | null>(30_000)
export function maybeResolve(name: string) {
let modpath = resolveCache.get(name)
if (modpath === undefined) {
try {
modpath = resolveJsFrom(fileURLToPath(import.meta.url), name)
resolveCache.set(name, modpath)
} catch {
resolveCache.set(name, null)
return null
}
}
return modpath
}
export async function loadIfExists<T>(name: string): Promise<T | null> {
let modpath = maybeResolve(name)
if (modpath) {
let mod = await import(name)
return mod.default ?? mod
}
return null
}
export function resolveJsFrom(base: string, id: string): string {
try {
return esmResolver.resolveSync({}, base, id) || id
} catch {
return cjsResolver.resolveSync({}, base, id) || id
}
}
export function resolveCssFrom(base: string, id: string) {
return cssResolver.resolveSync({}, base, id) || id
}