Skip to content

Commit

Permalink
web: fix nix builds
Browse files Browse the repository at this point in the history
- Bump wasm-bindgen to 0.2.91.
- Enable side effects for the wasm module to prevent webpack from
  removing code and breaking the build.
  See rustwasm/wasm-pack#1224.
- Build flux-next's wasm package in Nix.
  • Loading branch information
sandydoo committed Apr 20, 2024
1 parent ea27071 commit ba8587f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 25 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@
rustfmt = rustToolchain;
});

crateNameFromCargoToml = packageName: craneLib.crateNameFromCargoToml {cargoToml = ./${packageName}/Cargo.toml;};
crateNameFromCargoToml = packagePath:
craneLib.crateNameFromCargoToml {cargoToml = lib.path.append packagePath "Cargo.toml";};

crossCompileFor = {
hostPkgs,
targetTriple,
packageName,
packagePath,
}: let
rustToolchain = hostPkgs.pkgsBuildHost.rust-bin.stable.latest.default.override {
targets = [targetTriple];
Expand All @@ -95,9 +96,9 @@
inherit rustToolchain;

package = craneLib.buildPackage rec {
inherit (crateNameFromCargoToml packageName) pname version;
inherit (crateNameFromCargoToml packagePath) pname version;
src = ./.;
cargoExtraArgs = "-p ${packageName} --target ${targetTriple}";
cargoExtraArgs = "-p ${packagePath} --target ${targetTriple}";

# HOST_CC = "${pkgsCross.stdenv.cc.nativePrefix}cc";
preConfigure = ''
Expand Down Expand Up @@ -136,28 +137,41 @@
default = packages.flux-web;

flux = craneLib.buildPackage {
inherit (crateNameFromCargoToml "flux") pname version;
inherit (crateNameFromCargoToml ./flux) pname version;
src = ./.;
cargoExtraArgs = "-p flux";
doCheck = true;
};

flux-wasm = craneLib.buildPackage {
inherit (crateNameFromCargoToml "flux-wasm") pname version;
inherit (crateNameFromCargoToml ./flux-wasm) pname version;
src = ./.;

# By default, crane adds the `--workspace` flag to all commands.
# This is a bit of an issue because it builds all the packages in
# the workspace, even those that don’t support the wasm32 target (hi
# glutin).
cargoBuildCommand = "cargo build --release";
cargoCheckCommand = "cargo check --release";
cargoExtraArgs = "--package flux-wasm --target wasm32-unknown-unknown";
cargoExtraArgs = "--package flux-wasm";
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
doCheck = false; # This doesn’t disable the checks…
};

# TODO: move out to flux-next
flux-next-wasm = craneLib.buildPackage {
pname = "flux-next-wasm";
src = lib.cleanSourceWith {
src = ./flux-next;
filter = path: type:
(lib.hasSuffix "\.wgsl" path) ||
(craneLib.filterCargoSources path type);
};
cargoExtraArgs = "--package flux-wasm";
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
doCheck = false;
};

flux-web = pkgs.callPackage ./web/default.nix {
inherit (packages) flux-wasm;
inherit (packages) flux-wasm flux-next-wasm;
};

flux-desktop-wrapped = let
Expand Down Expand Up @@ -187,7 +201,7 @@
};

flux-desktop = craneLib.buildPackage {
inherit (crateNameFromCargoToml "flux-desktop") pname version;
inherit (crateNameFromCargoToml ./flux-desktop) pname version;
src = ./.;
release = true;
cargoExtraArgs = "-p flux-desktop";
Expand Down
2 changes: 1 addition & 1 deletion flux-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ js-sys = "0.3"
log = "0.4"
serde = { version = "1", features = ["derive"] }
gloo-utils = { version = "0.2", features = ["serde"] }
wasm-bindgen = { version = "=0.2.83" }
wasm-bindgen = { version = "=0.2.91" }

[dependencies.web-sys]
version = "0.3"
Expand Down
72 changes: 45 additions & 27 deletions web/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
lib,
stdenv,
flux-wasm,
flux-next-wasm,
mkYarnPackage,
}: let
packageJSON = builtins.fromJSON (builtins.readFile ./package.json);
Expand All @@ -18,36 +19,60 @@
publishBinsFor = ["webpack" "gh-pages"];
};

packageJson = ''
{
"files": [
"flux_wasm_bg.wasm",
"flux_wasm.js",
"flux_wasm_bg.js",
"flux_wasm.d.ts"
],
"module": "flux_wasm.js",
"types": "flux_wasm.d.ts",
"sideEffects": false
}
'';
# Prepare the wasm package the same way that wasm-pack does.
# TODO: maybe do this in the flux-wasm build
prepareWasm = wasmPkg:
let
packageJson = ''
{
"files": [
"index_bg.wasm",
"indexjs",
"index_bg.js",
"index.d.ts"
],
"module": "index.js",
"types": "index.d.ts",
"sideEffects": [
"./index.js",
"./snippets/*"
]
}
'';

in
pkgs.runCommand "flux-wasm" {} ''
mkdir $out
${wasm-bindgen-cli-0_2_91}/bin/wasm-bindgen \
--target bundler \
--out-name index \
--out-dir $out \
${wasmPkg}/lib/flux_wasm.wasm
mv $out/index_bg.wasm $out/index_bg_unoptimized.wasm
${pkgs.binaryen}/bin/wasm-opt -Os -o $out/index_bg.wasm $out/index_bg_unoptimized.wasm
echo '${packageJson}' > $out/package.json
'';

flux-wasm-packed = prepareWasm flux-wasm;
flux-next-wasm-packed = prepareWasm flux-next-wasm;

gitignoreSource = pkgs.nix-gitignore.gitignoreSource;

# Newer versions don't build flux-wasm properly. Last tested: 0.2.87.
wasm-bindgen-cli-0_2_83 = pkgs.wasm-bindgen-cli.overrideAttrs (drv: rec {
wasm-bindgen-cli-0_2_91 = pkgs.wasm-bindgen-cli.overrideAttrs (drv: rec {
name = "wasm-bindgen-cli-${version}";
version = "0.2.83";
version = "0.2.91";
src = pkgs.fetchCrate {
inherit (drv) pname;
inherit version;
hash = "sha256-+PWxeRL5MkIfJtfN3/DjaDlqRgBgWZMa6dBt1Q+lpd0=";
hash = "sha256-f/RK6s12ItqKJWJlA2WtOXtwX4Y0qa8bq/JHlLTAS3c=";
};

cargoDeps = drv.cargoDeps.overrideAttrs (lib.const {
inherit src;
name = "${drv.pname}-vendor.tar.gz";
outputHash = "sha256-snrU0D7YSDsF/NjckStk2wvqu1xNFXMJ9UxSAVK06pE=";
outputHash = "sha256-UcqGAeHfov0yABuxfxpHCFJKkJqaOtrDJY+LL0/sKSM=";
});

doCheck = false;
Expand All @@ -68,7 +93,7 @@ in
yarn
nodePackages.pnpm
elmPackages.elm
wasm-bindgen-cli-0_2_83
wasm-bindgen-cli-0_2_91
binaryen
];

Expand Down Expand Up @@ -96,15 +121,8 @@ in
installPhase = ''
mkdir -p $out
mkdir -p ./flux
wasm-bindgen \
--target bundler \
--out-dir ./flux \
${flux-wasm}/lib/flux_wasm.wasm
mv flux/flux_wasm_bg.wasm flux/flux_wasm_bg_unoptimized.wasm
wasm-opt -Os -o flux/flux_wasm_bg.wasm flux/flux_wasm_bg_unoptimized.wasm
echo '${packageJson}' > ./flux/package.json
ln -s ${flux-wasm-packed} ./flux
ln -s ${flux-next-wasm-packed} ./flux-next
webpack \
--mode production \
Expand Down

0 comments on commit ba8587f

Please sign in to comment.