assetmin

package module
v0.0.67 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 20 Imported by: 0

README

📦 AssetMin

Project Badges

🚀 A lightweight and efficient web asset packager and minifier for Go applications

AssetMin is a simple yet powerful tool that bundles and minifies your JavaScript and CSS files into single optimized output files, improving your web application's performance.

🛠️ Primary Use Case

AssetMin is primarily used in the GoDEV framework for developing full stack projects with Go. It provides an efficient solution for managing and optimizing web assets, ensuring seamless integration into your Go workflow. Whether you're building a small project or a large-scale application, AssetMin simplifies the bundling and minification of your JavaScript and CSS files with minimal effort.

✨ Features

  • 🔄 Live Asset Processing - Automatically processes files when they are created or modified
  • 🗜️ Minification - Reduces file size by removing unnecessary characters
  • 🔌 Concurrency Support - Thread-safe operation for multiple file processing
  • 📁 Directory Structure - Organizes files from theme and module directories
  • 🛠️ Simple API - Easy to integrate into your Go application

📥 Installation

import "github.com/cdvelop/assetmin"

🚀 Quick Start

package main

import (
	"fmt"
	
	"github.com/cdvelop/assetmin"
)

func main() {
	// Create configuration
	config := &assetmin.Config{
		// Define theme folder path
		ThemeFolder: func() string { 
			return "./web/theme" 
		},
		
		// Define public folder for output files
		OutputDir: func() string { 
			return "./web/public" 
		},
		
		// Define print function
		Print: func(messages ...any) {
			fmt.Println(messages...)
		},
		
		// Optional JavaScript initialization code
		GetRuntimeInitializerJS: func() (string, error) {
			return "console.log('App initialized!');", nil
		},
	}
	
	// Initialize AssetMin
	handler := assetmin.NewAssetMin(config)
	
	// Process a new JavaScript file
	handler.NewFileEvent("script.js", ".js", "./path/to/script.js", "create")
	
	// Process a new CSS file
	handler.NewFileEvent("styles.css", ".css", "./path/to/styles.css", "create")
	
	// Files will be bundled and minified into:
	// - ./web/public/main.js
	// - ./web/public/style.css
}

🔄 How It Works

  1. 📁 You define theme and output directories
  2. 📝 Create or modify JS/CSS files
  3. 🔄 Call NewFileEvent when files change
  4. 📦 AssetMin processes and bundles your files
  5. 🚀 Output is saved to your public directory as minified files

📋 API Reference

NewAssetMin(config *Config) *AssetMin

Creates a new instance of the AssetMin handler.

NewFileEvent(filename, extension, filepath, event string) error

Processes a file event (create/write).

🛠️ Configuration Options

Option Description
ThemeFolder Function that returns the path to your theme directory
OutputDir Function that returns the path to your public output directory
Print Function for logging messages
GetRuntimeInitializerJS Function that returns initialization JavaScript code

📋 Roadmap

See docs/issues/README.md for planned features:

  • HTTP Routes & Work Modes (SSR/Disk)
  • Asset Caching System
  • Template System Refactor
  • TUI Integration

Contributing

📄 License

This project is licensed under the [MIT] License.

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 NewFaviconSvgHandler added in v0.0.62

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 added in v0.0.20

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

NewHtmlHandler creates an HTML asset handler using the provided output filename

func NewSvgHandler added in v0.0.19

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

Types

type AssetMin

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

func NewAssetMin added in v0.0.5

func NewAssetMin(ac *Config) *AssetMin

func (*AssetMin) EnsureOutputDirectoryExists added in v0.0.5

func (c *AssetMin) EnsureOutputDirectoryExists()

func (*AssetMin) GetWorkMode added in v0.0.67

func (c *AssetMin) GetWorkMode() WorkMode

GetWorkMode returns the current work mode of AssetMin.

func (*AssetMin) NewFileEvent

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

event: create, remove, write, rename

func (*AssetMin) RefreshAsset added in v0.0.66

func (c *AssetMin) RefreshAsset(extension string)

func (*AssetMin) RegisterRoutes added in v0.0.67

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

RegisterRoutes registers the HTTP handlers for all assets.

func (*AssetMin) SetWorkMode added in v0.0.67

func (c *AssetMin) SetWorkMode(mode WorkMode)

SetWorkMode sets the work mode for AssetMin.

func (*AssetMin) SupportedExtensions added in v0.0.53

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 Config added in v0.0.3

type Config struct {
	OutputDir               string                 // eg: web/static, web/public, web/assets
	Logger                  func(message ...any)   // Renamed from io.Writer to a function type
	GetRuntimeInitializerJS 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
}

type TestEnvironment added in v0.0.3

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 added in v0.0.3

func (env *TestEnvironment) CleanDirectory()

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

func (*TestEnvironment) CreateModulesDir added in v0.0.4

func (env *TestEnvironment) CreateModulesDir() *TestEnvironment

CreateModulesDir creates the modules directory if it doesn't exist

func (*TestEnvironment) CreatePublicDir added in v0.0.4

func (env *TestEnvironment) CreatePublicDir() *TestEnvironment

CreatePublicDir creates the public directory if it doesn't exist

func (*TestEnvironment) CreateThemeDir added in v0.0.4

func (env *TestEnvironment) CreateThemeDir() *TestEnvironment

CreateThemeDir creates the theme directory if it doesn't exist

func (*TestEnvironment) TestConcurrentFileProcessing added in v0.0.9

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 added in v0.0.11

func (env *TestEnvironment) TestFileCRUDOperations(fileExtension string)

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

type WorkMode added in v0.0.67

type WorkMode int
const (
	MemoryMode WorkMode = iota // Serve from memory cache (default)
	DiskMode                   // Write to disk + serve from cache
)

Jump to

Keyboard shortcuts

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