jiti-hooks.mjs
3.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { isBuiltin } from "node:module";
import { createJiti } from "./jiti.mjs";
let jiti;
// https://nodejs.org/api/module.html#initialize
export async function initialize() {
jiti = createJiti();
}
// https://nodejs.org/api/module.html#resolvespecifier-context-nextresolve
export async function resolve(specifier, context, nextResolve) {
if (_shouldSkip(specifier)) {
return nextResolve(specifier, context);
}
const resolvedPath = jiti.esmResolve(specifier, {
parentURL: context?.parentURL,
conditions: context?.conditions,
});
return {
url: resolvedPath,
shortCircuit: true,
};
}
// https://nodejs.org/api/module.html#loadurl-context-nextload
export async function load(url, context, nextLoad) {
if (_shouldSkip(url)) {
return nextLoad(url, context);
}
const filename = fileURLToPath(url);
if (url.endsWith(".js")) {
const pkg = await _findClosestPackageJson(dirname(filename));
if (pkg && pkg.type === "module") {
return nextLoad(url, context);
}
}
const rawSource = await readFile(filename, "utf8");
if (url.endsWith(".json")) {
const pkg = await _findClosestPackageJson(dirname(filename));
return pkg && pkg.type === "module"
? {
source: `export default ${rawSource}`,
format: "module",
shortCircuit: true,
}
: {
source: `module.exports = ${rawSource}`,
format: "commonjs",
shortCircuit: true,
};
}
const transpiledSource = jiti.transform({
source: rawSource,
filename: filename,
ts: url.endsWith("ts"),
retainLines: true,
async: true,
jsx: jiti.options.jsx,
});
if (url.endsWith(".js") && !transpiledSource.includes("jitiImport")) {
return {
source: transpiledSource,
format: "commonjs",
shortCircuit: true,
};
}
return {
source: _wrapSource(transpiledSource, filename),
format: "module",
shortCircuit: true,
};
}
function _wrapSource(source, filename) {
const _jitiPath = new URL("jiti.mjs", import.meta.url).href;
return /*js*/ `import { createJiti as __createJiti__ } from ${JSON.stringify(_jitiPath)};async function _module(exports, require, module, __filename, __dirname, jitiImport) { ${source}\n};
// GENERATED BY JITI ESM LOADER
const filename = ${JSON.stringify(filename)};
const dirname = ${JSON.stringify(dirname(filename))};
const jiti = __createJiti__(filename);
const module = { exports: Object.create(null) };
await _module(module.exports, jiti, module, filename, dirname, jiti.import);
if (module.exports && module.exports.__JITI_ERROR__) {
const { filename, line, column, code, message } =
module.exports.__JITI_ERROR__;
const loc = [filename, line, column].join(':');
const err = new Error(code + ": " + message + " " + loc);
Error.captureStackTrace(err, _module);
throw err;
}
export default module.exports;
`;
}
function _shouldSkip(url) {
return (
!jiti ||
url.endsWith(".mjs") ||
url.endsWith(".cjs") ||
(!url.startsWith("./") && !url.startsWith("file://")) ||
isBuiltin(url)
);
}
async function _findClosestPackageJson(dir) {
if (dir === "/") return null;
const packageJsonPath = join(dir, "package.json");
if (existsSync(packageJsonPath)) {
return JSON.parse(await readFile(packageJsonPath, "utf8"));
}
return _findClosestPackageJson(dirname(dir));
}