-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.ts
More file actions
127 lines (109 loc) · 3.8 KB
/
build.ts
File metadata and controls
127 lines (109 loc) · 3.8 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
125
126
127
import { readFileSync, unlinkSync, writeFileSync } from "node:fs";
// Read package.json to get Pyodide version
const pkg = JSON.parse(readFileSync("package.json", "utf-8"));
const pyodideVersion = pkg.dependencies.pyodide.replace("^", "");
// Update Python pyproject.toml with Pyodide version
function updatePyprojectVersion() {
const pyprojectPath = "src/python/pyproject.toml";
const pyproject = readFileSync(pyprojectPath, "utf-8");
const updatedPyproject = pyproject.replace(
/pyodide-py>=[\d.]+/,
`pyodide-py>=${pyodideVersion}`,
);
writeFileSync(pyprojectPath, updatedPyproject);
}
// Inject Pyodide version into URLs
function injectPyodideVersion(source: string): string {
return source.replace(
/https:\/\/cdn\.jsdelivr\.net\/pyodide\/v[\d.]+\/full\//g,
`https://cdn.jsdelivr.net/pyodide/v${pyodideVersion}/full/`,
);
}
// Prepare TypeScript source with inlined Python code
function prepareEmbedSource(): string {
const pythonCode = readFileSync("src/python/console.py", "utf-8");
writeFileSync(
"src/console-code.ts",
`export const CONSOLE_PY = ${JSON.stringify(pythonCode)};`,
);
const tsSource = readFileSync("src/embed.ts", "utf-8");
return (
`import { CONSOLE_PY } from './console-code';\n` +
tsSource
.replace(
/function getConsoleCode\(\): Promise<string> \{\s+if \(!consoleCodePromise\) \{\s+consoleCodePromise = fetch\("\/python\/console\.py"\)\.then\(\(r\) => r\.text\(\)\);\s+\}\s+return consoleCodePromise;\s+\}/,
`function getConsoleCode(): Promise<string> {\n if (!consoleCodePromise) {\n consoleCodePromise = Promise.resolve(CONSOLE_PY);\n }\n return consoleCodePromise;\n}`,
)
.replace(
/https:\/\/cdn\.jsdelivr\.net\/pyodide\/v[\d.]+\/full\//g,
`https://cdn.jsdelivr.net/pyodide/v${pyodideVersion}/full/`,
)
);
}
// Prepare wrapper source with Pyodide version
function prepareWrapperSource(): string {
const wrapperSource = readFileSync("src/wrapper.js", "utf-8");
return injectPyodideVersion(wrapperSource);
}
function prepareComponentSource(): string {
const componentSource = readFileSync("src/component.ts", "utf-8");
// Update import to point to build version of embed
return componentSource.replace(
/from ['"]\.\/embed\.js['"]/,
`from './embed.build.js'`,
);
}
// Clean up temporary build files
function cleanup() {
unlinkSync("src/embed.build.ts");
unlinkSync("src/component.build.ts");
unlinkSync("src/wrapper.build.js");
unlinkSync("src/console-code.ts");
}
// Main build process
async function build() {
// Update Python dependencies
updatePyprojectVersion();
// Prepare sources
const embedSource = prepareEmbedSource();
writeFileSync("src/embed.build.ts", embedSource);
const componentSource = prepareComponentSource();
writeFileSync("src/component.build.ts", componentSource);
const wrapperSource = prepareWrapperSource();
writeFileSync("src/wrapper.build.js", wrapperSource);
// Bundle the ESM module
const esmResult = await Bun.build({
entrypoints: ["src/component.build.ts"],
outdir: "dist",
naming: "pyrepl.esm.js",
minify: true,
splitting: true,
target: "browser",
format: "esm",
define: {
"process.env.NODE_ENV": '"production"',
},
});
if (!esmResult.success) {
console.error("ESM build failed:", esmResult.logs);
process.exit(1);
}
console.log("Built dist/pyrepl.esm.js");
// Bundle the wrapper
const wrapperResult = await Bun.build({
entrypoints: ["src/wrapper.build.js"],
outdir: "dist",
naming: "pyrepl.js",
minify: true,
target: "browser",
format: "iife",
});
if (!wrapperResult.success) {
console.error("Wrapper build failed:", wrapperResult.logs);
process.exit(1);
}
console.log("Built dist/pyrepl.js (wrapper)");
// Clean up
cleanup();
}
build();