Commit ecb68f68 authored by Kai Brandes's avatar Kai Brandes

add webassembly build

parent 0770e31a
......@@ -9,4 +9,9 @@
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
\ No newline at end of file
**/*.rs.bk
bin
pkg
wasm-pack.log
......@@ -4,8 +4,28 @@ version = "0.1.0"
authors = ["Kai Brandes <Kai Brandes>"]
[dependencies]
hyper = "0.12"
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.1", optional = true }
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
wee_alloc = { version = "0.4.1", optional = true }
[lib]
name = "adventofcode_2017_lib"
path = "src/lib.rs"
crate-type = ["cdylib", "lib"]
[features]
default-features = ["console_error_panic_hook", "wee_alloc"]
[[bin]]
name = "adventofcode_2017"
path = "src/main.rs"
path = "src/bin.rs"
extern crate adventofcode_2017_lib;
fn main() {
println!("Welcome to my adventofcode-2017 solutions.");
println!("Solution for day 1a is: {}", adventofcode_2017_lib::results::day_1_a());
println!("Solution for day 1b is: {}", adventofcode_2017_lib::results::day_1_b());
println!("Solution for day 2a is: {}", adventofcode_2017_lib::results::day_2_a());
println!("Solution for day 2b is: {}", adventofcode_2017_lib::results::day_2_b());
println!("Solution for day 3a is: {}", adventofcode_2017_lib::results::day_3_a());
println!("Solution for day 3b is: {}", adventofcode_2017_lib::results::day_3_b());
}
\ No newline at end of file
extern crate cfg_if;
extern crate wasm_bindgen;
pub mod day1;
pub mod day2;
pub mod day3;
pub mod results;
mod utils;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, Advent!");
}
extern crate adventofcode_2017;
fn main() {
println!("Welcome to my adventofcode-2017 solutions.");
println!("Solution for day 1a is: {}", adventofcode_2017::results::day_1_a());
println!("Solution for day 1b is: {}", adventofcode_2017::results::day_1_b());
println!("Solution for day 2a is: {}", adventofcode_2017::results::day_2_a());
println!("Solution for day 2b is: {}", adventofcode_2017::results::day_2_b());
println!("Solution for day 3a is: {}", adventofcode_2017::results::day_3_a());
println!("Solution for day 3b is: {}", adventofcode_2017::results::day_3_b());
}
\ No newline at end of file
use cfg_if::cfg_if;
cfg_if! {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function to get better error messages if we ever panic.
if #[cfg(feature = "console_error_panic_hook")] {
extern crate console_error_panic_hook;
pub use console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}
cfg_if! {
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
extern crate adventofcode_2017;
extern crate adventofcode_2017_lib;
#[test]
fn it_shoul_assert_result_of_day1_a() {
assert_eq!(1031, adventofcode_2017::results::day_1_a());
assert_eq!(1031, adventofcode_2017_lib::results::day_1_a());
}
#[test]
fn it_shoul_assert_result_of_day1_b() {
assert_eq!(1080, adventofcode_2017::results::day_1_b());
assert_eq!(1080, adventofcode_2017_lib::results::day_1_b());
}
\ No newline at end of file
extern crate adventofcode_2017;
extern crate adventofcode_2017_lib;
#[test]
fn it_shoul_assert_result_of_day2_a() {
assert_eq!(34581, adventofcode_2017::results::day_2_a());
assert_eq!(34581, adventofcode_2017_lib::results::day_2_a());
}
#[test]
fn it_shoul_assert_result_of_day2_b() {
assert_eq!(214, adventofcode_2017::results::day_2_b());
assert_eq!(214, adventofcode_2017_lib::results::day_2_b());
}
\ No newline at end of file
extern crate adventofcode_2017;
extern crate adventofcode_2017_lib;
#[test]
fn it_shoul_assert_result_of_day3_a() {
assert_eq!(430, adventofcode_2017::results::day_3_a());
assert_eq!(430, adventofcode_2017_lib::results::day_3_a());
}
#[test]
fn it_shoul_assert_result_of_day3_b() {
assert_eq!(312453, adventofcode_2017::results::day_3_b());
assert_eq!(312453, adventofcode_2017_lib::results::day_3_b());
}
node_modules
dist
# create-wasm-app
> an npm init project for generating a project that consumes rust-generated wasm via webpack
## Usage
```
npm init wasm-app
```
## What's Included
- `.gitignore`: ignores `node_modules`
- `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you
- `README.md`: the file you are reading now!
- `index.html`: a bare bones html document that includes the webpack bundle
- `index.js`: example js file with a comment showing how to import and use a wasm pkg
- `package.json` and `package-lock.json`:
- pulls in devDependencies for using webpack:
- [`webpack`](https://www.npmjs.com/package/webpack)
- [`webpack-cli`](https://www.npmjs.com/package/webpack-cli)
- [`webpack-dev-server`](https://www.npmjs.com/package/webpack-dev-server)
- defines a `start` script to run `webpack-dev-server`
- `webpack.config.js`: configuration file for bundling your js with webpack
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
// 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));
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello wasm-pack!</title>
</head>
<body>
<script src="./bootstrap.js"></script>
</body>
</html>
import * as wasm from "adventofcode_2017";
wasm.greet();
This diff is collapsed.
{
"name": "adventofcode-2017",
"version": "0.1.0",
"description": "my solutions for adventofcode-2017 brought to you by webassembly and rust",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.codecentric.de/kai.brandes/adventofcode-2017"
},
"keywords": [
"webassembly",
"wasm",
"rust",
"webpack"
],
"author": "Kai Brandes <kai.brandes@codecentric.de>",
"devDependencies": {
"hello-wasm-pack": "^0.1.0",
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"copy-webpack-plugin": "^4.5.2"
}
}
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(['index.html'])
],
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment