This commit is contained in:
thetek 2023-03-24 18:52:02 +01:00
commit 7c372afd1e
13 changed files with 5582 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target
/bin
/pkg
**/*.rs.bk
Cargo.lock
wasm-pack.log

25
Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "rust-wasm-template"
version = "0.1.0"
authors = ["thetek <git@thetek.de>"]
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
console_error_panic_hook = { version = "0.1.7", optional = true } # better debugging for errors, not great for code size
wee_alloc = { version = "0.4.5", optional = true } # smaller, but slower allocator
#js-sys = "0.3.61"
#[dependencies.web-sys]
#version = "0.3.61"
#features = ["Document", "Element", "HtmlCanvasElement", "WebGlBuffer", "WebGlProgram", "WebGlRenderingContext", "WebGlShader", "Window"]
[profile.release]
opt-level = "s" # optimize for small code size

25
LICENSE Normal file
View File

@ -0,0 +1,25 @@
Copyright (c) 2023 thetek <git@thetek.de>
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# Rust & WebAssembly Template
Based on the [Rust and WebAssembly book](https://rustwasm.github.io/docs/book/game-of-life/hello-world.html#clone-the-project-template).
## Usage
Intitial setup:
```bash
wasm-pack build
cd www
npm install
```
Compile `.wasm` file:
```bash
wasm-pack build
```
Run development server:
```bash
cd www
npm run start
```

17
src/lib.rs Normal file
View File

@ -0,0 +1,17 @@
use wasm_bindgen::prelude::*;
mod utils;
// use wee_alloc as the global allocator if the feature is enabled
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn wasm_main() {
// set panic hook when console_error_panic_hook feature is enabled
#[cfg(feature = "console_error_panic_hook")]
utils::set_panic_hook();
utils::alert("Hello, WebAssembly!");
}

11
src/utils.rs Normal file
View File

@ -0,0 +1,11 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
pub fn alert(s: &str);
}
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

2
www/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

5
www/bootstrap.js vendored Normal file
View File

@ -0,0 +1,5 @@
// a dependency graph that contains any wasm must all be imported
// asynchronously. this `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import('./index.js')
.catch(e => console.error('error importing `index.js`:', e))

11
www/index.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rust WebAssembly Template</title>
</head>
<body>
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
<script src="./bootstrap.js"></script>
</body>
</html>

3
www/index.js Normal file
View File

@ -0,0 +1,3 @@
import * as wasm from 'rust-wasm-template'
wasm.wasm_main()

5402
www/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
www/package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "rust-wasm-template",
"version": "0.1.0",
"description": "template for webassembly and rust",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack serve"
},
"keywords": ["webassembly", "wasm", "rust", "webpack"],
"author": "thetek <git@thetek.de>",
"license": "MIT",
"dependencies": {
"rust-wasm-template": "file:../pkg"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"webpack": "^5.76.2",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.13.1"
}
}

27
www/webpack.config.js Normal file
View File

@ -0,0 +1,27 @@
const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path')
module.exports = {
entry: './bootstrap.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bootstrap.js',
},
mode: 'development',
plugins: [
new CopyWebpackPlugin({
patterns: [{ from: 'index.html' }],
})
],
experiments: {
asyncWebAssembly: true,
syncWebAssembly: true,
},
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
compress: true,
port: 3000,
},
}