Skip to content

Instantly share code, notes, and snippets.

View fvilches17's full-sized avatar
🤠

Francisco Vilches fvilches17

🤠
View GitHub Profile
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Jane Doe <jane.doe@example.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
async function handleGreetButtonClickEvent(event) {
event.preventDefault();
const { greet } = await import('../src/hello_world/pkg');
greet(); //When greet() is called, .wasm will update the <h1> title in index.html
}
document.querySelector('#btn-greet').onclick = handleGreetButtonClickEvent;
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WasmPackPlugin = require('@wasm-tool/wasm-pack-plugin');
function loadOutput(environment) {
const filename = environment.production ? 'scripts/[name].[hash].min.js' : 'scripts/[name].js';
const chunkFilename = environment.production ? '[name].[hash].min.js' : '[name].js';
return {
path: path.resolve(__dirname, 'dist'),
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn greet() {
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let header = document.get_element_by_id("greeting").unwrap();
header.set_inner_html("<h1>[Rust Says] Hello Wasm!</h1>");
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello Wasm</title>
</head>
<body>
<h1 id="greeting">[HTML Says] Hello World</h1>