-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
51 lines (42 loc) · 1.55 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
43
44
45
46
47
48
49
50
51
import {dirname,fromFileUrl,resolve,join} from "https://deno.land/std/path/mod.ts";
const dir = resolve(dirname(fromFileUrl(import.meta.url)))
if (Array.from(Deno.readDirSync(dir)).some(v => v.name === 'dist' && v.isDirectory)) {
await Deno.remove(resolve(dir,'dist'),{recursive:true})
}
await Deno.mkdir(resolve(dir,'dist'))
const p = Deno.run({
cmd:['wasm-pack','build','--target','web',resolve(dir,'wasm-deno-maxminddb')]
})
await p.status()
class encDec {
encoder: TextEncoder;
decoder: TextDecoder;
constructor(){
this.encoder = new TextEncoder();
this.decoder = new TextDecoder();
}
enc(str: string) {return this.encoder.encode(str)}
dec(buf: ArrayBuffer) {return this.decoder.decode(buf)}
}
const enc = new encDec()
await Deno.copyFile(resolve(dir,'wasm-deno-maxminddb/pkg/wasm_deno_maxminddb.d.ts'), join(resolve('dist'), 'lib.d.ts'))
const wasm = JSON.stringify(Array.from(await Deno.readFile(resolve(dir,"wasm-deno-maxminddb/pkg/wasm_deno_maxminddb_bg.wasm"))));
const lib = enc.enc(enc.dec(await Deno.readFile(resolve(dir,"wasm-deno-maxminddb/pkg/wasm_deno_maxminddb.js"))).replace(`
const { instance, module } = await load(await input, imports);
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
wasm.__wbindgen_start();
return wasm;
}
export default init;
`,`
const { instance } = await load(input, imports);
wasm = instance.exports;
wasm.__wbindgen_start();
}
`).replace(`
async function init(input) {
`,`{
let input = new Uint8Array(${wasm});
`));
await Deno.writeFile(join(resolve('dist'),'lib.js'),lib)