Using Cargo workspaces with a `napi-rs` project
Using workspaces to manage crates and dependencies within a default `napi-rs` project.
Creating a new project with https://napi.rs is as easy as:
npm install -g @napi-rs/cli
Followed by...
napi new
... which is great. But I wanted to keep the project within a Cargo Workspace
so I made the following changes (using the project name bslive
, which stands for Browsersync Live, in case you wondered)
Step 1
Create a new directory at the root level of the project
mkdir bslive
Step 2
Move the following files/folders
src/lib.rs
build.rs
Cargo.toml
into
bslive/src/lib.rs
bslive/build.rs
bslive/Cargo.toml
Should look something like:
Step 3
Now create a new Cargo.toml
in the root of the project, to replace the one you just moved
Cargo.toml
workspace.members = ["bslive"]
workspace.resolver = "2"
Step 4
The last is to tell napi
where to build from (since it defaults to the root of the project).
In the package.json
file, find the scripts
section and change both build
and build:debug
to include the
flag --cargo-name <name>
package.json
- "build": "napi build --platform --release",
+ "build": "napi build --cargo-name bslive --platform --release",
- "build:debug": "napi build --platform",
+ "build:debug": "napi build --cargo-name bslive --platform",
Done!
Now you can create lots of separate crates in the root of the project (or under a common subdirectory, like crates
)
and the regular workspace improvements will apply :)