assetmin

package module
v0.0.44 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2025 License: MIT Imports: 18 Imported by: 0

README

📦 AssetMin

🚀 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.AssetConfig{
		// Define theme folder path
		ThemeFolder: func() string { 
			return "./web/theme" 
		},
		
		// Define public folder for output files
		WebFilesFolder: 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
WebFilesFolder Function that returns the path to your public output directory
Print Function for logging messages
GetRuntimeInitializerJS Function that returns initialization JavaScript code

🤝 Contributing

Contributions are welcome! Feel free to submit a Pull Request.

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

func NewHtmlHandler(ac *AssetConfig) *asset

func NewSvgHandler added in v0.0.19

func NewSvgHandler(ac *AssetConfig) *asset

Types

type AssetConfig added in v0.0.12

type AssetConfig struct {
	ThemeFolder             func() string          // eg: web/theme
	WebFilesFolder          func() string          // eg: web/static, web/public, web/assets
	Print                   func(messages ...any)  // eg: fmt.Println
	GetRuntimeInitializerJS func() (string, error) // javascript code to initialize the wasm or other handlers
}

type AssetMin

type AssetMin struct {
	*AssetConfig

	WriteOnDisk bool // Indica si se debe escribir en disco
	// contains filtered or unexported fields
}

func NewAssetMin added in v0.0.5

func NewAssetMin(ac *AssetConfig) *AssetMin

func (*AssetMin) EnsureOutputDirectoryExists added in v0.0.5

func (c *AssetMin) EnsureOutputDirectoryExists()

crea el directorio de salida si no existe

func (*AssetMin) NewFileEvent

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

event: create, remove, write, rename

func (*AssetMin) NotifyIfOutputFilesExist added in v0.0.37

func (c *AssetMin) NotifyIfOutputFilesExist()

NotifyIfOutputFilesExist checks if the output files for all assets already exist

func (*AssetMin) UnobservedFiles

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

func (*AssetMin) UpdateFileContentInMemory

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

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

func (*TestEnvironment) TestJSInitCodePriority added in v0.0.29

func (env *TestEnvironment) TestJSInitCodePriority()

TestJSInitCodePriority verifica que el código de inicialización JS aparece al principio del archivo generado

func (*TestEnvironment) TestThemePriority added in v0.0.15

func (env *TestEnvironment) TestThemePriority(fileExtension string)

TestThemePriority tests that files in 'theme' folder appear before files in 'modules' folder

Jump to

Keyboard shortcuts

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