/
wasm-build.go
73 lines (55 loc) · 2.15 KB
/
wasm-build.go
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
package compiler
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/cdvelop/ldflags"
)
func (c Compiler) BuildWASM(event_name string) (err string) {
const this = "BuildWASM error "
if c.wasm_build {
if event_name != "" {
fmt.Println("Compilando WASM..." + event_name)
}
// fmt.Println("c.WORK_FOLDER:", c.WORK_FOLDER)
// Ejecutar go mod tidy en el directorio del proyecto
tidyCmd := exec.Command("go", "mod", "tidy")
tidyCmd.Dir = c.project_dir
tidyOutput, tidyErr := tidyCmd.CombinedOutput()
if tidyErr != nil {
return this + "al ejecutar 'go mod tidy': " + tidyErr.Error() + " " + string(tidyOutput)
}
var cmd *exec.Cmd
// log.Println("*** c.test_wasm_folder: ", c.test_wasm_folder)
input_go_file := filepath.Join(c.WORK_FOLDER, c.test_wasm_folder, c.wasm_file_name)
out_wasm_file := filepath.Join(c.STATIC_FOLDER, "/app"+c.test_suffix+".wasm")
// delete file anterior
os.Remove(out_wasm_file)
flags, err := ldflags.Add(c.GetTwoPublicKeysWasmClientAndGoServer())
if err != "" {
return this + err
}
// log.Println("*** input_go_file: ", input_go_file)
// Ajustamos los parámetros de compilación según la configuración
if c.with_tinyGo {
// fmt.Println("*** COMPILACIÓN WASM TINYGO ***")
cmd = exec.Command("tinygo", "build", "-o", out_wasm_file, "-target", "wasm", "--no-debug", "-ldflags", flags, input_go_file)
} else {
// compilación normal...
cmd = exec.Command("go", "build", "-o", out_wasm_file, "-tags", "dev", "-ldflags", flags, "-v", input_go_file)
// cmd = exec.Command("go", "build", "-o", out_wasm_file, "-tags", "dev", "-ldflags", "-s -w", "-v", input_go_file)
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
}
output, er := cmd.CombinedOutput()
if er != nil {
return "error al compilar a WebAssembly error: " + er.Error() + " string(output):" + string(output)
}
// Verificamos si el archivo wasm se creó correctamente
if _, er := os.Stat(out_wasm_file); er != nil {
return "error el archivo WebAssembly no se creó correctamente: " + er.Error()
}
// fmt.Printf("WebAssembly compilado correctamente y guardado en %s\n", out_wasm_file)
}
return ""
}