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
| rsw version | vite version |
|---|---|
1.8.x |
2.4.x |
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




Plugin Options
cli: specified package managernpmorpnpm, defaultnpm.root: rust crate root path. default project root path.unLinks:string[]- (npm unlink) uninstalls a package.crates: Item[ ] - (npm link) package name, support npm organization.- Item as string -
'@rsw/hello' - Item as RswCrateOptions -
{ name: '@rsw/hello', outDir: 'custom/path' }
- 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-xc-app: create a project in seconds!
Step1
# With NPM
npm init xc-app
# With Yarn:
yarn create xc-appYou can also directly specify the project name and the template you want to use via additional command line options.
# npm 6.x
npm init xc-app my-wasm-app --template wasm-react
# npm 7+, extra double-dash is needed:
npm init xc-app my-wasm-app -- --template wasm-react
# yarn
yarn create xc-app my-wasm-app --template wasm-reactCurrently supported template presets include wasm-vue3 and wasm-react.
Step2
cd my-wasm-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
Install lencx/rsw-node globally, you can use the rsw command.
npm i -g rsw-node
Example
Step1
npm install -D rsw-node"scripts": {
"rsw:deploy": "rsw && npm run build"
}Step2
{
"root": ".",
"crates": [
"@rsw/chasm",
"@rsw/game-of-life",
"@rsw/excel-read"
]
}Step3
learn-wasm/.github/workflows/deploy.yml
# .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:deploy # 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