Package Exports
- vite-plugin-rsw
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vite-plugin-rsw) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
vite-plugin-rsw
wasm-pack plugin for vite@v2
Pre-installed
Features
- mode:
development buildorrelease build - multiple rust crate
- compile
- hot-update
Quick Start
# more: https://github.com/lencx/create-xc-app
npx create-xc-app my-app --template wasm-reactGetting Started
Step1
# install rsw
npm i -D vite-plugin-rsw
# or
yarn add -D vite-plugin-rsw// vite.config.ts
import { defineConfig } from "vite";
import ViteRsw from "vite-plugin-rsw";
export default defineConfig({
plugins: [
ViteRsw({
mode: "release",
crates: ["@rsw/hey", "rsw-test"],
}),
],
});Step2
cargo new --lib <crate_name># Cargo.toml
[lib]
crate-type = ["cdylib"]
wasm-opt = false
[profile.release]
lto = true
opt-level = 'z'
[dependencies]
wasm-bindgen = "0.2.69"// src/lib.rs
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}Plugin Options
root: rust crate root path. default project root path.modedev: create a development build. Enable debug info, and disable optimizations.release: create a release build. Enable optimizations and disable debug info.
target: sets the target environment.web: defaultbundlernodejsno-modules
crates:string[]: package name, support npm organization.
⚠️ Note: Before performing the
vite build, at least oncevite dev, generatewasm package (rust-crate/pkg). In the project,wasm packageis installed byvite-plugin-rswin the form ofnpm link, otherwise it will errorCan not find module 'rust-crate' or its corresponding type declarations.
微信


License
MIT License © 2021 lencx