forked from eliasku/fri3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite-zig.ts
113 lines (102 loc) · 3 KB
/
vite-zig.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
111
112
113
import { Logger, Plugin } from "vite";
import { spawn } from "node:child_process";
import { wasmopt } from "./wasmopt";
export interface ZigBuildOptions {
// path to zig source-code
watch?: RegExp;
// Path to build.zig
build?: string;
}
const run = (cmd: string, args: string[]): Promise<number> => {
const child = spawn(cmd, args, {
stdio: "inherit",
});
return new Promise((resolve, reject) => {
child.on("error", reject);
child.on("close", resolve);
});
};
export const zig = (options: ZigBuildOptions = {}): Plugin => {
let logger: Logger;
const build: string = options.build ?? "./build.zig";
const watch: RegExp = options.watch ?? /^.+\.zig$/;
// check if file should be trig recompilation
const checkFile = (filepath: string) => watch.test(filepath);
const runBuild = async (command: string) => {
const zig = "zig";
let ts = performance.now();
let errorsCount = 0;
const args = ["build", "--summary", "all", "--build-file", build];
if (command === "build") {
args.push("-Drelease=true");
}
const result = await run(zig, args);
const compileError = result !== 0;
if (compileError) {
++errorsCount;
}
logger.info(`[vite-zig] build is done ${(performance.now() - ts) | 0} ms`);
if (compileError) {
logger.error("[vite-zig] failed, check compile errors");
} else if (command == "build") {
try {
let ts = performance.now();
await wasmopt({
file: "zig-out/bin/main.wasm",
dis: "zig-out/bin/main.wast",
});
logger.info(
`[wasmopt] optimized in ${(performance.now() - ts) | 0} ms`,
);
} catch (err) {
logger.warn(`[wasmopt] error:`);
logger.error(err);
}
}
};
let changed = true;
let buildInProgress = false;
return {
name: "zig",
enforce: "pre",
async configResolved(config) {
logger = config.logger;
if (config.command === "serve") {
setInterval(async () => {
if (changed && !buildInProgress) {
changed = false;
buildInProgress = true;
await runBuild(config.command);
buildInProgress = false;
}
}, 200);
} else {
await runBuild(config.command);
}
},
async configureServer(server) {
server.watcher
.on("add", (filepath) => {
if (checkFile(filepath)) {
logger.info("[vite-zig] file is added: " + filepath);
changed = true;
}
})
.on("change", (filepath) => {
if (checkFile(filepath)) {
logger.info("[vite-zig] file is changed: " + filepath);
changed = true;
}
})
.on("unlink", (filepath) => {
if (checkFile(filepath)) {
logger.info("[vite-zig] file is removed: " + filepath);
changed = true;
}
})
.on("error", (error) => {
logger.error("[vite-zig] error: " + error);
});
},
};
};