-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.ts
110 lines (95 loc) · 3.43 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
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
import fs from "node:fs";
import path from "node:path";
import type { Plugin } from "vite";
interface PyodidePluginOptions {
base: string;
entryPoint?: string;
}
function pyodidePlugin(options: PyodidePluginOptions): Plugin {
const virtualModuleId = "virtual:pyodide-files";
const resolvedVirtualModuleId = `\0${virtualModuleId}`;
// Move pythonFiles inside the plugin function
let pythonFiles: Record<string, string> = {};
const loadPythonFilesRecursively = (dir: string, baseDir: string) => {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
const filePath = path.join(dir, file.name);
const relativePath = path.relative(path.dirname(baseDir), filePath);
if (file.isDirectory()) {
loadPythonFilesRecursively(filePath, baseDir);
} else if (file.isFile() && file.name.endsWith(".py")) {
const content = fs.readFileSync(filePath, "utf-8");
pythonFiles[relativePath] = content;
}
}
};
const loadPythonFiles = () => {
const basePath = path.resolve(options.base);
pythonFiles = {}; // Reset pythonFiles before loading
loadPythonFilesRecursively(basePath, basePath);
return pythonFiles; // Return the updated pythonFiles
};
return {
name: "vite-plugin-pyodide",
handleHotUpdate({ file, server }) {
if (file.endsWith(".py")) {
loadPythonFiles();
server.ws.send({ type: "full-reload" });
const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (mod) {
server.moduleGraph.invalidateModule(mod);
}
}
},
buildStart() {
loadPythonFiles();
},
resolveId(id: string) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
load(id: string) {
if (id === resolvedVirtualModuleId) {
// Call loadPythonFiles() here to get the latest content
const files = loadPythonFiles();
const baseName = path.basename(options.base);
const entryPointFullPath = options.entryPoint
? path.join(baseName, options.entryPoint)
: "";
const entryPointContent = entryPointFullPath ? files[entryPointFullPath] : "";
return `
export async function setupPyodideFiles(pyodide) {
const files = ${JSON.stringify(files)};
for (const [filename, content] of Object.entries(files)) {
const dirs = filename.split('/').slice(0, -1);
let currentDir = '';
for (const dir of dirs) {
currentDir = currentDir ? \`\${currentDir}/\${dir}\` : dir;
try {
pyodide.FS.mkdir(currentDir);
} catch (e) {
// Directory might already exist, ignore the error
}
}
pyodide.FS.writeFile(filename, content);
}
}
export function runEntryPoint(pyodide) {
const entryPointContent = ${JSON.stringify(entryPointContent)};
if (entryPointContent) {
pyodide.runPython(entryPointContent);
}
}
export async function runEntryPointAsync(pyodide) {
const entryPointContent = ${JSON.stringify(entryPointContent)};
if (entryPointContent) {
await pyodide.runPythonAsync(entryPointContent);
}
}
`;
}
},
};
}
export default pyodidePlugin;