-
Notifications
You must be signed in to change notification settings - Fork 170
/
build.ts
42 lines (40 loc) · 1.36 KB
/
build.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
import { base64, brotli, ensureDir, Sha1 } from '../deps.ts'
if (import.meta.main) {
const p = Deno.run({
cmd: ['wasm-pack', 'build', '--target', 'web'],
stdout: 'inherit',
stderr: 'inherit'
})
await p.status()
p.close()
const wasmData = await Deno.readFile('./pkg/aleph_compiler_bg.wasm')
const wpjsContent = await Deno.readTextFile('./pkg/aleph_compiler.js')
const data = brotli.compress(wasmData)
const data64 = base64.encode(data)
const hash = (new Sha1).update(data).hex()
await ensureDir('./dist')
await Deno.writeTextFile(
'./dist/wasm.js',
[
`import { base64, brotli } from "../../deps.ts";`,
`const dataRaw = "${data64}";`,
`export default () => brotli.decompress(base64.decode(dataRaw))`
].join('\n')
)
await Deno.writeTextFile(
'./dist/wasm-checksum.js',
`export const checksum = ${JSON.stringify(hash)}`
)
await Deno.writeTextFile(
'./dist/wasm-pack.js',
`import log from "../../shared/log.ts";` + wpjsContent.replace('console.error(getStringFromWasm0(arg0, arg1));', `
const msg = getStringFromWasm0(arg0, arg1);
if (msg.includes("DiagnosticBuffer")) {
const diagnostic = msg.split('DiagnosticBuffer(["')[1].split('"])')[0]
log.error("swc:", diagnostic)
} else {
log.error(msg)
}
` )
)
}