cmd/tsconnect: run wasm-opt on the generated wasm file

Saves about 1.4MB from the generated wasm file. The Brotli size is
basically unchanged (it's actually slightly larger, by 40K), suggesting
that most of the size delta is due to not inlining and other changes
that were easily compressible.

However, it still seems worthwhile to have a smaller final binary, to
reduce parse time and increase likelihood that we fit in the browser's
disk cache. Actual performance appears to be unchanged.

Updates #5142

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2022-10-24 16:05:01 -07:00
committed by Mihai Parparita
parent 2a9ba28def
commit d60f7fe33f
3 changed files with 108 additions and 2 deletions

View File

@@ -216,10 +216,41 @@ func buildWasm(dev bool) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("Cannot build main.wasm: %w", err)
}
log.Printf("Built wasm in %v\n", time.Since(start))
log.Printf("Built wasm in %v\n", time.Since(start).Round(time.Millisecond))
if !dev {
err := runWasmOpt(outputPath)
if err != nil {
return nil, fmt.Errorf("Cannot run wasm-opt: %w", err)
}
}
return os.ReadFile(outputPath)
}
func runWasmOpt(path string) error {
start := time.Now()
stat, err := os.Stat(path)
if err != nil {
return fmt.Errorf("Cannot stat %v: %w", path, err)
}
startSize := stat.Size()
cmd := exec.Command("../../tool/wasm-opt", "-Oz", path, "-o", path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("Cannot run wasm-opt: %w", err)
}
stat, err = os.Stat(path)
if err != nil {
return fmt.Errorf("Cannot stat %v: %w", path, err)
}
endSize := stat.Size()
log.Printf("Ran wasm-opt in %v, size dropped by %dK\n", time.Since(start).Round(time.Millisecond), (startSize-endSize)/1024)
return nil
}
// installJSDeps installs the JavaScript dependencies specified by package.json
func installJSDeps() error {
log.Printf("Installing JS deps...\n")
@@ -256,7 +287,7 @@ func setupEsbuildTailwind(build esbuild.PluginBuild, dev bool) {
}
cmd := exec.Command(*yarnPath, yarnArgs...)
tailwindOutput, err := cmd.Output()
log.Printf("Ran tailwind in %v\n", time.Since(start))
log.Printf("Ran tailwind in %v\n", time.Since(start).Round(time.Millisecond))
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
log.Printf("Tailwind stderr: %s", exitErr.Stderr)