-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-loader.mjs
More file actions
44 lines (37 loc) · 1.16 KB
/
Copy pathtest-loader.mjs
File metadata and controls
44 lines (37 loc) · 1.16 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
import { existsSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
const extensionRoot = path.dirname(fileURLToPath(import.meta.url))
function resolveCandidate(basePath) {
const candidates = [
basePath,
`${basePath}.ts`,
`${basePath}.tsx`,
path.join(basePath, 'index.ts'),
path.join(basePath, 'index.tsx'),
]
for (const candidate of candidates) {
if (existsSync(candidate)) {
return pathToFileURL(candidate).href
}
}
return null
}
export async function resolve(specifier, context, defaultResolve) {
if (specifier.startsWith('@/')) {
const resolved = resolveCandidate(path.join(extensionRoot, 'src', specifier.slice(2)))
if (resolved) {
return { url: resolved, shortCircuit: true }
}
}
if ((specifier.startsWith('./') || specifier.startsWith('../')) && !path.extname(specifier)) {
const parentDir = context.parentURL
? path.dirname(fileURLToPath(context.parentURL))
: extensionRoot
const resolved = resolveCandidate(path.resolve(parentDir, specifier))
if (resolved) {
return { url: resolved, shortCircuit: true }
}
}
return defaultResolve(specifier, context, defaultResolve)
}