init
This commit is contained in:
commit
7c372afd1e
|
@ -0,0 +1,6 @@
|
|||
/target
|
||||
/bin
|
||||
/pkg
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
wasm-pack.log
|
|
@ -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
|
|
@ -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.
|
|
@ -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
|
||||
```
|
|
@ -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!");
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
dist
|
|
@ -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))
|
|
@ -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>
|
|
@ -0,0 +1,3 @@
|
|||
import * as wasm from 'rust-wasm-template'
|
||||
|
||||
wasm.wasm_main()
|
File diff suppressed because it is too large
Load Diff
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue