-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.js
104 lines (93 loc) · 2.79 KB
/
build.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
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
#!/usr/bin/env node
const esbuild = require("esbuild")
const fs = require("fs")
const childProcess = require("child_process")
let watch = false
let fast = false
const args = process.argv.slice(2)
if (args[0] == "--watch" || args[0] == "-watch" || args[0] == "-w") {
args.shift()
watch = true
}
if (args[0] == "--fast" || args[0] == "-fast" || args[0] == "-f") {
args.shift()
fast = true
}
if (args.length) {
console.log("Usage: ./build.js [--watch]")
process.exit(1)
}
function runTSC(args) {
return new Promise((resolve, reject) => {
let invoked = false
if (watch) args.push("--watch", "--preserveWatchOutput")
console.log("run tsc " + args.join(" "))
let tscPath = "node_modules/typescript/lib/tsc.js"
if (!fs.existsSync(tscPath)) tscPath = "../" + tscPath
const process = childProcess.fork(tscPath, args)
process.on("error", err => {
if (invoked) return
invoked = true
reject(err)
})
process.on("exit", code => {
if (invoked) return
invoked = true
if (code == 0) resolve()
else reject(new Error("exit " + code))
})
// in watch mode "go in background"
if (watch)
setTimeout(() => {
if (invoked) return
invoked = true
resolve()
}, 500)
})
}
const files = {
"dist/jacdac.cjs": "src/jacdac.ts",
"dist/jacdac.js": "src/jacdac.ts",
"dist/jacdac.mjs": "src/jacdac.ts",
"dist/p5.jacdac.js": "src/p5/p5.jacdac.ts",
"dist/jacdac-embed.js": "src/embed/jacdac-embed.ts",
"dist/jacdac-worker.js": "src/worker/jacdac-worker.ts",
"dist/jacdac-tstester.mjs": "src/tstester/jacdac-tstester.ts",
}
function check(pr) {
pr.then(
() => {},
err => {
console.error("Error: " + err.message)
process.exit(1)
}
)
}
async function main() {
try {
for (const outfile of Object.keys(files)) {
const src = files[outfile]
const cjs = outfile.endsWith(".cjs")
const mjs = outfile.endsWith(".mjs")
await esbuild.build({
entryPoints: [src],
bundle: true,
sourcemap: true,
outfile,
logLevel: "warning",
external: ["net", "webusb", "crypto", "fs"],
platform: cjs ? "node" : "browser",
target: "es2020",
format: mjs ? "esm" : cjs ? "cjs" : "iife",
globalName: "jacdac",
watch,
})
}
console.log("bundle done")
if (!fast) {
await runTSC(["-b", "."])
await runTSC(["-b", "src/worker"])
}
} catch {}
}
main()