Web Assembly
transliterate_wasm runs the 🦀 Rust implementation compiled to WebAssembly. It has the same API as transliterate and is about 2.5–3× faster. It is part of the lipilekhika package, no extra install needed.
import { transliterate_wasm } from 'lipilekhika';
await transliterate_wasm('na jAyatE mriyatE vA', 'Normal', 'Devanagari'); // न जायते म्रियते वाawait transliterate_wasm(['rAma', 'kRShNa'], 'Normal', 'Telugu'); // ['రామ', 'కృష్ణ']
// custom options, as with `transliterate`await transliterate_wasm('गङ्गा', 'Devanagari', 'Gujarati', { 'brahmic_to_brahmic:replace_pancham_varga_varna_with_anusvAra': true}); // ગંગાThe module is loaded on the first call. To have it ready before then, call preloadWasm() at startup:
import { preloadWasm } from 'lipilekhika';
await preloadWasm();Choosing an Entry Point
Section titled “Choosing an Entry Point”transliterate_wasm is available from three imports, which differ only in how the WASM module is loaded:
| Import | WASM module | Use for |
|---|---|---|
lipilekhika |
Embedded (base64) in a chunk loaded on first use | Recommended. Node.js, Bun, Deno, browsers, any bundler or CDN, no setup |
lipilekhika/wasm |
Loaded the best way for the runtime (see below) | Cloudflare Workers and Vercel Edge (required there), and the separate .wasm file in browsers |
lipilekhika/wasm/slim |
Nothing embedded, you pass the .wasm file to initWasm() |
A strict CSP, hosting the file yourself, or a setup the others do not cover |
Cloudflare Workers and Vercel Edge
Section titled “Cloudflare Workers and Vercel Edge”These runtimes do not allow compiling WebAssembly from bytes, so the embedded module of lipilekhika can not load there (it throws an error pointing to lipilekhika/wasm). Use lipilekhika/wasm:
import { transliterate_wasm } from 'lipilekhika/wasm';
export default { async fetch() { return new Response(await transliterate_wasm('namaskAra', 'Normal', 'Devanagari')); // नमस्कार }};lipilekhika/wasm picks its build through export conditions, with no configuration:
- Cloudflare Workers (
workerd) — imports the.wasmfile as a module. Works withwranglerand the Cloudflare Vite plugin (Astro, SvelteKit, React Router, Nuxt, …), in dev and deployed. - Vercel Edge / Next.js edge (
edge-light) — imports the.wasmfile with?module. - Browsers (
browser) — fetches the.wasmfile, see below. - Node.js, Bun, Deno — the embedded module, as
lipilekhika.
So in a full stack app (eg. Astro on Cloudflare) the same import works on the server and in the browser.
In the Browser
Section titled “In the Browser”lipilekhika/wasm fetches the .wasm file from new URL('../lipilekhika.wasm', import.meta.url): 80 KB gzipped instead of 114 KB of base64, cached on its own and compiled while it downloads. If the file can not be loaded, it falls back to the embedded module and logs a warning.
| Setup | Extra config |
|---|---|
| Vite 8 (Astro 7, SvelteKit, …), dev and build | None |
| Vite 7 and older, build | None |
| Vite 7 and older, dev | optimizeDeps: { exclude: ['lipilekhika'] } in vite.config, else the embedded module is used |
| esbuild | Copy node_modules/lipilekhika/dist/lipilekhika.wasm to the folder above your bundle, else the embedded module is used |
| CDN, no bundler | None |
Manual Loading (lipilekhika/wasm/slim)
Section titled “Manual Loading (lipilekhika/wasm/slim)”Call initWasm() once with the .wasm file, exported as lipilekhika/lipilekhika.wasm. It takes a WebAssembly.Module, the bytes, a Response, or a URL to fetch (or a promise of one).
import { initWasm, transliterate_wasm } from 'lipilekhika/wasm/slim';import wasmUrl from 'lipilekhika/lipilekhika.wasm?url';
await initWasm(wasmUrl);await transliterate_wasm('namaste', 'Normal', 'Devanagari');import { initWasm } from 'lipilekhika/wasm/slim';import wasmModule from 'lipilekhika/lipilekhika.wasm';
await initWasm(wasmModule);import { initWasm } from 'lipilekhika/wasm/slim';import wasmModule from 'lipilekhika/lipilekhika.wasm?module';
await initWasm(wasmModule);import { readFile } from 'node:fs/promises';import { createRequire } from 'node:module';import { initWasm } from 'lipilekhika/wasm/slim';
const require = createRequire(import.meta.url);await initWasm(await readFile(require.resolve('lipilekhika/lipilekhika.wasm')));import { initWasm } from 'lipilekhika/wasm/slim';
// the version must match the installed packageslim loads the same file as lipilekhika/wasm, so it is not smaller: use it to decide where the file comes from.
Other Environments
Section titled “Other Environments”<!-- Browser, from a CDN --><script type="module"> import { transliterate_wasm } from 'https://cdn.jsdelivr.net/npm/lipilekhika/dist/esm/index.mjs'; console.log(await transliterate_wasm('namaste', 'Normal', 'Devanagari')); // नमस्ते</script>// Denoimport { transliterate_wasm } from 'npm:lipilekhika';// Next.js edge route (app/api/transliterate/route.ts)import { transliterate_wasm } from 'lipilekhika/wasm';
export const runtime = 'edge';
export async function GET() { return new Response(await transliterate_wasm('namaste', 'Normal', 'Devanagari'));}Jest (jsdom): jsdom lacks TextEncoder / TextDecoder, which the WASM bindings use. Add them in a setup file (setupFiles):
const { TextEncoder, TextDecoder } = require('node:util');Object.assign(globalThis, { TextEncoder, TextDecoder });API Reference
Section titled “API Reference”transliterate_wasm(text, from, to, options?)
Section titled “transliterate_wasm(text, from, to, options?)”text: string | string[]— one string or many (same options for all)from,to: ScriptLangType— script/language namesoptions?: TransliterationOptions— custom transliteration options
Returns: Promise<string | string[]>, the same shape as text.
preloadWasm()
Section titled “preloadWasm()”Loads the WASM module ahead of the first call. Returns: Promise<void>.
initWasm(wasm) (lipilekhika/wasm/slim only)
Section titled “initWasm(wasm) (lipilekhika/wasm/slim only)”Loads the module from wasm: a WebAssembly.Module, bytes (BufferSource), a Response, a URL (string | URL), or a promise of one. Call it before transliterate_wasm; later calls return the same load. If it fails, it can be called again. Returns: Promise<void>.
WASM or JavaScript?
Section titled “WASM or JavaScript?”In the benchmarks WASM transliterates the test corpus in 65 ms on Node.js and 51 ms on Bun, against 198 ms and 130 ms for transliterate.
- WASM — large texts, many or repeated calls, switching between many scripts, servers.
- JavaScript (
transliterate) — a few small calls, where loading the module first is not worth it; environments without WebAssembly.
WebAssembly is supported by Chrome/Edge 57+, Firefox 52+, Safari 11+, Node.js, Deno and Bun. In very old browsers (IE11) use transliterate.