imagemin

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 15 Imported by: 0

README

imagemin — WebP Image Processing for SSR Modules

imagemin discovers images declared in ssr.go files of Go modules, converts them to WebP with responsive variants (L, M, S), and writes them to web/public/img/.

Features

  • Responsive Variants: Automatically generates S (640px), M (1024px), and L (1920px) variants.
  • WebP Optimized: Uses WebP for superior compression and quality.
  • Stat-based Caching: Uses file modification times (mtime) to avoid redundant processing without extra state files.
  • No Upscaling: Never enlarges images smaller than the target variant size.
  • AST-based Extraction: Discovers images by parsing source code, no runtime execution required.

Documentation

For a detailed look at the internal workings and design decisions, see the Architecture Documentation.

Usage in ssr.go

Modules declare their images by implementing a RenderImages function:

//go:build !wasm

package mymodule

import "github.com/tinywasm/imagemin"

func RenderImages() []imagemin.Asset {
    return []imagemin.Asset{
        {
            Path: "img/logo.png",
            Variants: imagemin.VariantS | imagemin.VariantM,
            Alt: "Company Logo",
        },
        {
            Path: "img/hero.jpg",
            Variants: imagemin.AllVariants,
        },
    }
}

Architecture Overview

  • types.go: Core types (Asset, Variant) shared across all contexts.
  • extract.go: AST parser that finds RenderImages declarations in modules.
  • convert.go: Image processing logic using imaging and nativewebp.
  • loader.go: Module discovery and orchestration using go list.
  • imagemin.go: Main Handler that implements the devwatch interfaces.

API

Configuration
type Config struct {
    RootDir   string // Project root (where go.mod is)
    OutputDir string // Output directory for WebP images (e.g., "web/public/img")
    Quality   int    // WebP quality (0-100)
}
Handler
handler := imagemin.New(&config)
handler.InitDefaultLoader() // Uses 'go list' for discovery
err := handler.LoadImages() // Initial load

Documentation

Index

Constants

View Source
const AllVariants = VariantS | VariantM | VariantL

AllVariants includes all responsive variants.

Variables

This section is empty.

Functions

func IsUpToDate added in v0.0.3

func IsUpToDate(srcPath string, variants Variant, outputDir string) bool

func ProcessImage added in v0.0.3

func ProcessImage(src ParsedAsset, outputDir string, quality int, log func(...any)) ([]string, error)

Types

type Asset added in v0.0.3

type Asset struct {
	Path     string  // relative to the module directory: "img/logo.png"
	Variants Variant // e.g., AllVariants, VariantS|VariantM, VariantL
	Alt      string  // SEO alternative text; if empty derived from filename
}

Asset represents an image declaration in a module.

type Config added in v0.0.3

type Config struct {
	RootDir   string
	OutputDir string
	Quality   int
}

type Handler added in v0.0.3

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

func New

func New(c *Config) *Handler

func (*Handler) InitDefaultLoader added in v0.0.3

func (h *Handler) InitDefaultLoader()

func (*Handler) LoadImages added in v0.0.3

func (h *Handler) LoadImages() error

LoadImages discovers modules via go list and processes their images.

func (*Handler) Logger added in v0.0.4

func (h *Handler) Logger(messages ...any)

func (*Handler) Name added in v0.0.3

func (h *Handler) Name() string

func (*Handler) ReloadModule added in v0.0.3

func (h *Handler) ReloadModule(moduleDir string) error

ReloadModule re-extracts and re-processes images for a single module.

func (*Handler) SetListModulesFn added in v0.0.3

func (h *Handler) SetListModulesFn(fn func(rootDir string) ([]string, error))

func (*Handler) SetLog added in v0.0.3

func (h *Handler) SetLog(fn func(messages ...any))

func (*Handler) UnobservedFiles added in v0.0.3

func (h *Handler) UnobservedFiles() []string

type ParsedAsset added in v0.0.3

type ParsedAsset struct {
	AbsPath  string  // absolute path: moduleDir + "/" + asset.Path
	Variants Variant // resolved bitmask value
	Alt      string
	BaseName string // base name without extension: "logo", "hero"
}

func ExtractImages added in v0.0.3

func ExtractImages(moduleDir string) ([]ParsedAsset, error)

type Variant added in v0.0.3

type Variant uint8

Variant represents a bitmask for responsive image variants.

const (
	VariantS Variant = 1 << iota // 1 — 640px  mobile
	VariantM                     // 2 — 1024px tablet
	VariantL                     // 4 — 1920px desktop
)

Jump to

Keyboard shortcuts

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