-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.js
71 lines (59 loc) · 2.15 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
#!/usr/bin/env node
if (process.env.NODE) {
process.env.EM_NODE_JS = process.env.NODE
}
const fs = require('fs')
const path = require('path')
const { spawn } = require('@tybys/cgen/bin/util/spawn.js')
const ts = require('@tybys/tsgo/lib/ts.js')
const { bundler } = require('@tybys/tsgo/lib/util.js')
const { readConfigNoCache } = require('@tybys/tsgo/lib/config.js')
function p (...args) {
if (!args.length) return __dirname
return path.isAbsolute(args[0]) ? path.join(...args) : path.join(__dirname, ...args)
}
function e (cmd) {
return process.platform === 'win32' ? `${cmd}.cmd` : cmd
}
function mkdir (dir) {
return fs.mkdirSync(dir, { recursive: true })
}
function cp (s, t) {
mkdir(path.dirname(t))
fs.copyFileSync(s, t)
}
async function invokeTSGO (command, config) {
const r = await require(`@tybys/tsgo/command/${command}.js`).run(config)
if (r !== 0) {
throw new Error(`TSGO command failed: ${command}`)
}
}
async function main () {
const tsgoConfig = readConfigNoCache(p('./tsgo.config.js'))
await invokeTSGO('lint', tsgoConfig)
await spawn(e('npx'), ['cgen', 'rebuild', '-e'], path.join(__dirname, 'wasm'))
const wasmoutdir = p('./wasm/.cgenbuild/Release')
fs.copyFileSync(p(wasmoutdir, 'wz.js'), p('./src/util/wz.js'))
mkdir(p('./lib/cjs-modern/util'))
mkdir(p('./lib/esm-modern/util'))
mkdir(p('./lib/esm/util'))
cp(p(wasmoutdir, 'wz.wasm'), p('./lib/cjs-modern/util/wz.wasm'))
cp(p(wasmoutdir, 'wz.wasm'), p('./lib/esm-modern/util/wz.wasm'))
ts.compile(p('tsconfig.json'))
ts.compile(p('tsconfig.modern.json'))
const asm = p('./src/util/wz.js')
cp(p(wasmoutdir, 'wzasm.js'), asm)
fs.writeFileSync(asm, fs.readFileSync(asm, 'utf8').replace('wzasm.js.mem', 'wz.js.mem').replace('wzasm.wasm', 'wz.wasm'), 'utf8')
ts.compile(p('tsconfig.esm.json'))
if (fs.existsSync(p(wasmoutdir, 'wzasm.js.mem'))) {
cp(p(wasmoutdir, 'wzasm.js.mem'), p('./lib/esm/util/wz.js.mem'))
}
await bundler.webpack(tsgoConfig)
await bundler.webpack(readConfigNoCache(p('./tsgo.es5.config.js')))
await invokeTSGO('dts', tsgoConfig)
await invokeTSGO('doc', tsgoConfig)
}
main().catch(err => {
console.error(err)
process.exit(1)
})