#
.SYNOPSIS
Build the static site that the Hugging Face Static Space serves.
.DESCRIPTION
Pipeline:
app/ (clean upstream mirror)
+ overlay/ (the browser-side API and its wiring)
-> .build/out/
-> copied to the repository root
There is no compiler and no package manager here: upstream's front end is
plain HTML, CSS, and JavaScript. The build is a copy, one HTML edit that
inserts the overlay's '
if ($html -notlike "*$anchor*") {
throw @"
Could not find upstream's app.js script tag in static/index.html.
Expected: $anchor
The overlay has to load before app.js does. Update the anchor in
scripts/build-static.ps1 to match upstream's markup.
"@
}
$injected = (($overlayScripts | ForEach-Object { "" }) -join "`n ")
$html = $html.Replace($anchor, "$injected`n $anchor")
[System.IO.File]::WriteAllText($indexPath, $html, (New-Object System.Text.UTF8Encoding $false))
Write-Host " inserted $($overlayScripts.Count) overlay script tag(s) before app.js"
# --- Remove the previous build's output from the repository root ----------
if (Test-Path -LiteralPath $manifestPath) {
$stale = Get-Content -LiteralPath $manifestPath |
Where-Object { $_.Trim() -and -not $_.StartsWith('#') }
foreach ($entry in $stale) {
$name = $entry.Trim()
if ($protected -contains $name) {
Write-Warning "Manifest names a protected path ('$name'); refusing to delete it."
continue
}
# Reject anything that is not a plain top-level name.
if ($name -match '[\\/]' -or $name -eq '.' -or $name -eq '..') {
Write-Warning "Manifest entry '$name' is not a top-level name; skipping."
continue
}
$target = Join-Path $repoRoot $name
if (Test-Path -LiteralPath $target) {
Remove-Item -LiteralPath $target -Recurse -Force
}
}
}
# --- Copy the new output to the repository root ---------------------------
Write-Host 'Copying build output to the repository root' -ForegroundColor Cyan
$generated = @()
foreach ($item in Get-ChildItem -LiteralPath $outDir -Force) {
if ($protected -contains $item.Name) {
throw "Build output contains '$($item.Name)', which collides with a repository file. Resolve before deploying."
}
Copy-Item -LiteralPath $item.FullName -Destination $repoRoot -Recurse -Force
$generated += $item.Name
}
$manifestLines = @(
'# Generated by scripts/build-static.ps1 - do not edit.',
'# Top-level repository paths that are build output, deleted and rewritten',
'# on each build.'
) + ($generated | Sort-Object)
Set-Content -LiteralPath $manifestPath -Value $manifestLines -Encoding ascii
# --- Summary --------------------------------------------------------------
$outFiles = Get-ChildItem -LiteralPath $outDir -Recurse -File
$sizeKb = [math]::Round((($outFiles | Measure-Object -Property Length -Sum).Sum / 1KB), 1)
Write-Host ''
Write-Host "Static site built: $($outFiles.Count) files, $sizeKb KB" -ForegroundColor Green
Write-Host "Generated at repo root: $($generated -join ', ')"
Write-Host ''
Write-Host 'Preview it locally with:' -ForegroundColor Cyan
Write-Host " python -m http.server 8000 --directory `"$repoRoot`""
Write-Host ' then open http://localhost:8000'