-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
plugin.ts
44 lines (38 loc) · 1.21 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
42
43
44
// TODO: Maybe add support for pip: namespace that automatically installs the module if it's not found.
// deno-lint-ignore-file no-explicit-any
import { plugin } from "bun";
import { python } from "./mod.ts";
const { dir } = python.builtins;
const { SourceFileLoader } = python.import("importlib.machinery");
export function exportModule(mod: any) {
const props = dir(mod).valueOf();
const exports: Record<string, any> = {};
for (let prop of props) {
prop = prop.toString();
exports[prop] = mod[prop];
}
return exports;
}
plugin({
name: "Python Loader",
setup: (build) => {
build.onLoad({ filter: /\.py$/ }, (args) => {
const name = args.path.split("/").pop()!.split(".py")[0];
const exports = SourceFileLoader(name, args.path).load_module();
return {
exports: exportModule(exports),
loader: "object",
};
});
build.onResolve({ filter: /.+/, namespace: "python" }, (args) => {
return { path: args.path, namespace: "python" };
});
build.onLoad({ filter: /.+/, namespace: "python" }, (args) => {
const exports = python.import(args.path);
return {
exports: exportModule(exports),
loader: "object",
};
});
},
});