-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwasm-builder.js
63 lines (51 loc) · 1.49 KB
/
wasm-builder.js
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
const { execSync } = require("child_process");
const fs = require("fs");
const Bundler = require("parcel-bundler");
const Path = require("path");
const chokidar = require("chokidar");
const dotenv = require("dotenv");
const app = require("express")();
const compression = require("compression");
dotenv.config();
const entryFiles = Path.join(__dirname, "index.html");
const buildType = process.env.NODE_ENV;
const options = {
outDir: "./dist",
outFile: "index.html",
publicUrl: "/",
watch: true,
minify: buildType === "production",
};
const bundler = new Bundler(entryFiles, options);
chokidar
.watch(["./crate/src", "./crate/Cargo.toml"])
.on("change", async (event, path) => {
console.log(
`there are new changes in '${path}'. Start to rebuild rustwasm sources`
);
bundler.bundle();
bundler.hmr.broadcast({
type: "reload",
});
});
bundler.on("buildStart", () => {
let prevtBuildFile = Path.join(__dirname, "./wasm_pack_cmd");
if (process.platform === "win32") {
prevtBuildFile = Path.join(__dirname, "./wasm_pack_cmd.bat");
}
console.log(`running: ${prevtBuildFile}`);
execSync(`${prevtBuildFile} ${buildType === "production" ? "" : "--dev"}`, {
stdio: "inherit",
});
});
if (buildType === "production") {
app.use(compression());
}
app.use(bundler.middleware());
app.listen(process.env.SERVER_ADDRESS || 1234, () => {
console.log(
`Yew app start in address: ${
process.env.SERVER_ADDRESS || "http://localhost:1234"
}`
);
});