#!/usr/bin/env bash # Link the wasm32 artifacts: ONE shared embedpdf.wasm + three environment-pure # JS glues around it. # # embedpdf.browser.js ESM -sENVIRONMENT=web,worker (zero Node branches) # embedpdf.node.js ESM -sENVIRONMENT=node # embedpdf.node.cjs CJS -sENVIRONMENT=node # # Environment forks live in the package's export conditions, NOT in a # universal loader: a universal glue's unused Node branches import Node # builtins, which strict browser bundlers (Angular/esbuild) refuse to resolve # — Vite merely papers over it with stubs. Limiting -sENVIRONMENT removes the # excluded environments' support code at the source. # # Each glue is linked in its own temp dir as `embedpdf.{js,cjs}` so all of them # reference the SAME wasm basename (`embedpdf.wasm`, resolved relative to the # glue via import.meta.url / __dirname); the wasm outputs are verified # identical and published once. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" TARGET="${PDF_RUNTIME_TARGET:-wasm32}" LIB_DIR="$ROOT/build/libpdfium/$TARGET" OUT_DIR="$ROOT/npm/wasm32/lib" GEN_DIR="$ROOT/build/generated" mkdir -p "$OUT_DIR" TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT # args: [extra flags...] # # -O3 here is the link-time optimization level: only with it does Emscripten run # wasm-opt over the whole module (the library's own -O2 compile does not). It # changes no floating-point semantics: there is no fast-math, and scalar wasm has # no fused multiply-add. # # MAXIMUM_MEMORY=4GB lets the heap grow to all that wasm32 can address, where # Emscripten stops at 2 GB by default. One heap holds every document a worker has # open, so several large drawings no longer run out at 2 GB. Addresses at or above # 2 GB reach JS as negative i32s: Emscripten's glue reads them unsigned when built # with this flag, and so does the runtime (`toPtr` in src/wasm/wasm-runtime.ts). # Browsers that cap memory lower (iOS kills a tab at 1.3–1.8 GB) behave exactly as # they do with a 2 GB maximum. link() { local out="$1" environment="$2" shift 2 em++ "$LIB_DIR/lib/libembedpdf.a" \ -O3 \ -sENVIRONMENT="$environment" \ -sMODULARIZE=1 \ -sWASM=1 \ -sALLOW_MEMORY_GROWTH=1 \ -sMAXIMUM_MEMORY=4GB \ -sALLOW_TABLE_GROWTH=1 \ -sEXPORT_NAME=createPdfRuntimeWasm \ -sASSERTIONS=1 \ -sEXPORTED_RUNTIME_METHODS="$(cat "$GEN_DIR/exported-runtime-methods.txt")" \ -sEXPORTED_FUNCTIONS="$(cat "$GEN_DIR/exported-functions.txt")" \ -I"$LIB_DIR/include" \ -std=c++17 \ -Wall \ --no-entry \ "$@" \ -o "$out" } mkdir -p "$TMP_DIR/browser" "$TMP_DIR/node-esm" "$TMP_DIR/node-cjs" link "$TMP_DIR/browser/embedpdf.js" "web,worker" -sEXPORT_ES6=1 link "$TMP_DIR/node-esm/embedpdf.js" "node" -sEXPORT_ES6=1 link "$TMP_DIR/node-cjs/embedpdf.cjs" "node" # The wasm must be environment-independent (ENVIRONMENT only shapes the JS # glue). Verify, then publish one copy every glue's `embedpdf.wasm` reference # resolves to. cmp "$TMP_DIR/browser/embedpdf.wasm" "$TMP_DIR/node-esm/embedpdf.wasm" cmp "$TMP_DIR/browser/embedpdf.wasm" "$TMP_DIR/node-cjs/embedpdf.wasm" # pre-split universal glues + pre-rename pdfium.* outputs rm -f "$OUT_DIR/embedpdf.js" "$OUT_DIR/embedpdf.cjs" "$OUT_DIR"/pdfium.* cp "$TMP_DIR/browser/embedpdf.js" "$OUT_DIR/embedpdf.browser.js" cp "$TMP_DIR/node-esm/embedpdf.js" "$OUT_DIR/embedpdf.node.js" cp "$TMP_DIR/node-cjs/embedpdf.cjs" "$OUT_DIR/embedpdf.node.cjs" cp "$TMP_DIR/browser/embedpdf.wasm" "$OUT_DIR/embedpdf.wasm"