Files
Furqan-14andClaude Opus 4.8 ddbafbcd43 fix(build): build_wasm.ps1 builds the real wasm-pdfium engine + deploys pdfium-engine.{mjs,wasm}
build_wasm.ps1 built the `wasm` STUB preset (PDFENGINE_WITH_PDFIUM=OFF, reflow #ifdef'd out)
and deployed it to the UNUSED pdfengine.{mjs,wasm}. The frontend actually loads
pdfium-engine.{mjs,wasm} (the `wasm-pdfium` preset). So "rebuild the WASM" silently shipped a
reflow-less stub to the wrong file — engine changes never reached the browser.

- Build the `wasm-pdfium` preset (PDFium ON) into pdfeng-build/wasm-pdfium (outside OneDrive).
- Deploy bin/pdfengine.{mjs,wasm} -> public/pdfium-engine.{mjs,wasm} (the rename the frontend expects).
- Set EMSDK_QUIET before sourcing emsdk_env.ps1 (its stderr tripped $ErrorActionPreference='Stop').
- Run a new pdfium-engine.smoke.mjs (buildInfo + live-preview exports) instead of pdfengine.test.mjs,
  which targets the stub facade (mock render / engineHasSkia / getDocumentFonts) and can't pass here.
- Relax EAP around the smoke node call so a real failure reports cleanly, not as NativeCommandError.
- pdfengine.test.mjs: de-brittle the buildInfo assertion (was pinned to a stale exact phrase).

Verified: scripts/build_wasm.ps1 runs green end-to-end and deploys pdfium-engine.wasm (md5 53d1ec9f).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 19:24:45 +05:30

110 lines
4.7 KiB
PowerShell

#requires -Version 5.1
# Build C++ Engine to WebAssembly (WASM) and deploy to Frontend public folder.
$ErrorActionPreference = 'Stop'
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectRoot = (Resolve-Path (Join-Path $ScriptDir "..")).Path
# --- Resolve EMSDK ---
$emsdk = $env:EMSDK
if (-not $emsdk) {
$candidates = @(
"C:\Users\$env:USERNAME\emsdk",
"C:\emsdk",
"C:\src\emsdk",
"C:\Users\furqa\emsdk"
)
foreach ($cand in $candidates) {
if (Test-Path $cand) {
$emsdk = $cand
break
}
}
if (-not $emsdk) {
Write-Error "EMSDK environment variable was not set and could not be found at standard locations. Please set the EMSDK environment variable."
exit 1
}
$env:EMSDK = $emsdk
Write-Host "EMSDK defaulted to: $emsdk" -ForegroundColor Yellow
}
# Load EMSDK environment variables in PowerShell context
$envScript = Join-Path $emsdk "emsdk_env.ps1"
if (-not (Test-Path $envScript)) {
Write-Error "Could not find emsdk_env.ps1 at $emsdk"
exit 1
}
Write-Host "Loading Emscripten environment variables..." -ForegroundColor Cyan
# emsdk_env.ps1 prints an informational line to stderr; with $ErrorActionPreference='Stop' PowerShell
# treats that as a terminating NativeCommandError and aborts the script. EMSDK_QUIET silences it.
$env:EMSDK_QUIET = "1"
. $envScript
# Resolve build directory.
# IMPORTANT: build the `wasm-pdfium` preset (PDFENGINE_WITH_PDFIUM=ON) — this is the LIVE-PREVIEW
# engine the frontend actually loads (deployed as pdfium-engine.{mjs,wasm}). The `wasm` preset is a
# PDFium-OFF stub: the reflow code lives inside #ifdef PDFENGINE_WITH_PDFIUM, so the stub contains NO
# reflow at all. Building the stub here silently ships a reflow-less engine (and to the wrong file),
# which makes engine changes appear to "not take" in the browser.
$BuildDir = Join-Path $ProjectRoot "out\build\wasm-pdfium"
if ($IsWindows -or $env:OS -eq "Windows_NT") {
# Keep the build dir OUTSIDE OneDrive / paths with spaces (Emscripten/Ninja break otherwise).
$BuildDir = "C:\Users\$env:USERNAME\pdfeng-build\wasm-pdfium"
if (-not (Test-Path $BuildDir)) {
New-Item -ItemType Directory -Path $BuildDir -Force | Out-Null
}
}
$WasmBinDir = Join-Path $BuildDir "bin"
# Configure WASM preset
Write-Host "Configuring CMake wasm-pdfium preset in $BuildDir..." -ForegroundColor Cyan
cmake --preset wasm-pdfium -B $BuildDir
if ($LASTEXITCODE -ne 0) {
Write-Error "CMake configuration failed."
exit $LASTEXITCODE
}
# Build WASM preset
Write-Host "Building WASM targets..." -ForegroundColor Cyan
cmake --build $BuildDir
if ($LASTEXITCODE -ne 0) {
Write-Error "WASM build failed."
exit $LASTEXITCODE
}
# Run WASM smoke tests. Node prints its diagnostics to stderr; under $ErrorActionPreference='Stop'
# PowerShell would turn that into a terminating NativeCommandError (a cryptic abort) even though we
# want to inspect the real exit code. Relax EAP just around the call and branch on $LASTEXITCODE.
Write-Host "Running WASM smoke tests..." -ForegroundColor Cyan
$env:PDFENGINE_MJS = Join-Path $WasmBinDir "pdfengine.mjs"
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
# Validate the REAL pdfium engine (buildInfo + live-preview exports). pdfengine.test.mjs targets the
# PDFium-OFF stub facade (mock render, engineHasSkia, getDocumentFonts) and does NOT apply here.
node wasm/pdfium-engine.smoke.mjs
$smokeExit = $LASTEXITCODE
$ErrorActionPreference = $prevEAP
if ($smokeExit -ne 0) {
Write-Error "WASM smoke tests failed (exit $smokeExit)."
exit $smokeExit
}
# Copy built targets to frontend public folder
$FrontendPublic = Join-Path $ProjectRoot "frontend\public"
if (-not (Test-Path $FrontendPublic)) {
Write-Warning "Frontend public folder not found at $FrontendPublic. Skipping copy."
} else {
# The cmake target's OUTPUT_NAME is "pdfengine"; the frontend loads it as "pdfium-engine".
# Deploy with that rename so the live preview picks up this build.
Write-Host "Deploying WASM engine -> pdfium-engine.{mjs,wasm} in frontend public..." -ForegroundColor Cyan
Copy-Item -Path (Join-Path $WasmBinDir "pdfengine.mjs") -Destination (Join-Path $FrontendPublic "pdfium-engine.mjs") -Force
Copy-Item -Path (Join-Path $WasmBinDir "pdfengine.wasm") -Destination (Join-Path $FrontendPublic "pdfium-engine.wasm") -Force
Write-Host "Successfully deployed pdfium-engine.{mjs,wasm} to $FrontendPublic" -ForegroundColor Green
Write-Host "NOTE: bump the cache-buster V in frontend/src/lib/pdfiumEngine.ts so browsers refetch." -ForegroundColor Yellow
}
Write-Host "Success! WASM (wasm-pdfium) built and deployed as pdfium-engine.{mjs,wasm}." -ForegroundColor Green