serve

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: GPL-3.0 Imports: 38 Imported by: 0

Documentation

Overview

Package serve implements the development server for custom element demos

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSSConfig

type CSSConfig struct {
	Enabled bool
	Include []string // Glob patterns to include
	Exclude []string // Glob patterns to exclude
}

CSSConfig holds CSS transform configuration

type Config

type Config struct {
	Port                 int
	Reload               bool
	Target               transform.Target      // Transform target (default: ES2022) - deprecated, use Transforms.TypeScript.Target
	Transforms           TransformConfig       // Transform configuration
	ImportMap            types.ImportMapConfig // Import map override configuration
	Demos                DemosConfig           // Demo rendering configuration
	ConfigFile           string                // Path to config file (for error reporting)
	WatchIgnore          []string              // Glob patterns to ignore in file watcher (e.g., ["_site/**", "dist/**"])
	SourceControlRootURL string                // Source control root URL for demo routing (e.g., "https://github.com/user/repo/tree/main/")
	FS                   platform.FileSystem   // Optional filesystem for testing (defaults to os package)
	URLRewrites          []config.URLRewrite   // URL rewrites for request path mapping (e.g., "/dist/:path*" -> "/src/{{.path}}")
	WebSocketManager     WebSocketManager      // Optional WebSocket manager for testing (created automatically if nil and Reload=true)
}

Config represents the dev server configuration

type DemosConfig added in v0.8.0

type DemosConfig struct {
	Rendering string // Default rendering mode: "light", "shadow", or "iframe"
}

DemosConfig holds demo rendering configuration

type ErrorMessage

type ErrorMessage struct {
	Type    string `json:"type"`
	Title   string `json:"title"`
	Message string `json:"message"`
	File    string `json:"file,omitempty"`
}

ErrorMessage represents a WebSocket error notification (e.g., transform errors)

type FileEvent

type FileEvent struct {
	Path           string   // Primary file path (for single file events)
	Paths          []string // All changed file paths (for batched events)
	EventType      string   // Event type for primary file (create/delete/modify)
	HasCreates     bool     // True if any files were created
	HasDeletes     bool     // True if any files were deleted
	HasPackageJSON bool     // True if package.json was modified
	Timestamp      time.Time
}

FileEvent represents a file system event

type FileWatcher

type FileWatcher interface {
	Watch(path string) error
	Events() <-chan FileEvent
	Close() error
	SetIgnorePatterns(watchDir string, patterns []string)
}

FileWatcher watches files for changes

type LogMessage

type LogMessage struct {
	Type string   `json:"type"`
	Logs []string `json:"logs"`
}

LogMessage represents a WebSocket log update

type Logger

type Logger = logger.Logger

Logger is a type alias for the logger.Logger interface

type ReloadMessage

type ReloadMessage struct {
	Type   string   `json:"type"`
	Reason string   `json:"reason"`
	Files  []string `json:"files"`
}

ReloadMessage represents a WebSocket reload event

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents the development server

func NewServer

func NewServer(port int) (*Server, error)

NewServer creates a new server with the given port

func NewServerWithConfig

func NewServerWithConfig(config Config) (*Server, error)

NewServerWithConfig creates a new server with the given config

func (*Server) BroadcastError

func (s *Server) BroadcastError(title, message, file string) error

BroadcastError broadcasts an error notification to all WebSocket clients

func (*Server) BroadcastReload

func (s *Server) BroadcastReload(files []string, reason string) error

BroadcastReload broadcasts a reload event to all WebSocket clients

func (*Server) Close

func (s *Server) Close() error

Close stops the server gracefully

func (*Server) CreateReloadMessage

func (s *Server) CreateReloadMessage(files []string, reason string) ([]byte, error)

CreateReloadMessage creates a reload message JSON

func (*Server) DebounceDuration

func (s *Server) DebounceDuration() time.Duration

DebounceDuration returns the debounce duration for file watching

func (*Server) DemoRenderingMode added in v0.8.0

func (s *Server) DemoRenderingMode() string

DemoRenderingMode returns the configured default rendering mode for demos

func (*Server) DemoRoutes

func (s *Server) DemoRoutes() map[string]*middleware.DemoRouteEntry

DemoRoutes returns the pre-computed demo routing table (both workspace and single-package mode)

func (*Server) Done

func (s *Server) Done() <-chan struct{}

Done returns a channel that's closed when the server shuts down. This allows goroutines to gracefully cancel work during shutdown.

func (*Server) FileSystem

func (s *Server) FileSystem() platform.FileSystem

FileSystem returns the filesystem abstraction

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the HTTP handler

func (*Server) ImportMap

func (s *Server) ImportMap() middleware.ImportMap

ImportMap returns the cached import map (may be nil)

func (*Server) InitializeWorkspaceMode

func (s *Server) InitializeWorkspaceMode() error

InitializeWorkspaceMode detects and initializes workspace mode if applicable

func (*Server) IsRunning

func (s *Server) IsRunning() bool

IsRunning returns whether the server is currently running

func (*Server) IsWorkspace

func (s *Server) IsWorkspace() bool

IsWorkspace returns true if server is running in workspace mode

func (*Server) Logger

func (s *Server) Logger() middleware.Logger

Logger returns the server logger

func (*Server) Manifest

func (s *Server) Manifest() ([]byte, error)

Manifest returns the current manifest

func (*Server) PackageJSON

func (s *Server) PackageJSON() (*middleware.PackageJSON, error)

PackageJSON returns parsed package.json from the watch directory

func (*Server) PathResolver added in v0.8.0

func (s *Server) PathResolver() middleware.PathResolver

PathResolver returns the cached path resolver for efficient URL rewriting

func (*Server) Port

func (s *Server) Port() int

Port returns the server's port

func (*Server) RebuildPathResolverForTest added in v0.8.0

func (s *Server) RebuildPathResolverForTest() error

RebuildPathResolverForTest exposes rebuildPathResolver for testing. This is a test-only wrapper that allows tests to trigger path resolver rebuilds.

func (*Server) RegenerateManifest

func (s *Server) RegenerateManifest() (int, error)

RegenerateManifest performs a full manifest regeneration Returns the manifest size in bytes and any error

func (*Server) RegenerateManifestIncremental

func (s *Server) RegenerateManifestIncremental(changedFiles []string) (int, error)

RegenerateManifestIncremental incrementally updates the manifest for changed files Returns the manifest size in bytes and any error

func (*Server) SetLogger

func (s *Server) SetLogger(log Logger)

SetLogger sets the server's logger

func (*Server) SetManifest

func (s *Server) SetManifest(manifest []byte) error

SetManifest sets the current manifest and builds the routing table

func (*Server) SetWatchDir

func (s *Server) SetWatchDir(dir string) error

SetWatchDir sets the directory to watch for file changes

func (*Server) SourceControlRootURL

func (s *Server) SourceControlRootURL() string

SourceControlRootURL returns the source control root URL for demo routing

func (*Server) Start

func (s *Server) Start() error

Start starts the HTTP server

func (*Server) TryLoadExistingManifest

func (s *Server) TryLoadExistingManifest() (int, error)

TryLoadExistingManifest attempts to load a manifest from disk Returns the manifest size in bytes and any error Returns 0, nil if no manifest file exists (not an error condition)

func (*Server) TsconfigRaw

func (s *Server) TsconfigRaw() string

TsconfigRaw returns the current tsconfig.json content

func (*Server) URLRewrites added in v0.8.0

func (s *Server) URLRewrites() []config.URLRewrite

URLRewrites returns the configured URL rewrites for request path resolution

func (*Server) WatchDir

func (s *Server) WatchDir() string

WatchDir returns the current watch directory

func (*Server) WebSocketManager

func (s *Server) WebSocketManager() WebSocketManager

WebSocketManager returns the WebSocket manager (nil if reload disabled)

func (*Server) WorkspacePackages

func (s *Server) WorkspacePackages() []middleware.WorkspacePackage

WorkspacePackages returns discovered workspace packages (workspace mode only)

type TransformConfig

type TransformConfig struct {
	TypeScript TypeScriptConfig
	CSS        CSSConfig
}

TransformConfig holds transform-specific configuration

type TypeScriptConfig

type TypeScriptConfig struct {
	Enabled bool
	Target  transform.Target
}

TypeScriptConfig holds TypeScript transform configuration

type WebSocketManager

type WebSocketManager interface {
	ConnectionCount() int
	Broadcast(message []byte) error
	BroadcastToPages(message []byte, pageURLs []string) error
	BroadcastShutdown() error
	CloseAll() error
	HandleConnection(w http.ResponseWriter, r *http.Request)
	SetLogger(logger Logger)
}

WebSocketManager manages WebSocket connections for live reload

Directories

Path Synopsis
internal
urlutil
Package urlutil provides utility functions for URL path matching and manipulation.
Package urlutil provides utility functions for URL path matching and manipulation.
types
Package types provides shared interfaces and types for middleware packages
Package types provides shared interfaces and types for middleware packages

Jump to

Keyboard shortcuts

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