Documentation
¶
Overview ¶
Package dev provides the development server and hot reload functionality.
This package implements:
- File watching for Go, CSS, and asset changes
- Incremental Go compilation
- WebSocket-based browser refresh
- Tailwind CSS compilation
- Error overlay in browser
Architecture ¶
The development server consists of several components:
- Watcher: Monitors file system for changes
- Compiler: Builds Go code incrementally
- Server: Serves the application and static files
- ReloadServer: Notifies browsers of changes via WebSocket
- TailwindRunner: Compiles Tailwind CSS on change
Usage ¶
srv := dev.NewServer(config, dev.Options{
Port: 3000,
OpenBrowser: true,
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := srv.Start(ctx); err != nil {
log.Fatal(err)
}
Configuration ¶
Hot reload can be disabled via vango.json (dev.hotReload=false). Watch paths are derived from project config (routes, components, static, etc.) plus any entries in dev.watch.
Hot Reload Protocol ¶
The browser connects to /_vango/reload via WebSocket. Messages are JSON-encoded:
{"type": "reload"} // Triggers full page reload
{"type": "css"} // Triggers CSS-only reload
{"type": "error", "error": "..."} // Shows error overlay
{"type": "clear"} // Clears error overlay
Index ¶
- Constants
- Variables
- func CollectWatchPaths(cfg *config.Config) []string
- func GetModulePath(projectDir string) (string, error)
- func IsRecoverableError(buildOutput string) bool
- type BuildError
- type BuildResult
- type Change
- type ChangeType
- type Compiler
- type CompilerConfig
- type ErrorRecovery
- type RecoveryResult
- type ReloadMessage
- type ReloadMessageType
- type ReloadServer
- func (r *ReloadServer) ClearError()
- func (r *ReloadServer) ClientCount() int
- func (r *ReloadServer) Close()
- func (r *ReloadServer) HandleWebSocket(w http.ResponseWriter, req *http.Request)
- func (r *ReloadServer) NotifyCSS(file string)
- func (r *ReloadServer) NotifyError(errMsg string)
- func (r *ReloadServer) NotifyReload()
- type Server
- type ServerOptions
- type Watcher
- type WatcherConfig
Constants ¶
const DevClientScript = `` /* 3617-byte string literal not displayed */
DevClientScript returns the JavaScript for hot reload. This is injected into the page in development mode.
Variables ¶
var DefaultIgnore = []string{
"*_test.go",
".git",
"node_modules",
"dist",
"tmp",
".vango",
"*.tmp",
"*.swp",
"*~",
}
DefaultIgnore contains default patterns to ignore.
Functions ¶
func CollectWatchPaths ¶
CollectWatchPaths returns a normalized list of watch paths for the project.
func GetModulePath ¶
GetModulePath reads the module path from go.mod.
func IsRecoverableError ¶
IsRecoverableError checks if a build error might be recoverable.
Types ¶
type BuildError ¶
BuildError represents a compilation error with context.
func (*BuildError) Error ¶
func (e *BuildError) Error() string
func (*BuildError) Format ¶
func (e *BuildError) Format() string
Format returns a formatted error suitable for display.
type BuildResult ¶
type BuildResult struct {
// Success indicates if the build succeeded.
Success bool
// Duration is how long the build took.
Duration time.Duration
// Output is the compiler output.
Output string
// Error is the build error, if any.
Error error
}
BuildResult contains the result of a build.
type Change ¶
type Change struct {
Path string
Type ChangeType
}
Change represents a detected file change.
type ChangeType ¶
type ChangeType int
ChangeType represents the type of file change.
const ( ChangeGo ChangeType = iota ChangeCSS ChangeAsset ChangeTemplate )
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler handles Go compilation and process management.
func NewCompiler ¶
func NewCompiler(config CompilerConfig) *Compiler
NewCompiler creates a new Go compiler.
func (*Compiler) BinaryPath ¶
BinaryPath returns the path to the compiled binary.
func (*Compiler) Build ¶
func (c *Compiler) Build(ctx context.Context) BuildResult
Build compiles the Go project.
type CompilerConfig ¶
type CompilerConfig struct {
// ProjectPath is the root directory of the project.
ProjectPath string
// BinaryPath is where to write the compiled binary.
BinaryPath string
// CachePath is where to store the Go build cache.
CachePath string
// Tags are build tags to pass to go build.
Tags []string
// LDFlags are linker flags to pass to go build.
LDFlags string
// Env are additional environment variables.
Env []string
}
CompilerConfig configures the Go compiler.
type ErrorRecovery ¶
type ErrorRecovery struct {
// contains filtered or unexported fields
}
ErrorRecovery handles automatic recovery from common build errors.
func NewErrorRecovery ¶
func NewErrorRecovery(projectDir, routesDir, routesGenPath, modulePath string) *ErrorRecovery
NewErrorRecovery creates a new error recovery handler.
func (*ErrorRecovery) AttemptRecovery ¶
func (r *ErrorRecovery) AttemptRecovery(buildOutput string) RecoveryResult
AttemptRecovery tries to automatically fix common build errors. Returns true if a fix was applied and the build should be retried.
type RecoveryResult ¶
type RecoveryResult struct {
// Recovered indicates if recovery was successful.
Recovered bool
// Action describes what was done.
Action string
// Details provides additional information.
Details string
}
RecoveryResult contains the result of an attempted recovery.
type ReloadMessage ¶
type ReloadMessage struct {
Type ReloadMessageType `json:"type"`
Error string `json:"error,omitempty"`
File string `json:"file,omitempty"`
}
ReloadMessage is sent to browsers via WebSocket.
type ReloadMessageType ¶
type ReloadMessageType string
ReloadMessageType represents the type of reload message.
const ( ReloadTypeFull ReloadMessageType = "reload" ReloadTypeCSS ReloadMessageType = "css" ReloadTypeError ReloadMessageType = "error" ReloadTypeClear ReloadMessageType = "clear" )
type ReloadServer ¶
type ReloadServer struct {
// contains filtered or unexported fields
}
ReloadServer manages WebSocket connections for hot reload.
func NewReloadServer ¶
func NewReloadServer() *ReloadServer
NewReloadServer creates a new reload server.
func (*ReloadServer) ClearError ¶
func (r *ReloadServer) ClearError()
ClearError clears the error overlay on all clients.
func (*ReloadServer) ClientCount ¶
func (r *ReloadServer) ClientCount() int
ClientCount returns the number of connected clients.
func (*ReloadServer) HandleWebSocket ¶
func (r *ReloadServer) HandleWebSocket(w http.ResponseWriter, req *http.Request)
HandleWebSocket handles WebSocket upgrade and connection.
func (*ReloadServer) NotifyCSS ¶
func (r *ReloadServer) NotifyCSS(file string)
NotifyCSS sends a CSS-only reload message to all clients.
func (*ReloadServer) NotifyError ¶
func (r *ReloadServer) NotifyError(errMsg string)
NotifyError sends an error message to all clients.
func (*ReloadServer) NotifyReload ¶
func (r *ReloadServer) NotifyReload()
NotifyReload sends a full page reload message to all clients.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the development server.
func NewServer ¶
func NewServer(options ServerOptions) *Server
NewServer creates a new development server.
type ServerOptions ¶
type ServerOptions struct {
// Config is the project configuration.
Config *config.Config
// Verbose enables verbose logging.
Verbose bool
// OnBuildStart is called when a build starts.
OnBuildStart func()
// OnBuildComplete is called when a build completes.
OnBuildComplete func(result BuildResult)
// OnReload is called when browsers are reloaded.
OnReload func(clients int)
}
ServerOptions configures the development server.
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher monitors files for changes.
func NewWatcher ¶
func NewWatcher(config WatcherConfig) *Watcher
NewWatcher creates a new file watcher.