minify

package
v0.0.0-...-7f9a76f Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package minify provides JavaScript and CSS minification capabilities with content-based hashing for cache-busting. It supports both bundle-based and single-file workflows, with automatic versioning and cleanup of old files.

The package offers: - Bundle configuration via JSON files - Content-based hashing for cache-busting - Automatic cleanup of old bundle versions - Support for JavaScript and CSS minification - Glob pattern support for flexible file selection - Single file minification with versioning

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndVersionCSS

func AndVersionCSS(inputPath, outputDir string) (string, error)

AndVersionCSS is a convenience function for minifying CSS files. It's a wrapper around AndVersionFile specifically for CSS files.

Parameters:

  • inputPath: Path to the input CSS file
  • outputDir: Directory where the minified CSS file should be written

Returns:

  • string: The filename of the created minified CSS file
  • error: Any error that occurred during processing

Example:

filename, err := AndVersionCSS("assets/css/main.css", "public/css")
if err != nil {
	log.Fatalf("Failed to minify CSS: %v", err)
}
fmt.Printf("Minified CSS: %s\n", filename) // Output: "main.a1b2c3d4.css"

func AndVersionFile

func AndVersionFile(inputPath, outputDir string, fileType string) (string, error)

AndVersionFile minifies a file and creates a versioned copy with content-based hashing. This is useful for individual file processing outside the bundle system.

The function: 1. Reads the input file 2. Minifies the content based on file type 3. Generates a content-based hash 4. Creates a versioned filename 5. Writes the minified content if it doesn't already exist 6. Cleans up old versions of the file

Parameters:

  • inputPath: Path to the input file to minify
  • outputDir: Directory where the minified file should be written
  • fileType: Type of file to minify ("css" or "js")

Returns:

  • string: The filename of the created minified file
  • Error: Any error that occurred during processing

Example:

filename, err := AndVersionFile("assets/css/main.css", "public/css", "css")
if err != nil {
	log.Fatalf("Failed to minify CSS: %v", err)
}
fmt.Printf("Minified CSS: %s\n", filename) // Output: "main.a1b2c3d4.css"

func BundleExists

func BundleExists(bundleName, bundlesFile, outputDir string) (bool, error)

BundleExists checks if a bundle file already exists in the output directory. This is useful for determining if a bundle needs to be regenerated.

Parameters:

  • bundleName: The name of the bundle to check
  • bundlesFile: Path to the bundle configuration file
  • outputDir: The directory where bundle files are stored

Returns:

  • bool: True if the bundle file exists, false otherwise
  • Error: Any error that occurred during the check

Example:

exists, err := BundleExists("base", "bundles.json", "./assets/static")
if err != nil {
	log.Fatalf("Failed to check bundle existence: %v", err)
}
if !exists {
	// Bundle needs to be generated
}

func CleanOldBundles

func CleanOldBundles(bundleName, bundlesFile, outputDir string) error

CleanOldBundles removes old versions of a bundle, keeping only the current version. This helps prevent disk space issues by removing outdated bundle files.

The function: 1. Determines the current bundle filename 2. Finds all files matching the bundle pattern 3. Removes files that don't match the current version

Parameters:

  • bundleName: The name of the bundle to clean
  • bundlesFile: Path to the bundle configuration file
  • outputDir: The directory containing bundle files

Returns:

  • error: Any error that occurred during cleanup

Example:

err := CleanOldBundles("base", "bundles.json", "./assets/static")
if err != nil {
	log.Printf("Warning: Failed to clean old bundles: %v", err)
}

func GetBundleFilename

func GetBundleFilename(bundleName, bundlesFile string) (string, error)

GetBundleFilename generates the complete filename for a bundle including its hash. This is useful for generating URLs or checking if a bundle file exists.

The filename format is: {bundleName}.{hash}.min.js

Parameters:

  • bundleName: The name of the bundle
  • bundlesFile: Path to the bundle configuration file

Returns:

  • string: Complete filename with hash (e.g., "base.a1b2c3d4.min.js")
  • error: Any error that occurred during processing

Example:

filename, err := GetBundleFilename("base", "bundles.json")
if err != nil {
	log.Fatalf("Failed to get bundle filename: %v", err)
}
fmt.Printf("Bundle filename: %s\n", filename) // Output: "base.a1b2c3d4.min.js"

func GetBundleHash

func GetBundleHash(bundleName, bundlesFile string) (string, error)

GetBundleHash calculates the content-based hash for a specific bundle. This is useful for generating cache-busting URLs or checking if a bundle needs to be regenerated.

The function: 1. Loads the bundle configuration 2. Finds the specified bundle 3. Collects all files matching the bundle's patterns 4. Combines and minifies the content 5. Generates a hash from the minified content

Parameters:

  • bundleName: The name of the bundle to hash
  • bundlesFile: Path to the bundle configuration file

Returns:

  • String: The 8-character hash string in base36 format
  • Error: Any error that occurred during processing

Example:

hash, err := GetBundleHash("base", "bundles.json")
if err != nil {
	log.Fatalf("Failed to get bundle hash: %v", err)
}
fmt.Printf("Bundle hash: %s\n", hash) // Output: "a1b2c3d4"

func ProcessBundles

func ProcessBundles(config Config) error

ProcessBundles processes all bundles defined in the configuration file. It reads the bundle configuration, processes each bundle by combining and minifying the specified files, and writes the output to the configured output directory with content-based hashing.

The function performs the following steps for each bundle: 1. Loads the bundle configuration from the JSON file 2. Processes each bundle by combining files and minifying JavaScript content 3. Generates a content-based hash for cache-busting 4. Writes the minified output to the specified directory

Parameters:

  • config: Configuration specifying the bundles file and output directory

Returns:

  • error: Any error that occurred during processing, or nil on success

Example:

config := Config{
	BundlesFile: "bundles.json",
	OutputDir:   "./assets/static",
}
err := ProcessBundles(config)
if err != nil {
	log.Fatalf("Failed to process bundles: %v", err)
}

Types

type Bundle

type Bundle struct {
	// Name is the bundle identifier, used in the output filename.
	// The final filename will be: {Name}.{Hash}.min.js
	Name string `json:"name"`

	// Files is a list of file patterns (glob patterns) to include in the bundle.
	// Patterns are processed in order and all matching files are combined.
	Files []string `json:"files"`
}

Bundle represents a single bundle configuration. Each bundle has a name and a list of file patterns to include.

type BundleConfig

type BundleConfig struct {
	// Bundles is the list of bundle configurations to process.
	Bundles []Bundle `json:"bundles"`
}

BundleConfig represents the structure of the bundle configuration file. It contains an array of bundle definitions.

type Config

type Config struct {
	// BundlesFile is the path to the JSON file containing bundle definitions.
	// This file should contain a BundleConfig structure with bundle definitions.
	BundlesFile string

	// OutputDir is the directory where minified bundle files will be written.
	// The directory will be created if it doesn't exist.
	OutputDir string
}

Config represents the configuration for bundle processing. It specifies the location of the bundle configuration file and the output directory for processed bundles.

Jump to

Keyboard shortcuts

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