-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.ts
41 lines (38 loc) · 1.02 KB
/
plugin.ts
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
import { plugin } from "bun";
import { parse } from "path";
import python from "./index";
const { dir } = python.import("builtins");
const { SourceFileLoader } = python.import("importlib.machinery");
function convertExports(raw: any) {
return Object.fromEntries(
dir(raw)
.valueOf()
.map((key: string) => [key, raw[key]])
)
}
plugin({
name: "python",
target: "bun",
setup({ onResolve, onLoad }) {
onResolve({ filter: /.+/, namespace: "python" }, ({ path }) => {
return { path, namespace: "python" };
});
onLoad({ filter: /.+/, namespace: "python" }, ({ path }) => {
const raw = python.import(path);
const exports = convertExports(raw);
return {
loader: "object",
exports,
};
})
onLoad({ filter: /\.(py)$/ }, ({ path }) => {
const { name } = parse(path);
const raw = SourceFileLoader(name, path).load_module();
const exports = convertExports(raw);
return {
loader: "object",
exports,
};
});
},
});