assetmin

package module
v0.0.80 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 19 Imported by: 0

README

AssetMin

Project Badges

AssetMin is a thread-safe, concurrent asset pipeline and bundler for Go web applications. It manages CSS, JavaScript, SVG sprites, and HTML templates, providing minification and caching.

Features

  • Bundling & Minification: CSS, JS, HTML, and SVG.
  • Component Registration: Automatically extract assets from Go structs.
  • SSR Support: Safe injection of public component HTML.
  • Memory & Disk Modes: Serve directly from RAM or build to disk.
  • Hot Reload: Thread-safe cache regeneration.

Documentation

  1. Architecture
  2. Assets
  3. Component Registration
  4. HTTP Handlers
  5. SSR

Installation

go get github.com/tinywasm/assetmin

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileWrite

func FileWrite(pathFile string, data bytes.Buffer) error

pathFile e.g., "theme/htmlMainFileName" data e.g., *bytes.Buffer NOTE: The buffer data will be cleared after writing the file

func FileWriteSafe added in v0.0.76

func FileWriteSafe(pathFile string, data bytes.Buffer) error

FileWriteSafe writes the data to pathFile only if the file does not already exist.

func NewFaviconSvgHandler

func NewFaviconSvgHandler(ac *Config, outputName string) *asset

NewFaviconSvgHandler creates a handler for favicon.svg that simply minifies and copies the file without sprite wrapping. This handler processes standalone SVG files like favicon.svg

func NewHtmlHandler

func NewHtmlHandler(ac *Config, outputName, cssURL, jsURL string) *asset

NewHtmlHandler creates an HTML asset handler using the provided output filename

func NewSvgHandler

func NewSvgHandler(ac *Config, outputName string) *asset

Types

type AccessLevel added in v0.0.76

type AccessLevel interface {
	AllowedRoles(action byte) []byte
}

AccessLevel is used to check permission for SSR injection.

type AssetMin

type AssetMin struct {
	*Config
	// contains filtered or unexported fields
}

func NewAssetMin

func NewAssetMin(ac *Config) *AssetMin

func (*AssetMin) AddCSS added in v0.0.76

func (c *AssetMin) AddCSS(providers ...CSSProvider)

AddCSS appends CSS content from providers to the bundle

func (*AssetMin) AddIcon added in v0.0.76

func (c *AssetMin) AddIcon(providers ...IconProvider) error

AddIcon adds icons from providers to the bundle

func (*AssetMin) AddJS added in v0.0.76

func (c *AssetMin) AddJS(providers ...JSProvider)

AddJS appends JS content from providers to the bundle

func (*AssetMin) BuildOnDisk added in v0.0.71

func (c *AssetMin) BuildOnDisk() bool

BuildOnDisk returns true if assets are written to disk.

func (*AssetMin) EnsureOutputDirectoryExists

func (c *AssetMin) EnsureOutputDirectoryExists()

func (*AssetMin) InjectBodyContent added in v0.0.76

func (c *AssetMin) InjectBodyContent(html string)

InjectBodyContent appends HTML to the body

func (*AssetMin) Logger added in v0.0.71

func (c *AssetMin) Logger(messages ...any)

func (*AssetMin) MainInputFileRelativePath added in v0.0.70

func (c *AssetMin) MainInputFileRelativePath() string

MainInputFileRelativePath returns the main input file path. AssetMin manages multiple assets, so returns empty. Used by DevWatch for specific file watching logic.

func (*AssetMin) MainOutputFileAbsolutePath added in v0.0.70

func (c *AssetMin) MainOutputFileAbsolutePath() string

MainOutputFileAbsolutePath returns the main output file path. AssetMin manages multiple outputs, so returns empty. Used by DevWatch for exclusion logic (handled by UnobservedFiles instead)

func (*AssetMin) Name added in v0.0.71

func (c *AssetMin) Name() string

func (*AssetMin) NewFileEvent

func (c *AssetMin) NewFileEvent(fileName, extension, filePath, event string) error

event: create, remove, write, rename

func (*AssetMin) RefreshAsset

func (c *AssetMin) RefreshAsset(extension string)

func (*AssetMin) RegisterRoutes

func (c *AssetMin) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers the HTTP handlers for all assets.

func (*AssetMin) SetBuildOnDisk added in v0.0.71

func (c *AssetMin) SetBuildOnDisk(onDisk bool)

SetBuildOnDisk sets the work mode for AssetMin. Deprecated: use SetExternalSSRCompiler instead to specify both callback and disk mode.

func (*AssetMin) SetExternalSSRCompiler added in v0.0.76

func (c *AssetMin) SetExternalSSRCompiler(fn func() error, buildOnDisk bool)

SetExternalSSRCompiler sets the external compiler trigger for SSR mode.

func (*AssetMin) SetLog added in v0.0.71

func (c *AssetMin) SetLog(f func(message ...any))

func (*AssetMin) ShouldCompileToWasm added in v0.0.70

func (c *AssetMin) ShouldCompileToWasm(fileName, filePath string) bool

ShouldCompileToWasm checks if the file triggers WASM compilation. AssetMin handles assets, not WASM, so always returns false.

func (*AssetMin) SupportedExtensions

func (c *AssetMin) SupportedExtensions() []string

func (*AssetMin) UnobservedFiles

func (c *AssetMin) UnobservedFiles() []string

func (*AssetMin) UpdateFileContentInMemory

func (c *AssetMin) UpdateFileContentInMemory(filePath, extension, event string, content []byte) (*asset, error)

type CSSProvider added in v0.0.76

type CSSProvider interface {
	RenderCSS() string
}

CSSProvider indicates a component can render CSS.

type Config

type Config struct {
	OutputDir          string                 // eg: web/static, web/public, web/assets
	GetSSRClientInitJS func() (string, error) // javascript code to initialize the wasm or other handlers
	AppName            string                 // Application name for templates (default: "MyApp")
	AssetsURLPrefix    string                 // New: for HTTP routes
	DevMode            bool                   // If true, disables caching (default: false)
}

type IconProvider added in v0.0.76

type IconProvider interface {
	IconSvg() []map[string]string // Each map: {"id": "...", "svg": "<svg>...</svg>"}
}

IconProvider indicates a component can provide SVG icons.

type JSProvider added in v0.0.76

type JSProvider interface {
	RenderJS() string
}

JSProvider indicates a component can render JS.

type TestEnvironment

type TestEnvironment struct {
	BaseDir       string
	ThemeDir      string
	PublicDir     string
	ModulesDir    string
	MainJsPath    string
	MainCssPath   string
	MainSvgPath   string
	MainHtmlPath  string
	AssetsHandler *AssetMin
	// contains filtered or unexported fields
}

TestEnvironment holds all the paths and components needed for asset tests

func (*TestEnvironment) CleanDirectory

func (env *TestEnvironment) CleanDirectory()

CleanDirectory removes all content from the test directory but keeps the directory itself

func (*TestEnvironment) CreateModulesDir

func (env *TestEnvironment) CreateModulesDir() *TestEnvironment

CreateModulesDir creates the modules directory if it doesn't exist

func (*TestEnvironment) CreatePublicDir

func (env *TestEnvironment) CreatePublicDir() *TestEnvironment

CreatePublicDir creates the public directory if it doesn't exist

func (*TestEnvironment) CreateThemeDir

func (env *TestEnvironment) CreateThemeDir() *TestEnvironment

CreateThemeDir creates the theme directory if it doesn't exist

func (*TestEnvironment) TestConcurrentFileProcessing

func (env *TestEnvironment) TestConcurrentFileProcessing(fileExtension string, fileCount int)

TestConcurrentFileProcessing is a reusable function to test concurrent file processing for both JS and CSS.

func (*TestEnvironment) TestFileCRUDOperations

func (env *TestEnvironment) TestFileCRUDOperations(fileExtension string)

TestFileCRUDOperations tests the complete CRUD cycle (create, write, remove) for a file

Jump to

Keyboard shortcuts

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