# vite-rust-plugin

> Vite plugin for Rust

Latest version **1.0.5** (published 2022-07-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-rust-plugin
pnpm add vite-rust-plugin
yarn add vite-rust-plugin
bun add vite-rust-plugin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-07-18 |
| First published | 2022-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | white-shirt |
| Maintainers | white-shirt |
| Keywords | rust, vite, wasm |

## Links

- npm: https://www.npmjs.com/package/vite-rust-plugin
- npm.io page: https://npm.io/package/vite-rust-plugin

## Dependencies (3)

- [chalk](https://npm.io/package/chalk.md) ^2.4.1
- [watchpack](https://npm.io/package/watchpack.md) ^2.3.1
- [child_process](https://npm.io/package/child_process.md) ^1.0.2

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.5 (latest) — 2022-07-18
- 1.0.4 — 2022-03-11
- 1.0.3 — 2022-03-11
- 1.0.2 — 2022-03-09
- 1.0.1 — 2022-03-08
- 1.0.0 — 2022-03-08

## README

# vite-rust-plugin

Vite plugin for Rust

## Requirement

You need [wasm-pack](https://github.com/rustwasm/wasm-pack) for Rust compile to WebAssembly.

## Install

```sh
npm install --save-dev vite-rust-plugin
```

## Usage

### 1. Creating cargo in your project

```sh
cargo new rust --lib
```

Add the following to Cargo.toml.

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

[dependencies]
wasm-bindgen = "^0.2"
```

src/lib.rs

```rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

#[wasm_bindgen(start)]
pub fn start() {
    log("hello wasm :)");
}
```

### 2. Setting vite.config.js

```js
import { defineConfig } from 'vite';
import ViteRustPlugin from 'vite-rust-plugin';

export default defineConfig({
    base: './',
    plugins: [
        new ViteRustPlugin({
            crateDir: './rust',
            extraArgs: '--no-typescript',
        }),
    ],
});
```

### 3. Setting index.html
The target bundle of wasm-pack build is not yet supported. Therefore, it is necessary to manually import using target web.

```html
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My App</title>
    </head>

    <body>
        <div id="app"></div>
        <h1>Hello</h1>
        <script type="module">
            import init from './rust/pkg';
            (async function () {
                await init();
            })();
        </script>
    </body>
</html>
```

### 4. Start dev server

```sh
vite --open
```

### 5. Build for production

```sh
vite build
```

## Template

You can use [vite-wasm-template](https://github.com/dfjk0/vite-wasm-template).

Execute the following command.

```sh
git clone https://github.com/dfjk0/vite-wasm-template
cd vite-wasm-template
npm install
npm run dev
```

---
_Source: https://npm.io/package/vite-rust-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
