This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "hello_world" | |
version = "0.1.0" | |
authors = ["Jane Doe <jane.doe@example.com>"] | |
edition = "2018" | |
[lib] | |
crate-type = ["cdylib"] | |
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |