Skip to content

Commit 1a40541

Browse files
committed
docs: migrating guide
1 parent bceeb54 commit 1a40541

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

docs/src/content/docs/_sidebar.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@
4545
}
4646
}
4747
]
48-
},
48+
}
49+
]
50+
},
51+
{
52+
"label": "Guides",
53+
"collapsed": false,
54+
"items": [
4955
{
5056
"label": "Bindings Structure",
5157
"link": "/structure"
58+
},
59+
{
60+
"label": "Migrating",
61+
"link": "/migrating"
5262
}
5363
]
5464
},

docs/src/content/docs/migrating.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
title: Migrating
3+
prev: false
4+
next: false
5+
---
6+
7+
This guide explains how to migrate your bindings generation from the previous tools like [dfx](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/) or [didc](https://internetcomputer.org/docs/building-apps/interact-with-canisters/candid/candid-tools#didc) to `@icp-sdk/bindgen`.
8+
9+
## From dfx (Rust canister)
10+
11+
### Before
12+
13+
If you were using `dfx` to generate bindings, your configuration was likely to be like this:
14+
15+
```json title="dfx.json"
16+
{
17+
"canisters": {
18+
"hello-world-backend": {
19+
"candid": "src/hello-world-backend/hello-world-backend.did",
20+
"package": "hello-world-backend",
21+
"type": "rust"
22+
},
23+
...
24+
}
25+
}
26+
```
27+
28+
And you were generating bindings with the following command:
29+
30+
```bash
31+
dfx generate
32+
```
33+
34+
This was typically writing the bindings' files in the `src/declarations/hello-world-backend` directory, unless you configured the `canisters.hello-world-backend.declarations` field.
35+
36+
### After
37+
38+
Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following command (typically in a script in the `package.json` file of the frontend):
39+
40+
```bash
41+
icp-bindgen --did-file ./src/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend
42+
```
43+
44+
> In case you configured the `canisters.hello-world-backend.declarations` field, you can pass the `canisters.hello-world-backend.declarations.output` value to the `--out-dir` flag. Note that in `@icp-sdk/bindgen`, there's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts).
45+
46+
This will generate the bindings in the `src/hello-world-frontend/backend` directory. See the [Bindings Structure](./structure) page for more information on the generated files.
47+
48+
> The same can be applied for a canister configured as `type: custom` in the `dfx.json` file.
49+
50+
## From dfx (Motoko canister)
51+
52+
### Before
53+
54+
If you were using `dfx` to generate bindings, your configuration was likely to be like this:
55+
56+
```json title="dfx.json"
57+
{
58+
"canisters": {
59+
"hello-world-backend": {
60+
"main": "src/hello-world-backend/main.mo",
61+
"type": "motoko"
62+
},
63+
...
64+
}
65+
}
66+
```
67+
68+
And you were generating bindings with the following command:
69+
70+
```bash
71+
dfx generate
72+
```
73+
74+
This was typically writing the bindings' files in the `src/declarations/hello-world-backend` directory, unless you configured the `canisters.hello-world-backend.declarations` field.
75+
76+
### After
77+
78+
In Motoko, the Candid declaration file (`.did`) is generated by `dfx` at build time. It's typically written in the `.dfx/<network-name>/canisters/hello-world-backend/hello-world-backend.did` file.
79+
80+
Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following commands (typically in a script in the `package.json` file of the frontend):
81+
82+
```bash
83+
dfx build --check hello-world-backend # can be skipped if you already built the canister
84+
icp-bindgen --did-file ./.dfx/local/canisters/hello-world-backend/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend
85+
```
86+
87+
> In case you configured the `canisters.hello-world-backend.declarations` field, you can pass the `canisters.hello-world-backend.declarations.output` value to the `--out-dir` flag. Note that in `@icp-sdk/bindgen`, there's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts).
88+
89+
This will generate the bindings in the `src/hello-world-frontend/backend` directory. See the [Bindings Structure](./structure) page for more information on the generated files.
90+
91+
---
92+
93+
For a more sophisticated approach, you can configure the `canisters.hello-world-backend.declarations` field to write the `.did` file to a directory of your choice and then use the `--did-file` flag to point to it. For example:
94+
95+
```json title="dfx.json"
96+
{
97+
"canisters": {
98+
"hello-world-backend": {
99+
"main": "src/hello-world-backend/main.mo",
100+
"type": "motoko",
101+
"declarations": {
102+
"bindings": ["did"],
103+
"output": "candid"
104+
}
105+
},
106+
...
107+
}
108+
}
109+
```
110+
111+
And then you can run the following commands:
112+
113+
```bash
114+
dfx generate --check hello-world-backend
115+
icp-bindgen --did-file ./candid/hello-world-backend.did --out-dir ./src/hello-world-frontend/backend
116+
```
117+
118+
## From didc
119+
120+
### Before
121+
122+
If you were using `didc` to generate bindings, you were likely running the following commands:
123+
124+
```bash
125+
didc bind --target js ./service.did > ./service.did.js
126+
didc bind --target ts ./service.did > ./service.did.d.ts
127+
```
128+
129+
### After
130+
131+
Now, you can migrate to `@icp-sdk/bindgen` in [CLI mode](./cli) by running the following command:
132+
133+
```bash
134+
icp-bindgen --did-file ./service.did --out-dir ./service
135+
```
136+
137+
This will generate the bindings in the `service` directory. See the [Bindings Structure](./structure) page for more information on the generated files.
138+
139+
> Note: There's no way to avoid generating the [TypeScript declarations](./structure#declarationsservice-namediddts).

docs/src/content/docs/structure.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ If provided, the `actorOptions` will be passed to the [`Actor.createActor`](http
398398

399399
This folder contains the actual Candid JS bindings. It generates the same bindings that the [`dfx generate`](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/dfx-generate) command was generating.
400400

401+
See the [Migrating](./migrating) page for more information on how to migrate from `dfx generate`.
402+
401403
### `declarations/<service-name>.did.d.ts`
402404

403405
This file is used in TypeScript projects to type the Candid JS bindings generated in [`declarations/<service-name>.did.js`](#declarationsservice-namedidjs).

0 commit comments

Comments
 (0)