-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilderInit.go
More file actions
140 lines (126 loc) · 5.05 KB
/
builderInit.go
File metadata and controls
140 lines (126 loc) · 5.05 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
package client
import (
"path/filepath"
"strings"
"time"
"github.com/tinywasm/gobuild"
)
// builderWasmInit configures 3 builders for WASM compilation modes
func (w *WasmClient) builderWasmInit() {
sourceDir := filepath.Join(w.AppRootDir, w.Config.SourceDir())
outputDir := filepath.Join(w.AppRootDir, w.Config.OutputDir())
mainInputFileRelativePath := filepath.Join(sourceDir, w.MainInputFile)
// Base configuration shared by all builders
// The baseConfig is no longer used directly for builderSizeLarge, but its fields are replicated.
// The other builders (Medium, Small) will still use a derived config from this base.
baseConfig := gobuild.Config{
AppRootDir: w.AppRootDir, // CRITICAL: Set working directory for go.mod resolution
MainInputFileRelativePath: mainInputFileRelativePath,
OutName: w.OutputName, // Output will be {OutputName}.wasm
Extension: ".wasm",
OutFolderRelativePath: outputDir,
Logger: nil,
Timeout: 60 * time.Second, // 1 minute for all modes
Callback: w.Callback,
}
// Configure Coding builder (Go standard)
codingConfig := baseConfig
codingConfig.Command = "go"
codingConfig.Env = []string{"GOOS=js", "GOARCH=wasm"}
// Append custom env from Config if provided
if len(w.Config.Env) > 0 {
codingConfig.Env = append(codingConfig.Env, w.Config.Env...)
}
codingConfig.CompilingArguments = func() []string {
args := []string{"-tags", "dev"}
if w.CompilingArguments != nil {
args = append(args, w.CompilingArguments()...)
}
args = append(args, "-p", "1")
return args
}
w.builderSizeLarge = gobuild.New(&codingConfig)
// Configure Debug builder (TinyGo debug-friendly)
debugConfig := baseConfig
debugConfig.Command = "tinygo"
if len(w.Config.Env) > 0 {
debugConfig.Env = w.Config.Env
}
debugConfig.CompilingArguments = func() []string {
args := []string{"-target", "wasm", "-opt=1"} // Keep debug symbols
if w.CompilingArguments != nil {
args = append(args, w.CompilingArguments()...)
}
args = append(args, "-p", "1") // Add -p 1
return args
}
w.builderSizeMedium = gobuild.New(&debugConfig)
// Configure Production builder (TinyGo optimized)
prodConfig := baseConfig
prodConfig.Command = "tinygo"
if len(w.Config.Env) > 0 {
prodConfig.Env = w.Config.Env
}
prodConfig.CompilingArguments = func() []string {
args := []string{"-target", "wasm", "-opt=z", "-no-debug", "-panic=trap"}
if w.CompilingArguments != nil {
args = append(args, w.CompilingArguments()...)
}
args = append(args, "-p", "1")
return args
}
w.builderSizeSmall = gobuild.New(&prodConfig)
// Sync active builder with current mode (don't always reset to Large)
// This is important when builderWasmInit is called after loadMode() (e.g., from SetAppRootDir)
switch w.CurrentSizeMode {
case w.buildMediumSizeShortcut: // "M"
w.activeSizeBuilder = w.builderSizeMedium
case w.buildSmallSizeShortcut: // "S"
w.activeSizeBuilder = w.builderSizeSmall
default: // "L" or empty
w.activeSizeBuilder = w.builderSizeLarge
}
}
// UpdateCurrentBuilder sets the activeSizeBuilder based on mode and cancels ongoing operations
func (w *WasmClient) UpdateCurrentBuilder(mode string) {
// 1. Cancel any ongoing compilation
if w.activeSizeBuilder != nil {
w.activeSizeBuilder.Cancel()
}
// 2. Update current mode tracking
w.CurrentSizeMode = mode
// 3. Set activeSizeBuilder based on mode
switch mode {
case w.buildLargeSizeShortcut: // "L"
w.activeSizeBuilder = w.builderSizeLarge
case w.buildMediumSizeShortcut: // "M"
w.activeSizeBuilder = w.builderSizeMedium
case w.buildSmallSizeShortcut: // "S"
w.activeSizeBuilder = w.builderSizeSmall
default:
w.activeSizeBuilder = w.builderSizeLarge // fallback to coding mode
}
}
// OutputRelativePath returns the RELATIVE path to the final output file
// eg: "deploy/edgeworker/app.wasm" (relative to AppRootDir)
// This is used by file watchers to identify output files that should be ignored.
// The returned path always uses forward slashes (/) for consistency across platforms.
func (w *WasmClient) OutputRelativePath() string {
// FinalOutputPath() returns absolute path like: /tmp/test/deploy/edgeworker/app.wasm
// We need to extract the relative portion: deploy/edgeworker/app.wasm
fullPath := w.activeSizeBuilder.FinalOutputPath()
// Remove AppRootDir prefix to get relative path
if strings.HasPrefix(fullPath, w.AppRootDir) {
relPath := strings.TrimPrefix(fullPath, w.AppRootDir)
// Remove leading separator (/ or \)
relPath = strings.TrimPrefix(relPath, string(filepath.Separator))
relPath = strings.TrimPrefix(relPath, "/") // Handle Unix paths
relPath = strings.TrimPrefix(relPath, "\\") // Handle Windows paths
// Normalize to forward slashes for consistency (replace all backslashes)
return strings.ReplaceAll(relPath, "\\", "/")
}
// Fallback: construct from config values (which are already relative)
// Normalize to forward slashes for consistency
result := filepath.Join(w.Config.OutputDir(), w.OutputName+".wasm")
return strings.ReplaceAll(result, "\\", "/")
}