vite-plugin-moonbit
v0.1.2
Published
Vite plugin for MoonBit - import MoonBit modules with mbt: prefix
Readme
vite-plugin-moonbit
Vite plugin for MoonBit projects. Supports both JS and WASM-GC backends.
Features
- Import resolution via
mbt:prefix - Auto-starts
moon build --watch - HMR on file changes
- JS and WASM-GC backend support
Install
pnpm add -D vite-plugin-moonbit// vite.config.ts
import { defineConfig } from 'vite';
import { moonbit } from 'vite-plugin-moonbit';
export default defineConfig({
plugins: [
moonbit({
target: "js",
// run: `moon build --target js --watch` in vite
// If you want to bulid manually, set `false` and `moon build`
watch: true
})
],
});Quick Start
npx tiged github:mizchi/vite-plugin-moonbit/examples/js_project myapp
cd myapp && pnpm install
moon build && pnpm devUsage
JS Backend (default)
// vite.config.ts
import { defineConfig } from 'vite';
import moonbit from 'vite-plugin-moonbit';
export default defineConfig({
plugins: [moonbit()]
});// main.ts
import { greet } from 'mbt:username/app';Optional: tsconfig.json's paths
{
"compilerOptions": {
// ...
"paths": {
"mbt:internal/app": [
"./target/js/release/build/app.js"
]
}
}
}Check out: npx tiged mizchi/vite-plugin-moonbit/examples/wasm_project myapp
WASM-GC Backend
// vite.config.ts
import { defineConfig } from 'vite';
import moonbit from 'vite-plugin-moonbit';
export default defineConfig({
plugins: [moonbit({ target: 'wasm-gc' })]
});// main.ts
import init from './target/wasm-gc/release/build/app.wasm?init';
const instance = await init();
const { add } = instance.exports as { add: (a: number, b: number) => number };
add(1, 2);Check out: npx tiged mizchi/vite-plugin-moonbit/examples/wasm_project myapp
Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| root | string | cwd() | MoonBit project root |
| watch | boolean | true (dev) | Run moon build --watch |
| target | 'js' \| 'wasm' \| 'wasm-gc' | 'js' | Build target |
| showLogs | boolean | true | Show build logs |
Path Resolution
| Import | JS Backend | WASM-GC Backend |
|--------|------------|-----------------|
| mbt:user/pkg | target/js/release/build/pkg.js | target/wasm-gc/release/build/pkg.wasm |
License
MIT
