dev

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 24 Imported by: 0

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

View Source
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

View Source
var DefaultIgnore = []string{
	"*_test.go",
	".git",
	"node_modules",
	"dist",
	"tmp",
	".vango",
	"*.tmp",
	"*.swp",
	"*~",
}

DefaultIgnore contains default patterns to ignore.

Functions

func CollectWatchPaths

func CollectWatchPaths(cfg *config.Config) []string

CollectWatchPaths returns a normalized list of watch paths for the project.

func GetModulePath

func GetModulePath(projectDir string) (string, error)

GetModulePath reads the module path from go.mod.

func IsRecoverableError

func IsRecoverableError(buildOutput string) bool

IsRecoverableError checks if a build error might be recoverable.

Types

type BuildError

type BuildError struct {
	File    string
	Line    int
	Column  int
	Message string
	Output  string
}

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

func (c *Compiler) BinaryPath() string

BinaryPath returns the path to the compiled binary.

func (*Compiler) Build

func (c *Compiler) Build(ctx context.Context) BuildResult

Build compiles the Go project.

func (*Compiler) Clean

func (c *Compiler) Clean() error

Clean removes the build cache and binary.

func (*Compiler) IsRunning

func (c *Compiler) IsRunning() bool

IsRunning returns whether the process is running.

func (*Compiler) Restart

func (c *Compiler) Restart(ctx context.Context) error

Restart stops the current process and starts a new one.

func (*Compiler) Start

func (c *Compiler) Start(ctx context.Context) error

Start runs the compiled binary.

func (*Compiler) Stop

func (c *Compiler) Stop()

Stop stops the running process.

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) Close

func (r *ReloadServer) Close()

Close closes all client connections.

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.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the development server.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the 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.

func (*Watcher) IsRunning

func (w *Watcher) IsRunning() bool

IsRunning returns whether the watcher is running.

func (*Watcher) OnChange

func (w *Watcher) OnChange(fn func(Change))

OnChange sets the callback for file changes.

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context) error

Start begins watching for file changes.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop stops the watcher.

type WatcherConfig

type WatcherConfig struct {
	// Paths are the directories to watch.
	Paths []string

	// Ignore patterns to skip (globs).
	Ignore []string

	// Debounce is the delay before triggering on change.
	Debounce time.Duration
}

WatcherConfig configures the file watcher.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL