This repository was archived by the owner on Jun 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.js
More file actions
98 lines (88 loc) · 2.42 KB
/
Copy pathbuild.js
File metadata and controls
98 lines (88 loc) · 2.42 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
#!/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|--fast]")
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
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 = {
"built/ml4f.js": "src/ml4f.ts",
"built/ml4f.cjs": "src/ml4f.ts",
"built/ml4f.mjs": "src/ml4f.ts",
"built/pxtml4f.js": "pxt/extension.ts",
"built/pxtml4f.cjs": "pxt/extension.ts",
"built/cli.cjs": "cli/src/cli.ts",
}
async function main() {
try {
for (const outfile of Object.keys(files)) {
const src = files[outfile]
const basename = outfile.replace(/.*\//, "").replace(/\..*/, "")
const cjs = outfile.endsWith(".cjs")
const mjs = outfile.endsWith(".mjs")
const iife = !cjs && !mjs
await esbuild.build({
entryPoints: [src],
bundle: true,
sourcemap: true,
outfile,
logLevel: "warning",
external: iife ? [] : ["@tensorflow/tfjs", "commander"],
platform: cjs ? "node" : "browser",
target: "es2019",
format: mjs ? "esm" : cjs ? "cjs" : "iife",
globalName: iife ? basename : undefined,
watch
})
}
console.log("bundle done")
if (!fast)
await runTSC(["-b", ".", "pxt", "cli"])
} catch (e) {
console.error(e)
}
}
main()