first commit: mc-cached-wasm

This commit is contained in:
Quinn Dougherty 2022-08-15 19:38:25 -07:00
parent 1cd10a9cad
commit a635219f46
19 changed files with 15311 additions and 14083 deletions

View File

@ -1,5 +1,20 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"gentype": {
"inputs": {
"nixpkgs": [
@ -42,21 +57,22 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1660485612,
"narHash": "sha256-sSLW1KaB1adKTJn9+Ja3h3AaS7QCZyhUKiSUStcLg80=",
"lastModified": 1660496378,
"narHash": "sha256-sgAhmrC1iSnl5T2VPPiMpciH1aRw5c7PYEdXX6jd6Gk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "6512b21eabb4d52e87ea2edcf31a288e67b2e4f8",
"rev": "879121648fe522b38cc1cf75aef160a14a1f2e7b",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-unstable",
"ref": "nixos-22.05",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"gentype": "gentype",
"hercules-ci-effects": "hercules-ci-effects",
"nixpkgs": "nixpkgs"

547
flake.nix
View File

@ -3,7 +3,7 @@
# Derived from https://gitlab.com/Silvers_Gw2/Stats_Frontend/-/blob/cc5d783abd54e95363410592c390ca6925462262/flake.nix
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
nixpkgs.url = "nixpkgs/nixos-22.05";
gentype = {
url = "github:quinn-dougherty/genType";
inputs.nixpkgs.follows = "nixpkgs";
@ -12,302 +12,309 @@
url = "github:hercules-ci/hercules-ci-effects";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils = {
url = "github:numtide/flake-utils";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, gentype, hercules-ci-effects, ... }:
let
# globals
system = "x86_64-linux";
pkgs = import nixpkgs {
system = system;
overlays = [
hercules-ci-effects.overlay
(final: prev: {
# set the node version here
nodejs = prev.nodejs-16_x;
# The override is the only way to get it into mkYarnModules
})
];
};
buildInputsCommon = with pkgs; [ nodejs yarn ];
prettierCommon = with pkgs.nodePackages; [ prettier ];
pkgWhich = [ pkgs.which ];
outputs = { self, nixpkgs, gentype, hercules-ci-effects, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
# globals
hciSystem = "x86_64-linux";
pkgs = import nixpkgs {
system = system;
overlays = [
(final: prev: {
# set the node version here
nodejs = prev.nodejs-16_x;
# The override is the only way to get it into mkYarnModules
})
];
};
hciPkgs = import nixpkgs { system = hciSystem; };
effects = hercules-ci-effects.lib.withPkgs hciPkgs;
# To get prettier into later source trees
monorepo-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-monorepo_source";
src = ./.;
packageJSON = ./package.json;
yarnLock = ./yarn.lock;
};
buildInputsCommon = with pkgs; [ nodejs yarn ];
prettierCommon = with pkgs.nodePackages; [ prettier ];
pkgWhich = [ pkgs.which ];
# packages in subrepos
lang-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-lang_source";
src = ./packages/squiggle-lang;
packageJSON = ./packages/squiggle-lang/package.json;
yarnLock = ./yarn.lock;
# extraBuildInputs = prettierCommon;
pkgConfig = {
rescript = {
buildInputs = pkgWhich ++ [ pkgs.gcc_multi ];
postInstall = ''
echo "PATCHELF'ING RESCRIPT EXECUTABLES (INCL NINJA)"
# Patching interpreter for linux/*.exe's
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD linux/*.exe && echo "- patched interpreter for linux/*.exe's"
# To get prettier into later source trees
monorepo-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-monorepo_source";
src = ./.;
packageJSON = ./package.json;
yarnLock = ./yarn.lock;
};
# Replacing needed shared library for linux/ninja.exe
THE_SO=$(find /nix/store/*/lib64 -name libstdc++.so.6 | head -n 1)
patchelf --replace-needed libstdc++.so.6 $THE_SO linux/ninja.exe && echo "- replaced needed for linux/ninja.exe"
'';
};
bisect_ppx = {
buildInputs =
pkgWhich; # ++ (with pkgs; [ ocaml nodePackages.esy ocamlPackages.bisect_ppx ]);
postInstall = ''
echo "PATCHELF'ING BISECT_PPX EXECUTABLE"
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD bin/linux/ppx
patchelf --set-interpreter $THE_LD bin/linux/bisect-ppx-report
'';
};
gentype = {
postInstall = ''
mv gentype.exe ELFLESS-gentype.exe
cp ${
gentype.outputs.defaultPackage."${system}"
}/GenType.exe gentype.exe
'';
# packages in subrepos
lang-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-lang_source";
src = ./packages/squiggle-lang;
packageJSON = ./packages/squiggle-lang/package.json;
yarnLock = ./yarn.lock;
# extraBuildInputs = prettierCommon;
pkgConfig = {
rescript = {
buildInputs = pkgWhich ++ [ pkgs.gcc_multi ];
postInstall = ''
echo "PATCHELF'ING RESCRIPT EXECUTABLES (INCL NINJA)"
# Patching interpreter for linux/*.exe's
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD linux/*.exe && echo "- patched interpreter for linux/*.exe's"
# Replacing needed shared library for linux/ninja.exe
THE_SO=$(find /nix/store/*/lib64 -name libstdc++.so.6 | head -n 1)
patchelf --replace-needed libstdc++.so.6 $THE_SO linux/ninja.exe && echo "- replaced needed for linux/ninja.exe"
'';
};
bisect_ppx = {
buildInputs =
pkgWhich; # ++ (with pkgs; [ ocaml nodePackages.esy ocamlPackages.bisect_ppx ]);
postInstall = ''
echo "PATCHELF'ING BISECT_PPX EXECUTABLE"
THE_LD=$(patchelf --print-interpreter $(which mkdir))
patchelf --set-interpreter $THE_LD bin/linux/ppx
patchelf --set-interpreter $THE_LD bin/linux/bisect-ppx-report
'';
};
gentype = {
postInstall = ''
mv gentype.exe ELFLESS-gentype.exe
cp ${
gentype.outputs.defaultPackage."${system}"
}/GenType.exe gentype.exe
'';
};
};
};
};
lang-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-lint";
src = lang-yarnPackage
+ "/libexec/@quri/squiggle-lang/deps/@quri/squiggle-lang";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = ''
yarn lint:prettier
yarn lint:rescript
'';
installPhase = "mkdir -p $out";
};
lang-build = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-build";
# `peggy` is in the `node_modules` that's adjacent to `deps`.
src = lang-yarnPackage + "/libexec/@quri/squiggle-lang";
buildInputs = buildInputsCommon;
buildPhase = ''
mv node_modules deps
pushd deps/@quri/squiggle-lang
yarn --offline build:peggy
yarn --offline build:rescript
yarn --offline build:typescript
lang-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-lint";
src = lang-yarnPackage
+ "/libexec/@quri/squiggle-lang/deps/@quri/squiggle-lang";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = ''
yarn lint:prettier
yarn lint:rescript
'';
installPhase = "mkdir -p $out";
};
lang-build = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-build";
# `peggy` is in the `node_modules` that's adjacent to `deps`.
src = lang-yarnPackage + "/libexec/@quri/squiggle-lang";
buildInputs = buildInputsCommon;
buildPhase = ''
mv node_modules deps
pushd deps/@quri/squiggle-lang
yarn --offline build:peggy
yarn --offline build:rescript
yarn --offline build:typescript
# custom gitignore so that the flake keeps build artefacts
mv .gitignore GITIGNORE
sed -i /Reducer_Peggy_GeneratedParser.js/d GITIGNORE
sed -i /\*.bs.js/d GITIGNORE
sed -i /\*.gen.ts/d GITIGNORE
sed -i /\*.gen.tsx/d GITIGNORE
sed -i /\*.gen.js/d GITIGNORE
sed -i /helpers.js/d GITIGNORE
# custom gitignore so that the flake keeps build artefacts
mv .gitignore GITIGNORE
sed -i /Reducer_Peggy_GeneratedParser.js/d GITIGNORE
sed -i /\*.bs.js/d GITIGNORE
sed -i /\*.gen.ts/d GITIGNORE
sed -i /\*.gen.tsx/d GITIGNORE
sed -i /\*.gen.js/d GITIGNORE
sed -i /helpers.js/d GITIGNORE
popd
'';
installPhase = ''
mkdir -p $out
# mkdir -p $out/node_modules
mv deps/@quri/squiggle-lang/GITIGNORE deps/@quri/squiggle-lang/.gitignore
popd
'';
installPhase = ''
mkdir -p $out
# mkdir -p $out/node_modules
mv deps/@quri/squiggle-lang/GITIGNORE deps/@quri/squiggle-lang/.gitignore
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-lang/node_modules deps/@quri/squiggle-lang/NODE_MODULES
mv deps/node_modules deps/@quri/squiggle-lang
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-lang/node_modules deps/@quri/squiggle-lang/NODE_MODULES
mv deps/node_modules deps/@quri/squiggle-lang
# the proper install phase
cp -r deps/@quri/squiggle-lang/. $out
'';
};
lang-test = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-test";
src = lang-build;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline test
'';
installPhase = ''
mkdir -p $out
cp -r . $out
'';
};
lang-bundle = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-bundle";
src = lang-test;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline bundle
'';
installPhase = ''
mkdir -p $out
cp -r dist $out
cp *.json $out/dist
'';
};
# the proper install phase
cp -r deps/@quri/squiggle-lang/. $out
'';
};
lang-test = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-test";
src = lang-build;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline test
'';
installPhase = ''
mkdir -p $out
cp -r . $out
'';
};
lang-bundle = pkgs.stdenv.mkDerivation {
name = "squiggle-lang-bundle";
src = lang-test;
buildInputs = buildInputsCommon;
buildPhase = ''
yarn --offline bundle
'';
installPhase = ''
mkdir -p $out
cp -r dist $out
cp *.json $out/dist
'';
};
componentsPackageJson =
let
componentsPackageJson = let
raw = pkgs.lib.importJSON ./packages/components/package.json;
modified = pkgs.lib.recursiveUpdate raw { dependencies.react-dom = "^18.2.0"; };
modified = pkgs.lib.recursiveUpdate raw {
dependencies.react-dom = "^18.2.0";
};
packageJsonString = builtins.toJSON modified;
in
pkgs.writeText "packages/components/patched-package.json" packageJsonString;
components-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-components_source";
buildInputs = buildInputsCommon;
src = ./packages/components;
packageJSON = componentsPackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
};
components-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-components-lint";
src = components-yarnPackage
+ "/libexec/@quri/squiggle-components/deps/@quri/squiggle-components";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = "yarn lint";
installPhase = "mkdir -p $out";
};
components-package-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-package-build";
src = components-yarnPackage + "/libexec/@quri/squiggle-components";
buildInputs = buildInputsCommon;
buildPhase = ''
cp -r node_modules/@quri/squiggle-lang deps/@quri
pushd deps/@quri/squiggle-components
in pkgs.writeText "packages/components/patched-package.json"
packageJsonString;
components-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-components_source";
buildInputs = buildInputsCommon;
src = ./packages/components;
packageJSON = componentsPackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
};
components-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-components-lint";
src = components-yarnPackage
+ "/libexec/@quri/squiggle-components/deps/@quri/squiggle-components";
buildInputs = buildInputsCommon ++ prettierCommon;
buildPhase = "yarn lint";
installPhase = "mkdir -p $out";
};
components-package-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-package-build";
src = components-yarnPackage + "/libexec/@quri/squiggle-components";
buildInputs = buildInputsCommon;
buildPhase = ''
cp -r node_modules/@quri/squiggle-lang deps/@quri
pushd deps/@quri/squiggle-components
yarn --offline build:cjs
yarn --offline build:css
popd
'';
installPhase = ''
mkdir -p $out
yarn --offline build:cjs
yarn --offline build:css
popd
'';
installPhase = ''
mkdir -p $out
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-components/node_modules deps/@quri/squiggle-components/NODE_MODULES
mv node_modules deps/@quri/squiggle-components
# annoying hack because permissions on transitive dependencies later on
mv deps/@quri/squiggle-components/node_modules deps/@quri/squiggle-components/NODE_MODULES
mv node_modules deps/@quri/squiggle-components
# patching .gitignore so flake keeps build artefacts
sed -i /dist/d deps/@quri/squiggle-components/.gitignore
cp -r deps/@quri/squiggle-components/. $out
'';
};
components-site-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-storybook";
src = components-package-build;
buildInputs = buildInputsCommon;
buildPhase = "yarn build:storybook";
installPhase = ''
mkdir -p $out
# patching .gitignore so flake keeps build artefacts
sed -i /dist/d deps/@quri/squiggle-components/.gitignore
cp -r deps/@quri/squiggle-components/. $out
'';
};
components-site-build = pkgs.stdenv.mkDerivation {
name = "squiggle-components-storybook";
src = components-package-build;
buildInputs = buildInputsCommon;
buildPhase = "yarn build:storybook";
installPhase = ''
mkdir -p $out
# patching .gitignore so flake keeps build artefacts
sed -i /\build/d .gitignore
sed -i /storybook-static/d .gitignore
'';
};
# patching .gitignore so flake keeps build artefacts
sed -i /\build/d .gitignore
sed -i /storybook-static/d .gitignore
'';
};
websitePackageJson =
let
websitePackageJson = let
raw = pkgs.lib.importJSON ./packages/website/package.json;
modified = pkgs.lib.recursiveUpdate raw {
dependencies.postcss-import = "^14.1.0";
dependencies.tailwindcss = "^3.1.8";
};
packageJsonString = builtins.toJSON modified;
in
pkgs.writeText "packages/website/patched-package.json" packageJsonString;
website-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-website_source";
src = ./packages/website;
packageJSON = websitePackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
packageResolutions."@quri/squiggle-components" =
components-package-build;
workspaceDependencies = [ lang-yarnPackage components-yarnPackage ];
};
website-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-website-lint";
buildInputs = buildInputsCommon ++ prettierCommon;
src = website-yarnPackage
+ "/libexec/squiggle-website/deps/squiggle-website";
buildPhase = "yarn --offline lint";
installPhase = "mkdir -p $out";
};
website = pkgs.stdenv.mkDerivation {
name = "squiggle-website";
buildInputs = buildInputsCommon;
src = website-yarnPackage + "/libexec/squiggle-website";
buildPhase = ''
pushd deps/squiggle-website
yarn --offline build
popd
'';
installPhase = ''
mkdir -p $out
cp -r $src/build $out
'';
};
in rec {
in pkgs.writeText "packages/website/patched-package.json"
packageJsonString;
website-yarnPackage = pkgs.mkYarnPackage {
name = "squiggle-website_source";
src = ./packages/website;
packageJSON = websitePackageJson;
yarnLock = ./yarn.lock;
packageResolutions."@quri/squiggle-lang" = lang-build;
packageResolutions."@quri/squiggle-components" =
components-package-build;
workspaceDependencies = [ lang-yarnPackage components-yarnPackage ];
};
website-lint = pkgs.stdenv.mkDerivation {
name = "squiggle-website-lint";
buildInputs = buildInputsCommon ++ prettierCommon;
src = website-yarnPackage
+ "/libexec/squiggle-website/deps/squiggle-website";
buildPhase = "yarn --offline lint";
installPhase = "mkdir -p $out";
};
website = pkgs.stdenv.mkDerivation {
name = "squiggle-website";
buildInputs = buildInputsCommon;
src = website-yarnPackage + "/libexec/squiggle-website";
buildPhase = ''
pushd deps/squiggle-website
yarn --offline build
popd
'';
installPhase = ''
mkdir -p $out
cp -r $src/build $out
'';
};
in rec {
checks.${system} = {
lang-lint = lang-lint;
lang-test = lang-test;
components-lint = components-lint;
docusaurus-lint = website-lint;
};
packages.${system} = {
default = website;
lang-bundle = lang-bundle;
components = components-package-build;
storybook = components-site-build;
docs-site = website;
# tmp = {
# lang-build = lang-build;
# components-yarnPkg = components-yarnPackage;
# components-lint = components-lint;
# };
};
checks = flake-utils.lib.flattenTree {
lang-lint = lang-lint;
lang-test = lang-test;
components-lint = components-lint;
docusaurus-lint = website-lint;
};
packages = flake-utils.lib.flattenTree {
default = website;
lang-bundle = lang-bundle;
components = components-package-build;
storybook = components-site-build;
docs-site = website;
# tmp = {
# lang-build = lang-build;
# components-yarnPkg = components-yarnPackage;
# components-lint = components-lint;
# };
};
# herc
herculesCI.onPush = {
lang.outputs = {
squiggle-lang-lint = checks.${system}.lang-lint;
squiggle-lang-test = checks.${system}.lang-test;
squiggle-lang-build = lang-build;
squiggle-lang-bundle = packages.${system}.lang-bundle;
# herc
herculesCI.onPush = {
lang.outputs = {
squiggle-lang-lint = checks.${hciSystem}.lang-lint;
squiggle-lang-test = checks.${hciSystem}.lang-test;
squiggle-lang-build = lang-build;
squiggle-lang-bundle = packages.${hciSystem}.lang-bundle;
};
components.outputs = {
squiggle-components = packages.${hciSystem}.components;
squiggle-components-lint = checks.${hciSystem}.components-lint;
squiggle-components-storybook = packages.${hciSystem}.storybook;
};
docs-site.outputs = {
squiggle-website = packages.${hciSystem}.docs-site;
docusaurus-lint = checks.${hciSystem}.docusaurus-lint;
};
};
components.outputs = {
squiggle-components = packages.${system}.components;
squiggle-components-lint = checks.${system}.components-lint;
squiggle-components-storybook = packages.${system}.storybook;
devShells.${system}.default = pkgs.mkShell {
name = "squiggle-wasm-development-shell";
buildInputs = with pkgs; [
wasm-pack
cargo
cargo-generate
yarn
rustup
pkg-config
openssl
];
};
docs-site.outputs = {
squiggle-website = packages.${system}.docs-site;
docusaurus-lint = checks.${system}.docusaurus-lint;
};
};
devShells.${system}.default = pkgs.mkShell {
name = "squiggle-wasm-development-shell";
buildInputs = with pkgs; [
wasm-pack
cargo
cargo-generate
yarn
rustup
pkg-config
openssl
];
};
};
});
}

View File

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

View File

@ -1,40 +0,0 @@
[package]
name = "rust"
version = "0.1.0"
authors = ["Quinn Dougherty <quinnd@riseup.net>"]
edition = "2018"
[package.metadata.wasm-pack.profile]
dev.wasm-opt = false
profiling.wasm-opt = false
release.wasm-opt = false
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
getrandom = { version = "0.2", features = ["js"] }
rand_distr = "0.4.3"
wasm-bindgen = "0.2.63"
# 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.6", 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.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@ -1,69 +0,0 @@
<div align="center">
<h1><code>wasm-pack-template</code></h1>
<strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong>
<p>
<a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a>
</p>
<h3>
<a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a>
<span> | </span>
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
</h3>
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
</div>
## About
[**📚 Read this template tutorial! 📚**][template-docs]
This template is designed for compiling Rust libraries into WebAssembly and
publishing the resulting package to NPM.
Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other
templates and usages of `wasm-pack`.
[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html
## 🚴 Usage
### 🐑 Use `cargo generate` to Clone this Template
[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
```
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project
```
### 🛠️ Build with `wasm-pack build`
```
wasm-pack build
```
### 🔬 Test in Headless Browsers with `wasm-pack test`
```
wasm-pack test --headless --firefox
```
### 🎁 Publish to NPM with `wasm-pack publish`
```
wasm-pack publish
```
## 🔋 Batteries Included
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
between WebAssembly and JavaScript.
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
for logging panic messages to the developer console.
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized
for small code size.

View File

@ -1,23 +0,0 @@
mod utils;
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// implement monte carlo in rust
//
#[wasm_bindgen]
pub fn greet() {
alert("Hello, rust!");
}

View File

@ -1,10 +0,0 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -1,13 +0,0 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

View File

@ -1,15 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
mkShell {
buildInputs = [
wasm-pack
cargo-generate
nodePackages.npm
rustup
cargo
pkg-config
openssl
];
}

View File

@ -0,0 +1,5 @@
node_modules
/dist
/target
/pkg
/wasm-pack.log

View File

@ -0,0 +1,55 @@
# You must change these to your own details.
[package]
name = "squiggle-mc-cached-wasm"
description = "Caching for Monte Carlo simulations in WebAssembly"
version = "0.0.1"
authors = ["Quinn <quinn@quantifieduncertainty.org>"]
categories = ["wasm"]
readme = "README.md"
edition = "2018"
[lib]
crate-type = ["cdylib"]
[profile.release]
# This makes the compiled code faster and smaller, but it makes compiling slower,
# so it's only enabled in release mode.
lto = true
[features]
# If you uncomment this line, it will enable `wee_alloc`:
#default = ["wee_alloc"]
[dependencies]
# The `wasm-bindgen` crate provides the bare minimum functionality needed
# to interact with JavaScript.
wasm-bindgen = "0.2.45"
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. However, it is slower than the default
# allocator, so it's not enabled by default.
wee_alloc = { version = "0.4.2", optional = true }
# Numerical things
getrandom = { version = "0.2", features = ["js"] }
rand_distr = "0.4.3"
# The `web-sys` crate allows you to interact with the various browser APIs,
# like the DOM.
[dependencies.web-sys]
version = "0.3.22"
features = ["console"]
# 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 it's only enabled
# in debug mode.
[target."cfg(debug_assertions)".dependencies]
console_error_panic_hook = "0.1.5"
# These crates are used for running unit tests.
[dev-dependencies]
wasm-bindgen-test = "0.2.45"
futures = "0.1.27"
js-sys = "0.3.22"
wasm-bindgen-futures = "0.3.22"

View File

@ -0,0 +1,48 @@
## How to install
```sh
npm install
```
## How to run in debug mode
```sh
# Builds the project and opens it in a new browser tab. Auto-reloads when the project changes.
npm start
```
## How to build in release mode
```sh
# Builds the project and places it into the `dist` folder.
npm run build
```
## How to run unit tests
```sh
# Runs tests in Firefox
npm test -- --firefox
# Runs tests in Chrome
npm test -- --chrome
# Runs tests in Safari
npm test -- --safari
```
## What does each file do?
* `Cargo.toml` contains the standard Rust metadata. You put your Rust dependencies in here. You must change this file with your details (name, description, version, authors, categories)
* `package.json` contains the standard npm metadata. You put your JavaScript dependencies in here. You must change this file with your details (author, name, version)
* `webpack.config.js` contains the Webpack configuration. You shouldn't need to change this, unless you have very special needs.
* The `js` folder contains your JavaScript code (`index.js` is used to hook everything into Webpack, you don't need to change it).
* The `src` folder contains your Rust code.
* The `static` folder contains any files that you want copied as-is into the final build. It contains an `index.html` file which loads the `index.js` file.
* The `tests` folder contains your Rust unit tests.

View File

@ -0,0 +1 @@
import("../pkg/index.js").catch(console.error);

View File

@ -0,0 +1,18 @@
{
"author": "You <you@example.com>",
"name": "rust-webpack-template",
"version": "0.1.0",
"scripts": {
"build": "rimraf dist pkg && webpack",
"start": "rimraf dist pkg && webpack-dev-server --open -d",
"test": "cargo test && wasm-pack test --headless"
},
"devDependencies": {
"@wasm-tool/wasm-pack-plugin": "^1.1.0",
"copy-webpack-plugin": "^5.0.3",
"webpack": "^4.42.0",
"webpack-cli": "^3.3.3",
"webpack-dev-server": "^3.7.1",
"rimraf": "^3.0.0"
}
}

View File

@ -0,0 +1,27 @@
use wasm_bindgen::prelude::*;
use web_sys::console;
// When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
// allocator.
//
// If you don't want to use `wee_alloc`, you can safely delete this.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
// This is like the `main` function, except for JavaScript.
#[wasm_bindgen(start)]
pub fn main_js() -> Result<(), JsValue> {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
// Your code goes here!
console::log_1(&JsValue::from_str("Hello world!"));
Ok(())
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Rust + Webpack project!</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>

View File

@ -0,0 +1,35 @@
use wasm_bindgen_test::{wasm_bindgen_test_configure, wasm_bindgen_test};
use futures::prelude::*;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
wasm_bindgen_test_configure!(run_in_browser);
// This runs a unit test in native Rust, so it can only use Rust APIs.
#[test]
fn rust_test() {
assert_eq!(1, 1);
}
// This runs a unit test in the browser, so it can use browser APIs.
#[wasm_bindgen_test]
fn web_test() {
assert_eq!(1, 1);
}
// This runs a unit test in the browser, and in addition it supports asynchronous Future APIs.
#[wasm_bindgen_test(async)]
fn async_test() -> impl Future<Item = (), Error = JsValue> {
// Creates a JavaScript Promise which will asynchronously resolve with the value 42.
let promise = js_sys::Promise::resolve(&JsValue::from(42));
// Converts that Promise into a Future.
// The unit test will wait for the Future to resolve.
JsFuture::from(promise)
.map(|x| {
assert_eq!(x, 42);
})
}

View File

@ -0,0 +1,28 @@
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
const dist = path.resolve(__dirname, "dist");
module.exports = {
mode: "production",
entry: {
index: "./js/index.js"
},
output: {
path: dist,
filename: "[name].js"
},
devServer: {
contentBase: dist,
},
plugins: [
new CopyPlugin([
path.resolve(__dirname, "static")
]),
new WasmPackPlugin({
crateDirectory: __dirname,
}),
]
};

28420
yarn.lock

File diff suppressed because it is too large Load Diff