summaryrefslogtreecommitdiff
path: root/std/hash/_wasm/build.ts
blob: b269372e44c64548500d6eedade591393de82d75 (plain)
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { encode as base64Encode } from "../../encoding/base64.ts";

// 1. build wasm
async function buildWasm(path: string): Promise<void> {
  const cmd = [
    "wasm-pack",
    "build",
    "--target",
    "web",
    "--release",
    "-d",
    path,
  ];
  const builder = Deno.run({ cmd });
  const status = await builder.status();

  if (!status.success) {
    console.error(`Failed to build wasm: ${status.code}`);
    Deno.exit(1);
  }
}

// 2. encode wasm
async function encodeWasm(wasmPath: string): Promise<string> {
  const wasm = await Deno.readFile(`${wasmPath}/deno_hash_bg.wasm`);
  return base64Encode(wasm);
}

// 3. generate script
async function generate(wasm: string, output: string): Promise<void> {
  const initScript = await Deno.readTextFile(`${output}/deno_hash.js`);
  const denoHashScript =
    "/* eslint-disable */\n" +
    "//deno-fmt-ignore-file\n" +
    "//deno-lint-ignore-file\n" +
    `import * as base64 from "../../encoding/base64.ts";` +
    `export const source = base64.decode("${wasm}");` +
    initScript;

  await Deno.writeFile("wasm.js", new TextEncoder().encode(denoHashScript));
}

const OUTPUT_DIR = "./out";

await buildWasm(OUTPUT_DIR);
const wasm = await encodeWasm(OUTPUT_DIR);
await generate(wasm, OUTPUT_DIR);