Deno is a new JavaScript runtime built with Rust and V8that enables you to run JavaScript outside the browser. Deno is more secure than Node.jsbecause it limits network and file system access by default.
One of the cool things about Deno is that it enables you to write and use plugins written in Rust within Deno code. This works thanks to plugins, which can also be written in Rust.
In this tutorial, we’ll show you how to create Deno plugins in Rust. We’ll cover the following:
Plugins in Deno generally provide better performance and provide access to a wider range of tools.
Due to their performant nature, pluginsare often used to perform calculations for heavy tasks such as image processing.Plugins also give you access to a variety of libraries written in other languages, including high-quality Rust crates.
Deno plugin project structure
The plugin project structure is the same as any Deno module. For project structure,you can use this boilerplate.
First, build the Rust boilerplate for the plugin:
cd my_module/native
cargo build
Next, run a test to verify that Deno is picking up the correct library:
cd my_module/native
deno test --unstable --allow-plugin
The boilerplate includes a Rust project in the native directory and a Deno module in the root.
Building a Rust project
The Rust project compiles a dynamic library that is loaded by the Deno runtime. The file type and name of the library depends on the operating system. TheRust project may compile to a so file — dylib or dll— and the name of the compiled file may also be different. The boilerplate can handle three major platforms:Linux, macOS, and Windows.
Why write Deno plugins in Rust?
Deno plugin project structure
git clone https://github.com/anshulrgoyal/deno-rust-starter.git my_module
cd my_module/native
cargo build
cd my_module/native
deno test --unstable --allow-plugin
Building a Rust project
[package]
name = "native"
version = "0.1.0"
authors = ["anshul <anshulgoel151999@gmail.com>"]
edition = "2018"
[lib]
name = "native"
crate-type = ["cdylib"]
[dependencies]
deno_core = "0.75.0"