-
Notifications
You must be signed in to change notification settings - Fork 8
/
sama-loader.mjs
54 lines (40 loc) · 1.39 KB
/
sama-loader.mjs
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
import path from "node:path"
import fs from "node:fs"
const SAMA_ALIAS = "@sama"
const SAMA_XMPP_ALIAS = "@sama-xmpp"
const loaderFilePath = new URL(import.meta.url).pathname
const dirPath = path.dirname(loaderFilePath)
const samaCoreDirPath = path.join(dirPath, "app")
const samaXmppApiDirPath = path.join(dirPath, "APIs", "XMPP")
const samaXmppApiIndexPath = path.join(samaXmppApiDirPath, "index.js")
const samaXmppApiExists = fs.existsSync(samaXmppApiIndexPath)
export async function resolve(specifier, context, nextResolve) {
if (specifier.startsWith(`${SAMA_ALIAS}/`)) {
const filePath = specifier.replace(`${SAMA_ALIAS}/`, "")
const fileFullPath = path.join(samaCoreDirPath, filePath)
return {
shortCircuit: true,
url: new URL(`file://${fileFullPath}`).href,
}
}
if (specifier.startsWith(`${SAMA_XMPP_ALIAS}/`)) {
const filePath = specifier.replace(`${SAMA_XMPP_ALIAS}/`, "")
const fileFullPath = path.join(samaXmppApiDirPath, filePath)
return {
shortCircuit: true,
url: new URL(`file://${fileFullPath}`).href,
}
}
return nextResolve(specifier)
}
export async function load(url, context, nextLoad) {
const { format } = context
if (url.includes(samaXmppApiIndexPath) && !samaXmppApiExists) {
return {
format: format || "module",
shortCircuit: true,
source: "export default null",
}
}
return nextLoad(url)
}