forked from littledivy/deno_sass2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
90 lines (69 loc) · 2.12 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { encode } from "https://deno.land/std@0.61.0/encoding/base64.ts";
import Terser from "https://esm.sh/terser@4.8.0";
const name = "deno_sass2";
const encoder = new TextEncoder();
async function requires(...executables) {
const where = Deno.build.os === "windows" ? "where" : "which";
for (const executable of executables) {
const process = Deno.run({
cmd: [where, executable],
stderr: "null",
stdin: "null",
stdout: "null",
});
if (!(await process.status()).success) {
err(`Could not find required build tool ${executable}`);
}
}
}
async function run(msg, cmd) {
log(msg);
const process = Deno.run({ cmd });
if (!(await process.status()).success) {
err(`${msg} failed`);
}
}
function log(text) {
console.log(`[log] ${text}`);
}
function err(text) {
console.log(`[err] ${text}`);
return Deno.exit(1);
}
await requires("rustup", "rustc", "cargo", "wasm-pack");
if (!(await Deno.stat("Cargo.toml")).isFile) {
err(`the build script should be executed in the "${name}" root`);
}
await run(
"building using wasm-pack",
["wasm-pack", "build", "--target", "web", "--release"],
);
const wasm = await Deno.readFile(`pkg/${name}_bg.wasm`);
const encoded = encode(wasm);
log(
`encoded wasm using base64, size increase: ${encoded.length -
wasm.length} bytes`,
);
log("inlining wasm in js");
const source =
`export const source = Uint8Array.from(atob("${encoded}"), c => c.charCodeAt(0));`;
const init = await Deno.readTextFile(`pkg/${name}.js`);
log("minifying js");
const output = Terser.minify(`${source}\n${init}`, {
mangle: { module: true },
output: {
preamble: "//deno-fmt-ignore-file",
},
});
if (output.error) {
err(`encountered error when minifying: ${output.error}`);
}
const reduction = new Blob([(`${source}\n${init}`)]).size -
new Blob([output.code]).size;
log(`minified js, size reduction: ${reduction} bytes`);
log(`writing output to file ("wasm.js")`);
await Deno.writeFile("wasm.js", encoder.encode(output.code));
const outputFile = await Deno.stat("wasm.js");
log(
`output file ("wasm.js"), final size is: ${outputFile.size} bytes`,
);