-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-wasm.ps1
More file actions
143 lines (118 loc) · 3.62 KB
/
build-wasm.ps1
File metadata and controls
143 lines (118 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# ViewStage WASM Build Script
# Automatically compiles the WASM module and copies it to the src directory
param(
[string]$Mode = "release",
[switch]$Help
)
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$WasmDir = Join-Path $ScriptDir "wasm-viewstage"
$OutputDir = Join-Path $ScriptDir "src\wasm"
function Write-Status {
param([string]$Message)
Write-Host "[ViewStage] $Message" -ForegroundColor Cyan
}
function Write-Success {
param([string]$Message)
Write-Host "[ViewStage] $Message" -ForegroundColor Green
}
function Write-Error {
param([string]$Message)
Write-Host "[ViewStage] ERROR: $Message" -ForegroundColor Red
}
if ($Help) {
Write-Host @"
ViewStage WASM Build Script
Usage: .\build-wasm.ps1 [-Mode <mode>] [-Help]
Options:
-Mode Build mode: 'release' (default) or 'dev'
-Help Show this help message
Examples:
.\build-wasm.ps1 # Build in release mode
.\build-wasm.ps1 -Mode dev # Build in development mode
"@
exit 0
}
Write-Status "Starting WASM build process..."
Write-Status "Build mode: $Mode"
# Check if wasm-pack is installed
Write-Status "Checking for wasm-pack..."
$wasmPack = Get-Command wasm-pack -ErrorAction SilentlyContinue
if (-not $wasmPack) {
Write-Error "wasm-pack is not installed!"
Write-Host ""
Write-Host "Please install wasm-pack using one of the following methods:"
Write-Host " 1. cargo install wasm-pack"
Write-Host " 2. Invoke-WebRequest -Uri 'https://rustwasm.github.io/wasm-pack/installer/init.ps1' -OutFile 'install.ps1'; .\install.ps1"
exit 1
}
Write-Success "wasm-pack found: $($wasmPack.Source)"
# Check if the wasm-viewstage directory exists
if (-not (Test-Path $WasmDir)) {
Write-Error "wasm-viewstage directory not found: $WasmDir"
exit 1
}
# Create output directory if it doesn't exist
if (-not (Test-Path $OutputDir)) {
Write-Status "Creating output directory: $OutputDir"
New-Item -ItemType Directory -Path $OutputDir -Force | Out-Null
}
# Build WASM
Write-Status "Building WASM module..."
$buildTarget = if ($Mode -eq "dev") { "dev" } else { "release" }
$buildArgs = @(
"build",
"--target", "web",
"--$buildTarget",
"--out-dir", $OutputDir
)
Push-Location $WasmDir
try {
$startTime = Get-Date
& wasm-pack @buildArgs
if ($LASTEXITCODE -ne 0) {
throw "wasm-pack build failed with exit code $LASTEXITCODE"
}
$endTime = Get-Date
$duration = $endTime - $startTime
Write-Success "WASM build completed in $($duration.TotalSeconds.ToString('F2')) seconds"
}
catch {
Write-Error "Build failed: $_"
Pop-Location
exit 1
}
finally {
Pop-Location
}
# Verify output files
$expectedFiles = @(
"wasm_viewstage.js",
"wasm_viewstage_bg.wasm"
)
$missingFiles = @()
foreach ($file in $expectedFiles) {
$filePath = Join-Path $OutputDir $file
if (-not (Test-Path $filePath)) {
$missingFiles += $file
}
}
if ($missingFiles.Count -gt 0) {
Write-Error "Missing output files: $($missingFiles -join ', ')"
exit 1
}
# Show output file sizes
Write-Status "Output files:"
foreach ($file in $expectedFiles) {
$filePath = Join-Path $OutputDir $file
$fileInfo = Get-Item $filePath
$sizeKB = [math]::Round($fileInfo.Length / 1KB, 2)
Write-Host " - $file ($sizeKB KB)"
}
# Generate TypeScript definitions if needed (optional)
$tsDefPath = Join-Path $OutputDir "wasm_viewstage.d.ts"
if (Test-Path $tsDefPath) {
Write-Status "TypeScript definitions generated"
}
Write-Success "WASM build completed successfully!"
Write-Status "Output directory: $OutputDir"