From ba8587f4a608857930710e8362c026525ea4bbe1 Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 20 Apr 2024 13:29:43 +0000 Subject: [PATCH] web: fix nix builds - 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 https://github.com/rustwasm/wasm-pack/pull/1224. - Build flux-next's wasm package in Nix. --- Cargo.lock | 24 +++++++-------- flake.nix | 36 +++++++++++++++------- flux-wasm/Cargo.toml | 2 +- web/default.nix | 72 +++++++++++++++++++++++++++----------------- 4 files changed, 83 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c3083f..977b41b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1651,9 +1651,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -1661,24 +1661,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.45", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1686,22 +1686,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.45", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" [[package]] name = "wayland-client" diff --git a/flake.nix b/flake.nix index 4678e59..18c3642 100644 --- a/flake.nix +++ b/flake.nix @@ -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]; @@ -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 = '' @@ -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 @@ -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"; diff --git a/flux-wasm/Cargo.toml b/flux-wasm/Cargo.toml index 6aea5bd..d6b2deb 100644 --- a/flux-wasm/Cargo.toml +++ b/flux-wasm/Cargo.toml @@ -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" diff --git a/web/default.nix b/web/default.nix index 6fdbb7f..6faf56b 100644 --- a/web/default.nix +++ b/web/default.nix @@ -3,6 +3,7 @@ lib, stdenv, flux-wasm, + flux-next-wasm, mkYarnPackage, }: let packageJSON = builtins.fromJSON (builtins.readFile ./package.json); @@ -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; @@ -68,7 +93,7 @@ in yarn nodePackages.pnpm elmPackages.elm - wasm-bindgen-cli-0_2_83 + wasm-bindgen-cli-0_2_91 binaryen ]; @@ -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 \