Remix sandbox repo for Rust compiled to WASM and to native N-API modules

Overview

Rust <-> Remix Sandbox

Now with both native Rust and WASM versions!

If you want to combine the Web Fundamentals & Modern UX of Remix together with the Reliability, Performance & Efficiency of Rust, you can use functions built with Rust on your server. Useful for intensive computations such as on the fly machine learning tasks, image processing, fibonacci etc.

One of the best ways to do that is to use Node's FFI interface through napi-rs, which generates compiled native node addons from Rust code. This has been shown to be faster than the compiling it to WASM and including it that way.

This repo contains changes to the Remix compiler to add support for native .node files, which get included in your server bundle. This could probably can't run on the client, so it's to be used in loaders/actions only.

I have this repo setup for both methods, whether you'd like to compile to WASM or use napi-rs to generate a local Node module, this repo should help

Example

If you don't have Rust installed on your computer the first thing to do is to get this set up

Installing Rust:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Installing napi-rs

Then we need to install napi-rs globally with

yarn global add @napi-rs/cli
# or
npm install -g @napi-rs/cli
# or
pnpm add -g @napi-rs/cli

Save the index.js file in the napi-rust-functions folder. If you need a different platform than the one included in the repo, you need to delete the napi-rust-functions target and create a new one. Give it a name of napi-rust-functions and select the target for your CPU. The rest of the options don't matter unless you want to publish your package on npm.

napi new

Build the JS wrapper and index.js file

cd napi-rust-functions
npm run build

You'll need to remove the platform detection code in index.js. The saved index.js file is a good example which includes only one prebuilt item, but you'll need to remove the platform detection code, and copy the filename for your platform's .node file.

If you make changes to the rust code, you will need to rerun the npm run build command and edit the index.js file. I suspect you won't need to edit the index.js file if you generate every platform's code, but I have not explored that yet.

Installing the WASM version:

The WASM version of the function lives in the rust-functions folder. It gets rebuilt everytime you run npm run dev via the predev command in package.json.

Here we use wasm-pack to comile our Rust code down to WASM

Installing wasm-pack:

cargo install wasm-pack

In this example, the Rust library is already built with the associated code. But if you wanted to set up your own library you could do so by running the following command:

cargo new --lib <library-name>

Then build the library using:

cd <library-name>
wasm-pack build --target nodejs

After succesfully bulding the library you can add this to your dependencies by running inside your remix project:

npm install ./<library-name>/pkg

This will add the dependency to your package.json file.

    ...
    "@remix-run/serve": "1.1.3",
    "<library-name>": "file:<library-name>/pkg"
    ...

Notes:

To prevent remix from including the rust-functions library in the client build we can re-export the functions using a .server.ts file, e.g. rust.server.ts

This project includes patches to @remix-run/dev to support the .node add on. These should be automatically applied during the npm install step, but may become unecessary in the future.

Related Links

Rust

Napi-rs

Wasm-pack

Tensorflow & WebAssembly

You might also like...

A library of icons rendered purely in CSS and compiled using SASS.

Welcome to PureIcons.css: This is a library of icons rendered purely in CSS and compiled using SASSs. It is currently at 79 icons. More will be added

Apr 27, 2021

Minimal template engine with compiled output for JavaScript.

@fnando/seagull Minimal template engine with compiled output for JavaScript. Installation This package is available as a NPM package. To install it, u

Mar 1, 2022

PDF.js compiled for node-less, serverless enviroments.

Pdf.mjs PDF.js custom-compiled for nodeless, serverless enviroments, like Deno Deploy and Cloudflare Workers. Rocking in under 700kb uncompressed. Usa

Apr 16, 2022

A compiled-away, type-safe, readable RegExp alternative

🦄 magic-regexp A compiled-away, type-safe, readable RegExp alternative ✨ Changelog 📖 Documentation ▶️ Online playground Features Runtime is zero-dep

Jan 8, 2023

Remix enables you to build fantastic user experiences for the web and feel happy with the code that got you there. In this workshop, we'll look at some more advanced use cases when building Remix applications.

💿 Advanced Remix Workshop Remix enables you to build fantastic user experiences for the web and feel happy with the code that got you there. In this

Dec 9, 2022

Remix enables you to build fantastic user experiences for the web and feel happy with the code that got you there. Get a jumpstart on Remix with this workshop.

💿 Remix Fundamentals Build Better websites with Remix Remix enables you to build fantastic user experiences for the web and feel happy with the code

Dec 25, 2022

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using Remix and TailwindCSS. Create your own blog in just a few minutes!

simple-remix-blog is a blog template built using remix.run and TailwindCSS. It supports markdown and MDX for the blog posts. You can clone it and star

Dec 8, 2022

The Remix version of the fakebooks app demonstrated on https://remix.run. Check out the CRA version: https://github.com/kentcdodds/fakebooks-cra

Remix Fakebooks App This is a (very) simple implementation of the fakebooks mock app demonstrated on remix.run. There is no database, but there is an

Dec 22, 2022

Remix Stack for deploying to Vercel with remix-auth, Planetscale, Radix UI, TailwindCSS, formatting, linting etc. Written in Typescript.

Remix Stack for deploying to Vercel with remix-auth, Planetscale, Radix UI, TailwindCSS, formatting, linting etc. Written in Typescript.

Remix Synthwave Stack Learn more about Remix Stacks. npx create-remix --template ilangorajagopal/synthwave-stack What's in the stack Vercel deploymen

Dec 25, 2022
Comments
  • 🛸 use react-dom 18 hydration api

    🛸 use react-dom 18 hydration api

    eliminates the console error and seems to run just fine in react 18

    (btw, it was a pleasure making your acquaintance at remix conf, ben—I was the guy talking about the roof geometry app)

    you should consider making this a template repo!

    opened by jeremybanka 1
Owner
Ben Wishovich
I'm passionate about React, Typescript, Python, and Rust. I build unique experiences on the web. Full stack engineer with a background in SQA and Hardware
Ben Wishovich
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
An obsidian plugin that allows code blocks executed interactively in sandbox like jupyter notebooks. Supported language rust、kotlin、python、Javascript、TypeScript etc.

Obsidian Code Emitter This plugin allows code blocks executed interactively like jupyter notebooks. Currently, support languages: Rust Kotlin JavaScri

YiiSh 38 Dec 28, 2022
Drawing Newton's fractal using pure js, rust-wasm, SIMDs, threads and GPU

Newton's fractal Runtime Newton's fractal renderer. >>Click<< to open in your browser Inspired by 3blue1brown's video about Newton's fractal. Drawing

Aleksei 86 Nov 17, 2022
This project is based on the Awesome Books app repo, refactored with ES6 and organized with modules. The purpose of this project is to learn functionality organization using JavaScript modules.

Awesome Books with ES6 and modules A basic app project built with HTML, CSS and JS ES6 to keep track of awesome books. Built With HTML/CSS and JS best

Karla Delgado 10 Aug 27, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
An Electron app using NAPI-RS and a native Rust module to implement a Svelte store.

Svelte Store Written in Rust This repo shows a demo of an Electron app running a Svelte UI that reads and writes a Svelte store, but with a twist: the

Dave Ceddia 27 Sep 20, 2022
Template for generating rust-based native projects

Speedy NAPI-RS template Use this template Click use this template above, then find and replace speedy-sourcemap to your desired naming. Setup Rust too

speedystack 10 Jan 21, 2022
AWS CDK compiled for web (and Node!)

cdk-web ?? DEMO ?? AWS CDK compiled for web (and Node!) cdk-web and aws-cdk-web are functionally identical packages on npm. read about the differences

Sepehr Laal 44 Dec 5, 2022
Solid, AssemblyScriptified and compiled to WebAssembly

solid-as Solid as _____! Solid, AssemblyScriptified and compiled to WebAssembly (very work in progress). Installation TODO Test npm i && npm test TODO

Joe Pea 4 Jul 19, 2022