Skip to content

Commit df4e3c7

Browse files
committed
Add nix output for web-grpc client bundled as JS
1 parent 6e6486d commit df4e3c7

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

flake.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
};
7575
inherit (nixpkgs) lib;
7676

77+
proto-js-bundle-drv = import ./nix/proto-to-js.nix { pkgs = nixpkgs; };
78+
7779
# We use cabalProject' to ensure we don't build the plan for
7880
# all systems.
7981
cabalProject = nixpkgs.haskell-nix.cabalProject' ({config, ...}: {
@@ -245,6 +247,9 @@
245247
# also provide hydraJobs through legacyPackages to allow building without system prefix:
246248
inherit hydraJobs;
247249
};
250+
packages = lib.optionalAttrs (system != "aarch64-darwin") {
251+
proto-js-bundle = proto-js-bundle-drv;
252+
};
248253
devShells = let
249254
# profiling shell
250255
profilingShell = p: {

nix/npm-deps/package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/npm-deps/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"google-protobuf": "^3.21.4",
4+
"grpc-web": "^1.5.0"
5+
}
6+
}

nix/proto-to-js.nix

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# proto-to-js.nix
2+
3+
{ pkgs ? import <nixpkgs> {} }:
4+
5+
let
6+
7+
# Node dependencies
8+
node-deps = pkgs.buildNpmPackage {
9+
version = "1.0.0";
10+
name = "proto-js-dependencies";
11+
src = ../nix/npm-deps;
12+
npmDepsHash = "sha256-b8x9xZ0dCu1cvILF0HPVVLfkCGHOWCcPUKyC2x1gQ+c=";
13+
dontNpmBuild = true;
14+
dontNpmInstall = true;
15+
installPhase = ''
16+
mkdir -p $out
17+
cp -r node_modules $out/
18+
'';
19+
};
20+
21+
cardano-rpc-src = ../cardano-rpc;
22+
23+
in pkgs.stdenv.mkDerivation {
24+
pname = "cardano-rpc-proto-js-bundle";
25+
version = "0.1.0";
26+
27+
src = cardano-rpc-src;
28+
29+
nativeBuildInputs = [
30+
pkgs.protobuf
31+
pkgs.protoc-gen-js
32+
pkgs.protoc-gen-grpc-web
33+
pkgs.nodePackages.browserify
34+
];
35+
36+
buildPhase = ''
37+
runHook preBuild
38+
39+
PROTO_INCLUDE_PATH=$src/proto
40+
GEN_JS_PATH=./generated-js
41+
BUNDLE_PATH=./bundled-js
42+
43+
mkdir -p $GEN_JS_PATH
44+
mkdir -p $BUNDLE_PATH
45+
46+
PROTO_FILE=$PROTO_INCLUDE_PATH/cardano/rpc/node.proto
47+
48+
echo "--- Compiling .proto file: $PROTO_FILE ---"
49+
50+
protoc \
51+
-I=$PROTO_INCLUDE_PATH \
52+
--js_out=import_style=commonjs,binary:$GEN_JS_PATH \
53+
--grpc-web_out=import_style=commonjs,mode=grpcwebtext:$GEN_JS_PATH \
54+
$PROTO_FILE
55+
56+
echo "--- Compilation finished. Generated files are in $GEN_JS_PATH ---"
57+
ls -R $GEN_JS_PATH
58+
59+
GENERATED_GRPC_FILE=$GEN_JS_PATH/cardano/rpc/node_grpc_web_pb.js
60+
61+
if [ ! -f "$GENERATED_GRPC_FILE" ]; then
62+
echo "Error: Protoc did not generate the expected gRPC-Web file!"
63+
exit 1
64+
fi
65+
66+
echo "--- Setting up node_modules for browserify ---"
67+
ln -s ${node-deps}/node_modules ./node_modules
68+
69+
echo "--- Bundling generated JS with browserify ---"
70+
71+
browserify --standalone grpc $GENERATED_GRPC_FILE > $BUNDLE_PATH/node_grpc_web_pb.js
72+
73+
echo "--- Bundling complete. Final file is in $BUNDLE_PATH ---"
74+
ls $BUNDLE_PATH
75+
76+
runHook postBuild
77+
'';
78+
79+
installPhase = ''
80+
runHook preInstall
81+
mkdir -p $out
82+
cp ./bundled-js/node_grpc_web_pb.js $out/
83+
runHook postInstall
84+
'';
85+
86+
dontConfigure = true;
87+
}
88+

0 commit comments

Comments
 (0)