Skip to content

Latest commit

 

History

History

wasm

wasm-pack-template

A template for kick starting a Rust and WebAssembly project using wasm-pack.

Build Status

Built with 🦀🕸 by The Rust and WebAssembly Working Group

About

📚 Read this template tutorial! 📚

This template is designed for compiling Rust libraries into WebAssembly and publishing the resulting package to NPM.

Be sure to check out other wasm-pack tutorials online for other templates and usages of wasm-pack.

🚴 Usage

🐑 Use cargo generate to Clone this Template

Learn more about cargo generate here.

cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project

🛠️ Build with wasm-pack build

wasm-pack build

🔬 Test in Headless Browsers with wasm-pack test

wasm-pack test --headless --firefox

Generate your dart bindings

rm -rf lib/src/flutter_web_wasm_base.dart
dart_js_facade_gen pkg/flutter_web_wasm.d.ts | tee lib/src/flutter_web_wasm_base.dart

🎁 Publish to NPM with wasm-pack publish

wasm-pack publish

Add your depedency Flutter side

In example/lib/web/index.html import your js library via script tag

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>example</title>
</head>

<body>
  <script src="main.dart.js" type="application/javascript"></script>
  <script src="https://unpkg.com/[email protected]/flutter_web_wasm.js"></script>  <!-- Import your freshly generated library from unpk cdn -->
</body>

</html>

In your example/lib/main.dart import your library

import 'package:flutter_web_wasm/flutter_web_wasm.dart';

🔋 Batteries Included