TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/metareflection/dafny-replay/llms.txt
Use this file to discover all available pages before exploring further.
dafny2js tool bridges the gap between verified Dafny code and idiomatic JavaScript. You point it at your .dfy file, tell it which module is your public AppCore, and it generates an app.js (or app.ts) file that wraps every function in the AppCore module with proper type conversions. The result is a module you can import directly into React components — no manual BigNumber coercion, no __default prefixes, no hand-written Dafny-to-JS glue code.
The compilation pipeline
Generating a JavaScript API from Dafny takes two distinct steps: Dafny’s own compiler, and thendafny2js.
Compile Dafny to JavaScript
Run the Dafny compiler to produce a CommonJS bundle that includes the Dafny runtime and your compiled modules:Copy the output to your app’s source tree:The
.cjs extension tells Vite to treat it as CommonJS (which the Dafny compiler emits).CLI flags
| Flag | Required | Description |
|---|---|---|
--file <path> | Yes | Path to the .dfy source file. Used for type analysis. |
--app-core <name> | Yes | Name of the module to wrap (e.g., AppCore). |
--cjs-name <name> | Yes | Filename of the compiled .cjs bundle (e.g., MyApp.cjs). |
--output <path> | Yes | Output path for the generated wrapper file. |
--client | No | Emit Vite-compatible client code (default). Uses ?raw import and new Function loader. |
--deno | No | Emit Deno-compatible server code. Uses readFileSync-style loading. |
--null-options | No | Emit TypeScript with T | null option types instead of strict non-null types. |
--client and --deno are mutually exclusive. The default behavior (--client) generates code that works in Vite via the ?raw import and a new Function evaluator. Use --deno for server-side edge functions.What app.js contains
The generatedapp.js has five sections.
1. BigNumber setup and module loading
?raw import tells Vite to load the file as a string. The new Function trick evaluates the CommonJS bundle in a scope where require('bignumber.js') resolves correctly. This is the only supported pattern for loading Dafny-compiled code in a Vite/ESM environment.
2. Type converters (toJson / fromJson)
For each datatype in your domain,dafny2js emits bidirectional converters between JavaScript values and Dafny runtime objects:
3. Action constructors
Each datatype constructor gets a zero-argument factory function (for constructors with no fields) or an argument-accepting wrapper that converts JS types to Dafny types before calling the underlying constructor.4. AppCore function wrappers
Every function in yourAppCore module gets a wrapper that accepts and returns JavaScript-friendly values:
Present converts the returned BigNumber to a plain JavaScript number automatically.
5. Converter exports and internal access
_internal object gives app-extras.js access to the raw Dafny modules when you need to call functions outside the AppCore surface or build custom converters.
Using app.js in a component
After running the pipeline, importApp from the generated file and call its functions directly:
TypeScript output
Pass--output app.ts to generate a TypeScript file instead. The generated TypeScript includes explicit type definitions for the JSON representation of each datatype:
toJson/fromJson, not the internal Dafny runtime objects. React components can import them for prop types and state annotations.
React integration
Load the generated app.js in Vite and wire it into React state.
app-extras.js pattern
Layer customizations on top of the generated API without modifying generated files.