|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Quick sanity check for consumers of @lit-protocol/constants and contracts-sdk. |
| 4 | +# We spin up two tiny entry points, bundle them with esbuild, and dump which |
| 5 | +# @lit-protocol/contracts artifacts actually end up in the output. That makes it |
| 6 | +# easy to see whether only the intended network blobs are being shipped. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | +ROOT_DIR="$SCRIPT_DIR" |
| 12 | +BUNDLE_DIR="$ROOT_DIR/tmp/bundle-test" |
| 13 | +ESBUILD="$ROOT_DIR/node_modules/.bin/esbuild" |
| 14 | + |
| 15 | +if [[ ! -x "$ESBUILD" ]]; then |
| 16 | + echo "esbuild not found at $ESBUILD; run yarn install first." >&2 |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +mkdir -p "$BUNDLE_DIR" |
| 21 | + |
| 22 | +# Modules that are Node-only; when bundling for browsers we treat them as |
| 23 | +# externals so esbuild doesn't error on builtins like `crypto`. |
| 24 | +BROWSER_EXTERNALS=( |
| 25 | + assert buffer child_process crypto fs http https module net os path stream |
| 26 | + timers tls tty url util zlib |
| 27 | +) |
| 28 | +BROWSER_EXTERNAL_ARGS=() |
| 29 | +for mod in "${BROWSER_EXTERNALS[@]}"; do |
| 30 | + BROWSER_EXTERNAL_ARGS+=(--external:"$mod") |
| 31 | +done |
| 32 | + |
| 33 | +# Generate the constants entry: loads NETWORK_CONTEXT_BY_NETWORK so the mapper |
| 34 | +# import path is exercised the same way a consuming app would. |
| 35 | +cat >"$BUNDLE_DIR/constants-entry.js" <<'JS' |
| 36 | +const { NETWORK_CONTEXT_BY_NETWORK } = require('@lit-protocol/constants'); |
| 37 | +
|
| 38 | +if (!NETWORK_CONTEXT_BY_NETWORK?.datil) { |
| 39 | + throw new Error('datil context missing'); |
| 40 | +} |
| 41 | +
|
| 42 | +console.log('datil contracts count:', NETWORK_CONTEXT_BY_NETWORK.datil.data.length); |
| 43 | +JS |
| 44 | + |
| 45 | +# Generate the contracts SDK entry: touches LitContracts to mimic a typical SDK |
| 46 | +# consumer without pulling in unrelated code. |
| 47 | +cat >"$BUNDLE_DIR/contracts-sdk-entry.js" <<'JS' |
| 48 | +const { LitContracts } = require('@lit-protocol/contracts-sdk'); |
| 49 | +
|
| 50 | +if (typeof LitContracts !== 'function') { |
| 51 | + throw new Error('LitContracts constructor not found'); |
| 52 | +} |
| 53 | +
|
| 54 | +console.log('LitContracts ready'); |
| 55 | +JS |
| 56 | + |
| 57 | +# Bundle both entries for browser + node targets so we catch regressions in |
| 58 | +# either environment. |
| 59 | +echo "⭐️ [Browser] Bundling @lit-protocol/constants mapper..." |
| 60 | +"$ESBUILD" "$BUNDLE_DIR/constants-entry.js" \ |
| 61 | + --bundle \ |
| 62 | + --platform=browser \ |
| 63 | + --format=esm \ |
| 64 | + --outfile="$BUNDLE_DIR/constants-browser-bundle.js" \ |
| 65 | + --metafile="$BUNDLE_DIR/constants-browser-meta.json" \ |
| 66 | + "${BROWSER_EXTERNAL_ARGS[@]}" \ |
| 67 | + --log-level=info >/dev/null |
| 68 | + |
| 69 | +echo "⭐️ [CJS] Bundling @lit-protocol/constants mapper..." |
| 70 | +"$ESBUILD" "$BUNDLE_DIR/constants-entry.js" \ |
| 71 | + --bundle \ |
| 72 | + --platform=node \ |
| 73 | + --format=cjs \ |
| 74 | + --outfile="$BUNDLE_DIR/constants-node-bundle.cjs" \ |
| 75 | + --metafile="$BUNDLE_DIR/constants-node-meta.json" \ |
| 76 | + --log-level=info >/dev/null |
| 77 | + |
| 78 | +echo "⭐️ [Browser] Bundling @lit-protocol/contracts-sdk..." |
| 79 | +"$ESBUILD" "$BUNDLE_DIR/contracts-sdk-entry.js" \ |
| 80 | + --bundle \ |
| 81 | + --platform=browser \ |
| 82 | + --format=esm \ |
| 83 | + --outfile="$BUNDLE_DIR/contracts-sdk-browser-bundle.js" \ |
| 84 | + --metafile="$BUNDLE_DIR/contracts-sdk-browser-meta.json" \ |
| 85 | + "${BROWSER_EXTERNAL_ARGS[@]}" \ |
| 86 | + --log-level=info >/dev/null |
| 87 | + |
| 88 | +echo "⭐️ [CJS] Bundling @lit-protocol/contracts-sdk..." |
| 89 | +"$ESBUILD" "$BUNDLE_DIR/contracts-sdk-entry.js" \ |
| 90 | + --bundle \ |
| 91 | + --platform=node \ |
| 92 | + --format=cjs \ |
| 93 | + --outfile="$BUNDLE_DIR/contracts-sdk-node-bundle.cjs" \ |
| 94 | + --metafile="$BUNDLE_DIR/contracts-sdk-node-meta.json" \ |
| 95 | + --log-level=info >/dev/null |
| 96 | + |
| 97 | +# Pretty-print the relevant portion of esbuild's metafile so it's obvious which |
| 98 | +# contract blobs made it into the bundle and their combined size. |
| 99 | +summarise_meta () { |
| 100 | + local label="$1" |
| 101 | + local meta_path="$2" |
| 102 | + local indent="${3:-}" |
| 103 | + echo "" |
| 104 | + echo "${indent}${label}" |
| 105 | + echo "${indent} (Every line below lists a bundled @lit-protocol/contracts file and its raw size)" |
| 106 | + META_PATH="$meta_path" INDENT="$indent" node - <<'NODE' |
| 107 | +const path = require('path'); |
| 108 | +const metaPath = process.env.META_PATH; |
| 109 | +const meta = require(metaPath); |
| 110 | +const indent = process.env.INDENT || ''; |
| 111 | +const entries = Object.entries(meta.inputs) |
| 112 | + .filter(([p]) => p.includes(path.join('@lit-protocol', 'contracts'))); |
| 113 | +
|
| 114 | +if (entries.length === 0) { |
| 115 | + console.log(`${indent} <no @lit-protocol/contracts files detected>`); |
| 116 | + process.exit(0); |
| 117 | +} |
| 118 | +
|
| 119 | +let total = 0; |
| 120 | +for (const [file, info] of entries) { |
| 121 | + total += info.bytes; |
| 122 | + console.log(`${indent} ${file} -> ${(info.bytes / 1024).toFixed(2)} KiB`); |
| 123 | +} |
| 124 | +
|
| 125 | +console.log(`${indent} Total -> ${(total / 1024 / 1024).toFixed(3)} MiB`); |
| 126 | +NODE |
| 127 | + echo "${indent} (Total reflects the sum of those files before compression/minification)" |
| 128 | +} |
| 129 | + |
| 130 | +summarise_package () { |
| 131 | + local package_label="$1" |
| 132 | + local browser_meta="$2" |
| 133 | + local node_meta="$3" |
| 134 | + |
| 135 | + echo "" |
| 136 | + echo "⭐️ ${package_label} bundles:" |
| 137 | + summarise_meta "Browser includes:" "$browser_meta" " " |
| 138 | + summarise_meta "Node includes:" "$node_meta" " " |
| 139 | +} |
| 140 | + |
| 141 | +summarise_package "@lit-protocol/constants" \ |
| 142 | + "$BUNDLE_DIR/constants-browser-meta.json" \ |
| 143 | + "$BUNDLE_DIR/constants-node-meta.json" |
| 144 | + |
| 145 | +summarise_package "@lit-protocol/contracts-sdk" \ |
| 146 | + "$BUNDLE_DIR/contracts-sdk-browser-meta.json" \ |
| 147 | + "$BUNDLE_DIR/contracts-sdk-node-meta.json" |
| 148 | + |
| 149 | +echo "" |
| 150 | +echo "Artifacts written to $BUNDLE_DIR" |
0 commit comments