Skip to content

Commit

Permalink
Use esbuild-plugin-wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Dec 7, 2024
1 parent 79d8f69 commit 4614a1c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions telegram-bot/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mv temp ../wasm/pkg-nodejs/package.json

npm install
npm exec tsc
npm exec -- esbuild --bundle index.ts --outdir=dist --platform=node --format=esm --loader:.wasm=binary
node --experimental-strip-types esbuild.ts

rm -f lambda_package.zip

zip -j lambda_package.zip package.json dist/index.js
zip -j -r lambda_package.zip package.json dist/
29 changes: 29 additions & 0 deletions telegram-bot/esbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { context } from "esbuild";
import { wasmLoader } from 'esbuild-plugin-wasm';

const watch = process.argv.includes('--watch');

async function main() {
const ctx = await context({
entryPoints: ['index.ts'],
bundle: true,
outdir: 'dist',
platform: 'node',
format: 'esm',
plugins: [wasmLoader()],
});

if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
await ctx.dispose();
}
}

try {
await main();
} catch (e) {
console.error(e);
process.exit(1);
};
5 changes: 5 additions & 0 deletions telegram-bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as fend from 'fend-wasm-nodejs';

const two = fend.evaluateFendWithTimeout('1+1', 500);
if (two !== '20') {
throw new Error(`could not execute webassembly: got '${two}' instead of '2'`);
}

const TELEGRAM_BOT_API_TOKEN = process.env.TELEGRAM_BOT_API_TOKEN;

/*
Expand Down
15 changes: 15 additions & 0 deletions telegram-bot/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions telegram-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@types/node": "^22.10.1",
"esbuild": "^0.24.0",
"esbuild-plugin-wasm": "^1.1.0",
"typescript": "^5.7.2"
}
}

0 comments on commit 4614a1c

Please sign in to comment.