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": { "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": { "gentype": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": [
@ -42,21 +57,22 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1660485612, "lastModified": 1660496378,
"narHash": "sha256-sSLW1KaB1adKTJn9+Ja3h3AaS7QCZyhUKiSUStcLg80=", "narHash": "sha256-sgAhmrC1iSnl5T2VPPiMpciH1aRw5c7PYEdXX6jd6Gk=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "6512b21eabb4d52e87ea2edcf31a288e67b2e4f8", "rev": "879121648fe522b38cc1cf75aef160a14a1f2e7b",
"type": "github" "type": "github"
}, },
"original": { "original": {
"id": "nixpkgs", "id": "nixpkgs",
"ref": "nixos-unstable", "ref": "nixos-22.05",
"type": "indirect" "type": "indirect"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils",
"gentype": "gentype", "gentype": "gentype",
"hercules-ci-effects": "hercules-ci-effects", "hercules-ci-effects": "hercules-ci-effects",
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"

547
flake.nix
View File

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