Run Minify using Docker
masterYou can run the Minify CLI using a Docker container:
docker run -it tdewolff/minify --helprepository·master·Indexed 26 days ago
https://github.com/tdewolff/minifyA high-performance minification package written in Go providing minifiers for HTML5, CSS3, JS, JSON, SVG, and XML. It includes a CLI tool, an extensible interface for custom minifiers, and official bindings for JavaScript (@tdewolff/minify) and Python.
You can run the Minify CLI using a Docker container:
docker run -it tdewolff/minify --helpMinify can be used as middleware in a Go web server. Use m.MiddlewareWithError(handler) to wrap an existing http.Handler. This will automatically minify responses.
Important: When using MiddlewareWithError, you must close the response writer (which is returned as an io.Closer) to ensure errors are captured and the response is properly finalized.
// Standard Middleware usage
fs := http.FileServer(http.Dir("www/"))
http.Handle("/", m.MiddlewareWithError(fs))
// Handling errors in custom handlers with Middleware
m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(input))
// You must cast the writer to io.Closer and close it
if err = w.(io.Closer).Close(); err != nil {
panic(err)
}
})).ServeHTTP(rec, req)The minify command minifies input files or directories based on their extension or an explicitly provided type.
minify -o <output> <input>minify <input>--type.minify --type=js < script.js > script-min.js-r to process directories and subdirectories.src/) copies all files inside the directory.out/) forces writing into a directory.-b or --bundle to concatenate multiple input files into a single output file.-w or --watch to automatically re-minify files when they change.-i or --inplace to overwrite the input files.minify -o index-min.html index.htmlYou can minify resources on-the-fly by wrapping an http.Handler with m.Middleware(handler).
Content-Length header.Content-Type header or, if empty, the file extension in the request URI.fs := http.FileServer(http.Dir("www/"))
http.Handle("/", m.Middleware(fs))Use minify.New() to retrieve a *minify.M struct. This struct holds a mapping of mediatypes to their respective minification functions. You must register minifiers (e.g., CSS, HTML, JS) using AddFunc, AddFuncRegexp, Add, or AddRegexp before they can be used.
Note: Input streams are buffered for performance, but output streams are not. It is recommended to preallocate an output buffer equal to the input size or use bufio to wrap the output writer.
```go
m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
```埋You can install the minify CLI tool via several methods depending on your operating system or environment.
Ensure you have Go and Git installed, then run:
mkdir $HOME/src
cd $HOME/src
git clone https://github.com/tdewolff/minify.git
cd minify
make installIf make is not available, use:
go install ./cmd/minify
source minify_bash_tab_completionTo install the latest version directly via Go:
go install github.com/tdewolff/minify/v2/cmd/minify@latestyay -S minifypkg install minifyapk add minify (requires community repo)brew install tdewolff/tap/minifysudo apt install minify (may be outdated)Pull the image:
docker pull tdewolff/minifyRun in interactive mode:
docker run -i tdewolff/minify sh -c 'echo "(function(){ if (a == false) { return 0; } else { return 1; } })();" | minify --type js'go install github.com/tdewolff/minify/v2/cmd/minify@latestMinify is available for several other environments:
npm i @tdewolff/minifypip install tdewolff-minifyscoop install main/minifyInstall-Package NMinify or dotnet add package NMinifyThe Python bindings provide three primary functions: minify.config() to set minifier options, minify.string() to minify a string in memory, and minify.file() to minify a file on disk. All functions require a mediatype as the first argument.
import minify
# Configure minifier options
minify.config({
'css-precision': 0,
'html-keep-comments': False,
'js-version': 0,
})
# Minify a string
s = minify.string('text/html', '<span style="color:#ff0000;" class="text">Some text</span>')
print(s)
# Minify a file (creates output_file from input_file)
minify.file('text/html', 'example.html', 'example.min.html')To use Minify in your Go project, ensure you have Git and Go (1.18 or higher) installed. Initialize your module and fetch the package using the following commands:
mkdir Project
cd Project
go mod init
go get -u github.com/tdewolff/minify/v2To clean up your go.mod and go.sum files, you can optionally run go mod tidy.
To use the various minifiers (HTML, CSS, JS, etc.), you must import the core package along with the specific minifier implementations:
import (
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)To use the JavaScript bindings for the Go minifiers, install the @tdewolff/minify package. This package requires Node.js 20.19+.
npm install @tdewolff/minify