Skip to content

LIMPIX31/plugin-repack

Repository files navigation

Repack 1.1

Yarn plugin for easy integration of rust wasm into a web or nodejs project

Installation

  1. Install rust nightly toolchain and cargo
  2. Setup rust for wasm32-unknown-unknown target
  3. Install yarn (v3/4)
  4. Install plugin in the project with cargo workspaces + yarn workspaces
yarn plugin import https://raw.githubusercontent.com/LIMPIX31/plugin-repack/master/yarn-plugin/bundles/@yarnpkg/plugin-repack.js
yarn repack install

Usage

Create wasm crate

cargo new --lib hello-world

...and add target

[lib]
crate-type = ["cdylib"]

[repack]
target = "web" # or "nodejs"

...and add crate to cargo workspaces

[workspace]
members = [
  "hello-world"
]

Add crate to yarn workspace dependencies and build

{
 "name": "my-project",
 "version": "1.0.0",
 "main": "src/index.ts",
 "dependencies": {
   "@crate/hello-world": "crate:*"
 }
}

...then make the initial build

yarn

Rebuild crates

# Try rebuild only hello-world
yarn repack rebuild hello-world
# Try rebuild all crates
yarn repack rebuild
# Try rebuild hello-world crate for release
yarn repack rebuild --release hello-world

Import your crate

import { hello_world } from '@crate/hello-world'

Example

You can find examples in examples branch

git checkout examples

Known issues

When using vite, you need to exclude crate

export default defineConfig({
  plugins: [react()],
  build: {
    target: 'esnext'
  },
  optimizeDeps: {
    exclude: ['@crate/my-crate']
  },
})

Misc

@crate scope

...is required. It is needed to restrict your dependencies semantically, so that you can understand that you are importing a crate and not something else

Releases

No releases published

Packages

No packages published