roc/examples/typescript-interop/node
2023-03-19 09:48:31 -04:00
..
platform Drop unused import 2023-03-19 09:26:45 -04:00
addon.d.ts Make some files POSIX compliant (end in newline) 2023-03-17 21:55:21 -04:00
binding.gyp Make some files POSIX compliant (end in newline) 2023-03-17 21:55:21 -04:00
demo.c Set refcount in init_rocbytes 2023-03-19 09:48:31 -04:00
hello.ts Make some files POSIX compliant (end in newline) 2023-03-17 21:55:21 -04:00
main.roc Call Roc from Node 2023-03-16 10:29:01 -04:00
package-lock.json Split out node and wasm in typescript-interop 2023-03-16 08:51:06 -04:00
package.json Split out node and wasm in typescript-interop 2023-03-16 08:51:06 -04:00
README.md Fix TS interop readme typos 2023-03-17 21:26:08 -04:00

TypeScript Interop

This is an example of calling Roc code from TypeScript on Node.js.

Installation

You'll need to have a C compiler installed, but most operating systems will have one already. (e.g. macOS has clang installed by default, Linux usually has gcc by default, etc.) All of these commands should be run from the same directory as this README file.

First, run this to install Node dependencies and generate the Makefile that will be used by future commands. (You should only need to run this once.)

npm install
npx node-gyp configure

Building the Roc library

First, cd into this directory and run this in your terminal:

roc build --lib

This compiles your Roc code into a shared library in the current directory. The library's filename will be libhello plus an OS-specific extension (e.g. libhello.dylib on macOS).

Next, run this to rebuild the C sources.

npx node-gyp build

Finally, run this to copy the generated TypeScript type definitions into the build directory:

cp addon.d.ts build/Release/

You can verify that TypeScript sees the correct types with:

npx tsc hello.ts

Try it out!

Now that everything is built, you should be able to run the example with:

npx ts-node hello.ts

To rebuild after changing either the demo.c file or any .roc files, run:

roc build --lib && npx node-gyp build

About this example

This was created by following the NodeJS addons tutorial and switching from C++ to C, then creating the addon.d.ts file to add types to the generated native Node module.