Package Exports
- vite-plugin-rsw
- vite-plugin-rsw/dist/index.js
- vite-plugin-rsw/dist/index.mjs
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
| rsw version | vite version |
|---|---|
>= 1.8.0 |
>= 2.4.0 |
1.7.0 |
2.0.0 ~ 2.3.8 |
Pre-installed
Article
Features
- startup optimization
- enable debug mode:
DEBUG=rsw yarn dev - friendly error message: browser and terminal
- automatically generate template when
cratedoes not exist - multiple rust crate
- compile
- hot-update
- watch for dependent workspace crates (#18)




Plugin Options
cli: specified package managernpmorpnpm, default valuenpm.root: rust crate root path. default project root path.unLinks:string[]- (npm unlink) uninstalls a package.unwatch:string[]- stop watching files, directories, or glob patterns, takes an array of strings.profile:'dev' | 'release' | 'profiling', default valuedev. wasm-pack build.profiletarget:'bundler' | 'web' | 'nodejs' | 'no-modules', default valuewebwasm-pack build.targetcrates: Item[ ] - (npm link) package name, support npm organization. wasm-pack build doc- Item as string -
'@rsw/hello' - Item as RswCrateOptions -
{ name: '@rsw/hello', outDir: 'custom/path', unwatch: ['./pkg'] }useunwatchto avoid infinite loops when crate is built in a watched directory #24name- support--out-nameandscopeoutDir---out-dirtarget-'bundler' | 'web' | 'nodejs' | 'no-modules', default valuewebprofile-'dev' | 'release' | 'profiling', default valuedevmode-'no-install' | 'normal', no default valueextraOpts- extra optionsunwatch- stop watching files, directories, or glob patterns, takes an array of strings
- Item as string -
⚠️ 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.
Quick Start
create-mpl: create a project in seconds!
Step1
# With NPM
npm init mpl@lastst
# With Yarn:
yarn create mpl# --type, -t: web | wasm | mini | electron | extension | chrome | github
# npm 6.x
npm init mpl@latest my-app --type wasm
# npm 7+, extra double-dash is needed:
npm init mpl@latest my-app -- --type wasm
# yarn
yarn create mpl my-app --type wasm
# pnpm
pnpm create mpl my-app -- --type wasmCurrently supported template presets include wasm-vue3 and wasm-react.
Step2
cd my-app
npm install
npm run devStep3
You can edit the crates in ViteRsw to create your own rust crates.
// vite.config.ts
import { defineConfig } from 'vite';
import ViteRsw from 'vite-plugin-rsw';
export default defineConfig({
plugins: [
ViteRsw({
crates: [
'@rsw/hey', // npm org
'rsw-test', // npm package
// https://github.com/lencx/vite-plugin-rsw/issues/8#issuecomment-820281861
// outDir: use `path.resolve` or relative path.
{ name: '@rsw/hello', outDir: 'custom/path' },
],
}),
],
});Step4
Use exported Rust things from JavaScript with ECMAScript modules!
import init, { greet } from '@rsw/hey';
// 1. `WebAssembly.Instance` initialization
init();
// 2. Make sure this method is executed after `init()` is called
greet('World!');Remote Deployment
{
"scripts": {
+ "rsw:build": "rsw && npm run build"
}
}Example
"scripts": {
"rsw:build": "rsw && npm run build"
}# .github/workflows/deploy.yml
name: github pages
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: yarn
+ - run: yarn rsw:build # use rsw-node build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GH_TOKEN }}
publish_dir: ./dist
Video
Examples
Credits
Backers
Thank you to all our backers! 🙏
微信
群二维码已过期,关注公众号《浮之静》,发送“进群”,我将拉你进群一起学习。

License
MIT License © 2021 lencx