Skip to content

Commit 283230a

Browse files
authored
chore(docs): patch web app tutorial in 1.0.0-beta3 versioned docs (#8158)
1 parent c69acd7 commit 283230a

1 file changed

Lines changed: 29 additions & 59 deletions

File tree

docs/versioned_docs/version-v1.0.0-beta.3/tutorials/noirjs_app.md

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@ You can find the complete app code for this guide [here](https://github.com/noir
1414

1515
## Dependencies
1616

17-
Before we start, we want to make sure we have Node installed. For convenience (and speed), we can just install [Bun](https://bun.sh) as our package manager, and Node will work out-of-the-box:
17+
Before we start, we want to make sure we have Node installed. If you don't have it already you can install it [here](https://nodejs.org/en/download), we recommend using [Yarn](https://yarnpkg.com/getting-started/install) as our package manager for this tutorial.
1818

19-
```bash
20-
curl -fsSL https://bun.sh/install | bash
21-
```
19+
We'll also need version 1.0.0-beta.2 nargo installed, see the Noir [installation guide](../getting_started/noir_installation.md) for details.
2220

2321
Let's go barebones. Doing the bare minimum is not only simple, but also allows you to easily adapt it to almost any frontend framework.
2422

2523
Barebones means we can immediately start with the dependencies even on an empty folder 😈:
2624

2725
```bash
28-
bun i @noir-lang/noir_wasm@1.0.0-beta.2 @noir-lang/noir_js@1.0.0-beta.2 @aztec/bb.js@0.72.1
26+
yarn add @noir-lang/noir_js@1.0.0-beta.2 @aztec/bb.js@0.72.1
2927
```
3028

3129
Wait, what are these dependencies?
3230

33-
- `noir_wasm` is the `wasm` version of the Noir compiler. Although most developers prefer to use `nargo` for compiling, there's nothing wrong with `noir_wasm`. We like `noir_wasm`.
3431
- `noir_js` is the main Noir package. It will execute our program, and generate the witness that will be sent to the backend.
3532
- `bb.js` is the Typescript interface for Aztec's Barretenberg proving backend. It also uses the `wasm` version in order to run on the browser.
3633

@@ -77,6 +74,20 @@ This is all that we need to get started with Noir.
7774

7875
![my heart is ready for you, noir.js](@site/static/img/memes/titanic.jpeg)
7976

77+
## Compile compile compile
78+
79+
Finally we're up for something cool. But before we can execute a Noir program, we need to compile it into ACIR: an abstract representation.
80+
81+
This can be done by cd-ing into our circuit directory and running the `nargo compile` command.
82+
83+
```bash
84+
cd circuits
85+
86+
nargo compile
87+
```
88+
89+
This will write the compiled circuit into the `target` directory, which we'll then load into our JS later on.
90+
8091
## Setting up our app
8192

8293
Remember when apps only had one `html` and one `js` file? Well, that's enough for Noir webapps. Let's create them:
@@ -158,72 +169,25 @@ At this point in the tutorial, your folder structure should look like this:
158169

159170
:::
160171

161-
## Compile compile compile
162-
163-
Finally we're up for something cool. But before we can execute a Noir program, we need to compile it into ACIR: an abstract representation. Here's where `noir_wasm` comes in.
164-
165-
`noir_wasm` expects a filesystem so it can resolve dependencies. While we could use the `public` folder, let's just import those using the nice `?url` syntax provided by vite. At the top of the file:
166-
167-
```js
168-
import { compile, createFileManager } from "@noir-lang/noir_wasm"
169-
170-
import main from "./circuit/src/main.nr?url";
171-
import nargoToml from "./circuit/Nargo.toml?url";
172-
```
173-
174-
Compiling on the browser is common enough that `createFileManager` already gives us a nice in-memory filesystem we can use. So all we need to compile is fetching these files, writing them to our filesystem, and compile. Add this function:
175-
176-
```js
177-
export async function getCircuit() {
178-
const fm = createFileManager("/");
179-
const { body } = await fetch(main);
180-
const { body: nargoTomlBody } = await fetch(nargoToml);
181-
182-
fm.writeFile("./src/main.nr", body);
183-
fm.writeFile("./Nargo.toml", nargoTomlBody);
184-
return await compile(fm);
185-
}
186-
```
187-
188-
:::tip
189-
190-
As you can imagine, with `node` it's all conveniently easier since you get native access to `fs`...
191-
192-
:::
193-
194172
## Some more JS
195173

196174
We're starting with the good stuff now. We want to execute our circuit to get the witness, and then feed that witness to Barretenberg. Luckily, both packages are quite easy to work with. Let's import them at the top of the file:
197175

198176
```js
199177
import { UltraHonkBackend } from '@aztec/bb.js';
200178
import { Noir } from '@noir-lang/noir_js';
179+
import circuit from "./circuit/target/circuit.json";
201180
```
202181

203182
And instantiate them inside our try-catch block:
204183

205184
```ts
206185
// try {
207-
const { program } = await getCircuit();
208-
const noir = new Noir(program);
209-
const backend = new UltraHonkBackend(program.bytecode);
186+
const noir = new Noir(circuit);
187+
const backend = new UltraHonkBackend(circuit.bytecode);
210188
// }
211189
```
212190

213-
:::warning
214-
215-
WASMs are not always easy to work with. In our case, `vite` likes serving them with the wrong MIME type. There are different fixes but we found the easiest one is just YOLO instantiating the WASMs manually. Paste this at the top of the file, just below the other imports, and it will work just fine:
216-
217-
```js
218-
import initNoirC from "@noir-lang/noirc_abi";
219-
import initACVM from "@noir-lang/acvm_js";
220-
import acvm from "@noir-lang/acvm_js/web/acvm_js_bg.wasm?url";
221-
import noirc from "@noir-lang/noirc_abi/web/noirc_abi_wasm_bg.wasm?url";
222-
await Promise.all([initACVM(fetch(acvm)), initNoirC(fetch(noirc))]);
223-
```
224-
225-
:::
226-
227191
## Executing and proving
228192

229193
Now for the app itself. We're capturing whatever is in the input when people press the submit button. Inside our `try` block, let's just grab that input and get its value. Noir will gladly execute it, and give us a witness:
@@ -257,16 +221,22 @@ Our program is technically **done** . You're probably eager to see stuff happeni
257221
touch vite.config.js
258222
```
259223

260-
`vite` helps us with a little catch: `bb.js` in particular uses top-level awaits which aren't supported everywhere. So we can add this to the `vite.config.js` to make the bundler optimize them:
224+
Noir needs to load two WASM modules, but Vite doesn't include them by default in the bundle. We need to add the configuration below to `vite.config.js` to make it work.
225+
We also need to target ESNext since `bb.js` uses top-level await, which isn't supported in some browsers.
261226

262227
```js
263-
export default { optimizeDeps: { esbuildOptions: { target: "esnext" } } };
228+
export default {
229+
optimizeDeps: {
230+
esbuildOptions: { target: "esnext" },
231+
exclude: ['@noir-lang/noirc_abi', '@noir-lang/acvm_js']
232+
}
233+
};
264234
```
265235

266236
This should be enough for vite. We don't even need to install it, just run:
267237

268238
```bash
269-
bunx vite
239+
yarn dlx vite
270240
```
271241

272242
If it doesn't open a browser for you, just visit `localhost:5173`. You should now see the worst UI ever, with an ugly input.

0 commit comments

Comments
 (0)