Skip to content

Instantly share code, notes, and snippets.

@chinoto
Created December 14, 2020 16:51
Show Gist options
  • Save chinoto/abcad98f5406883f01d7973a2c99003e to your computer and use it in GitHub Desktop.
Save chinoto/abcad98f5406883f01d7973a2c99003e to your computer and use it in GitHub Desktop.
wasm-pack does a bit more than just build (npm stuff), but that's really all most people care about (myself included). The relevant pieces can be traced from the build command:
https://github.com/rustwasm/wasm-pack/blob/master/src/command/build.rs#L264
Important points are the calls to step_build_wasm, step_install_wasm_bindgen, step_run_wasm_bindgen, and step_run_wasm_opt, the other 4 steps are just fluff for making a node module/npm package (the package.json allows `import {my_func} from './pkg';` instead of './pkg/${crate_name}.js'...).
step_build_wasm is simply `cargo build --lib --release --target wasm32-unknown-unknown`
https://github.com/rustwasm/wasm-pack/blob/master/src/build/mod.rs#L76
step_install_wasm_bindgen is what justifies wasm-pack's existence because whatever version of wasm-bindgen your crate depends on requires that you use a corresponding version of wasm-bindgen-cli to generate the bindings correctly. It will either use an existing install, download a precompiled version, or run `cargo install --force wasm-bindgen-cli --version ${version} --root ${tmp_dir}`
download_prebuilt: https://github.com/rustwasm/wasm-pack/blob/master/src/install/mod.rs#L122
cargo_install: https://github.com/rustwasm/wasm-pack/blob/master/src/install/mod.rs#L260
step_run_wasm_bindgen: `${wasm-bindgen-cli_path} target/wasm32-unknown-unknown/release/${crate_name}.wasm --out-dir ${crate_root}/pkg --typescript --target bundler`
step_run_wasm_opt: fetch a prebuilt version of binaryen's wasm-opt, then for each file in ${crate_root}/pkg that ends with wasm {run `wasm-opt -o wasm-opt.wasm ${file} && mv wasm-opt.wasm ${file}`}
https://github.com/rustwasm/wasm-pack/blob/master/src/wasm_opt.rs#L33
@chinoto
Copy link
Author

chinoto commented Jul 30, 2022

@xy2iii Interesting, you're coming at the version issue from the other way. But I see that you're using Nix, which, from what little I've read, tries to deliver reproducible builds, so shouldn't the versions be known to match? Or perhaps this script is meant to be used by the maintainer when updating dependencies as a convenience, not by the end users?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment