-
Notifications
You must be signed in to change notification settings - Fork 5
/
esbuild.js
76 lines (70 loc) · 1.93 KB
/
esbuild.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
import { copyFileSync, unlinkSync, readFileSync, writeFileSync } from 'fs'
import esbuild from 'esbuild'
import watPlugin from 'esbuild-plugin-wat'
import path from 'path'
import { fileURLToPath } from 'url'
import { minify } from 'uglify-js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// BUNDLERS SUCK.
// no matter how convoluted, this works...
// step 1: migrate to dist folder
esbuild
.buildSync({
entryPoints: [
'./test/index.ts'
],
format: 'esm',
sourcemap: false,
treeShaking: true,
bundle: true,
minify: false,
outfile: './dist/tmp_worker.mjs',
external: ['*.wasm'],
})
// step 2: copy zigWASM and zigWASI to dist
copyFileSync('./zig-out/lib/zigWASM.wasm', './dist/zigWASM.wasm')
copyFileSync('./zig-out/lib/zigWASI.wasm', './dist/zigWASI.wasm')
copyFileSync('./memfs.wasm', './dist/memfs.wasm')
// step 3: final build
esbuild
.build({
entryPoints: [
'./dist/tmp_worker.mjs'
],
format: 'esm',
sourcemap: true,
treeShaking: true,
bundle: true,
minify: false,
outfile: './dist/worker.mjs',
plugins: [
watPlugin({
loader: 'file'
})
]
})
.then(() => {
// cleanup tmp files
unlinkSync('./dist/tmp_worker.mjs')
unlinkSync('./dist/zigWASM.wasm')
unlinkSync('./dist/zigWASI.wasm')
unlinkSync('./dist/memfs.wasm')
})
.then(() => {
stepFour()
})
.catch((err) => { console.log(err); process.exit(1) })
// step 4: read line-by-line. If line includes a .wasm file, convert to import
function stepFour () {
const file = readFileSync('./dist/worker.mjs', 'utf8')
const lines = file.split('\n')
for (let i = 0, ll = lines.length; i < ll; i++) {
const line = lines[i]
if (line.includes('.wasm"')) {
lines[i] = line.replace('var', 'import').replace('=', 'from')
}
}
const { code } = minify(lines.join('\n'))
writeFileSync('./dist/worker.mjs', code)
}