Skip to content

Commit 2e0c0fa

Browse files
committed
migrate build.sh to build.mts
1 parent 99d10a6 commit 2e0c0fa

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

package-lock.json

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"sideEffects": false,
2626
"scripts": {
2727
"build": "npm publish --dry-run",
28-
"prepare": "npm run clean && ./wasm/build.sh && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js dist.esm/*.d.ts dist.esm/*/*.d.ts && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js dist.cjs/*.d.ts dist.cjs/*/*.d.ts",
28+
"build:wasm": "node wasm/build.mts",
29+
"prepare": "npm run clean && webpack --bail && tsgo --build tsconfig.dist.cjs.json tsconfig.dist.esm.json && tsimp tools/fix-ext.mts --mjs dist.esm/*.js dist.esm/*/*.js dist.esm/*.d.ts dist.esm/*/*.d.ts && tsimp tools/fix-ext.mts --cjs dist.cjs/*.js dist.cjs/*/*.js dist.cjs/*.d.ts dist.cjs/*/*.d.ts",
2930
"prepublishOnly": "npm run test:dist",
3031
"clean": "rimraf build dist dist.*",
3132
"test": "mocha 'test/**/*.test.ts'",
@@ -74,9 +75,10 @@
7475
"@types/node": "latest",
7576
"@typescript-eslint/eslint-plugin": "latest",
7677
"@typescript-eslint/parser": "latest",
77-
"@typescript/native-preview": "^7.0.0-dev.20251225.1",
78+
"@typescript/native-preview": "latest",
7879
"assert": "latest",
7980
"benchmark": "latest",
81+
"binaryen": "latest",
8082
"buffer": "latest",
8183
"core-js": "latest",
8284
"eslint": "latest",

src/utils/utf8-wasm-binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Auto-generated by wasm/build.sh - DO NOT EDIT MANUALLY
1+
// Auto-generated by wasm/build.mts - DO NOT EDIT MANUALLY
22
// Source: wasm/utf8.wat
33

44
export const wasmBinary = `

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
/* Basic Options */
44
"target": "es2020", /* the baseline */
5-
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
5+
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
66
"lib": [
77
"esnext",
88
"dom"

wasm/build.mts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Build script for UTF-8 wasm module
2+
// Invoked by `npm run build:wasm`
3+
4+
/* eslint-disable no-console */
5+
6+
import fs from "node:fs";
7+
import path from "node:path";
8+
import { fileURLToPath } from "node:url";
9+
import binaryen from "binaryen";
10+
import binaryenMetadata from "binaryen/package.json" with { type: "json" };
11+
12+
13+
const dirname = path.dirname(fileURLToPath(import.meta.url));
14+
15+
const watPath = path.join(dirname, "utf8.wat");
16+
const wasmPath = path.join(dirname, "utf8.wasm");
17+
const tsOutputPath = path.join(dirname, "..", "src", "utils", "utf8-wasm-binary.ts");
18+
19+
console.log(`Compiling utf8.wat -> utf8.wasm with Binaryen v${binaryenMetadata.version}...`);
20+
21+
// Read WAT source
22+
const watSource = fs.readFileSync(watPath, "utf-8");
23+
24+
// Parse WAT to module
25+
const mod = binaryen.parseText(watSource);
26+
27+
// Enable required features
28+
mod.setFeatures(binaryen.Features.ReferenceTypes | binaryen.Features.GC | binaryen.Features.Strings);
29+
30+
// Optimize (equivalent to wasm-opt -O4)
31+
mod.optimize();
32+
33+
// Emit binary
34+
const wasmBinary = mod.emitBinary();
35+
36+
// Write wasm file
37+
fs.writeFileSync(wasmPath, wasmBinary);
38+
39+
console.log("Generating base64 TypeScript module...");
40+
41+
// Convert to base64 with line breaks (like base64 -b 78)
42+
const base64 = Buffer.from(wasmBinary).toString("base64");
43+
const base64WithLineBreaks = base64.match(/.{1,78}/g)?.join("\n") ?? base64;
44+
45+
// Generate TypeScript file
46+
const tsContent = `// Auto-generated by wasm/build.mts - DO NOT EDIT MANUALLY
47+
// Source: wasm/utf8.wat
48+
49+
export const wasmBinary = \`
50+
${base64WithLineBreaks}
51+
\`;
52+
`;
53+
54+
fs.writeFileSync(tsOutputPath, tsContent);
55+
56+
// Clean up
57+
mod.dispose();
58+
59+
console.log("Done! Generated:");
60+
console.log(` - wasm/utf8.wasm (${wasmBinary.length} bytes)`);
61+
console.log(` - src/utils/utf8-wasm-binary.ts (${Buffer.byteLength(tsContent)} bytes)`);

wasm/build.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)