Back to godoc.org
github.com/tdewolff/minify/v2

Package minify

v2.9.10
Latest Go to latest

The latest major version is v2.

Published: 4 days ago | License: MIT | Module: github.com/tdewolff/minify/v2

Overview

Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.

Index

Examples

Constants

const MaxInt = int(^uint(0) >> 1)

MaxInt is the maximum value of int.

const MinInt = -MaxInt - 1

MinInt is the minimum value of int.

Variables

var Epsilon = 0.00001

Epsilon is the closest number to zero that is not considered to be zero.

var ErrNotExist = errors.New("minifier does not exist for mimetype")

ErrNotExist is returned when no minifier exists for a given mimetype.

var Warning = log.New(os.Stderr, "WARNING: ", 0)

Warning is used to report usage warnings such as using a deprecated feature

func DataURI

func DataURI(m *M, dataURI []byte) []byte

DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.

func Decimal

func Decimal(num []byte, prec int) []byte

Decimal minifies a given byte slice containing a decimal and removes superfluous characters. It differs from Number in that it does not parse exponents. It does not parse or output exponents. prec is the number of significant digits. When prec is zero it will keep all digits. Only digits after the dot can be removed to reach the number of significant digits. Very large number may thus have more significant digits.

func Mediatype

func Mediatype(b []byte) []byte

Mediatype minifies a given mediatype by removing all whitespace.

func Number

func Number(num []byte, prec int) []byte

Number minifies a given byte slice containing a number and removes superfluous characters.

type M

type M struct {
	URL *url.URL
	// contains filtered or unexported fields
}

M holds a map of mimetype => function to allow recursive minifier calls of the minifier functions.

func New

func New() *M

New returns a new M.

func (*M) Add

func (m *M) Add(mimetype string, minifier Minifier)

Add adds a minifier to the mimetype => function map (unsafe for concurrent use).

func (*M) AddCmd

func (m *M) AddCmd(mimetype string, cmd *exec.Cmd)

AddCmd adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification. It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype.

func (*M) AddCmdRegexp

func (m *M) AddCmdRegexp(pattern *regexp.Regexp, cmd *exec.Cmd)

AddCmdRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification. It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype regular expression.

func (*M) AddFunc

func (m *M) AddFunc(mimetype string, minifier MinifierFunc)

AddFunc adds a minify function to the mimetype => function map (unsafe for concurrent use).

func (*M) AddFuncRegexp

func (m *M) AddFuncRegexp(pattern *regexp.Regexp, minifier MinifierFunc)

AddFuncRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use).

func (*M) AddRegexp

func (m *M) AddRegexp(pattern *regexp.Regexp, minifier Minifier)

AddRegexp adds a minifier to the mimetype => function map (unsafe for concurrent use).

func (*M) Bytes

func (m *M) Bytes(mediatype string, v []byte) ([]byte, error)

Bytes minifies an array of bytes (safe for concurrent use). When an error occurs it return the original array and the error. It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.

func (*M) Match

func (m *M) Match(mediatype string) (string, map[string]string, MinifierFunc)

Match returns the pattern and minifier that gets matched with the mediatype. It returns nil when no matching minifier exists. It has the same matching algorithm as Minify.

func (*M) Middleware

func (m *M) Middleware(next http.Handler) http.Handler

Middleware provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter. http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ... Minification might be slower than just sending the original file! Caching is advised.

func (*M) Minify

func (m *M) Minify(mediatype string, w io.Writer, r io.Reader) error

Minify minifies the content of a Reader and writes it to a Writer (safe for concurrent use). An error is returned when no such mimetype exists (ErrNotExist) or when an error occurred in the minifier function. Mediatype may take the form of 'text/plain', 'text/*', '*/*' or 'text/plain; charset=UTF-8; version=2.0'.

Example (Custom)

Code:

m := New()
m.AddFunc("text/plain", func(m *M, w io.Writer, r io.Reader, _ map[string]string) error {
	// remove all newlines and spaces
	rb := bufio.NewReader(r)
	for {
		line, err := rb.ReadString('\n')
		if err != nil && err != io.EOF {
			return err
		}
		if _, errws := io.WriteString(w, strings.Replace(line, " ", "", -1)); errws != nil {
			return errws
		}
		if err == io.EOF {
			break
		}
	}
	return nil
})

in := "Because my coffee was too cold, I heated it in the microwave."
out, err := m.String("text/plain", in)
if err != nil {
	panic(err)
}
fmt.Println(out)
Becausemycoffeewastoocold,Iheateditinthemicrowave.

func (*M) MinifyMimetype

func (m *M) MinifyMimetype(mimetype []byte, w io.Writer, r io.Reader, params map[string]string) error

MinifyMimetype minifies the content of a Reader and writes it to a Writer (safe for concurrent use). It is a lower level version of Minify and requires the mediatype to be split up into mimetype and parameters. It is mostly used internally by minifiers because it is faster (no need to convert a byte-slice to string and vice versa).

func (*M) Reader

func (m *M) Reader(mediatype string, r io.Reader) io.Reader

Reader wraps a Reader interface and minifies the stream. Errors from the minifier are returned by the reader.

Example

Code:

b := bytes.NewReader([]byte("input"))

m := New()
// add minfiers

r := m.Reader("mime/type", b)
if _, err := io.Copy(os.Stdout, r); err != nil {
	if _, err := io.Copy(os.Stdout, b); err != nil {
		panic(err)
	}
}

func (*M) ResponseWriter

func (m *M) ResponseWriter(w http.ResponseWriter, r *http.Request) *minifyResponseWriter

ResponseWriter minifies any writes to the http.ResponseWriter. http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ... Minification might be slower than just sending the original file! Caching is advised.

func (*M) String

func (m *M) String(mediatype string, v string) (string, error)

String minifies a string (safe for concurrent use). When an error occurs it return the original string and the error. It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.

func (*M) Writer

func (m *M) Writer(mediatype string, w io.Writer) *minifyWriter

Writer wraps a Writer interface and minifies the stream. Errors from the minifier are returned by Close on the writer. The writer must be closed explicitly.

Example

Code:

m := New()
// add minfiers

w := m.Writer("mime/type", os.Stdout)
if _, err := w.Write([]byte("input")); err != nil {
	panic(err)
}
if err := w.Close(); err != nil {
	panic(err)
}

type Minifier

type Minifier interface {
	Minify(*M, io.Writer, io.Reader, map[string]string) error
}

Minifier is the interface for minifiers. The *M parameter is used for minifying embedded resources, such as JS within HTML.

type MinifierFunc

type MinifierFunc func(*M, io.Writer, io.Reader, map[string]string) error

MinifierFunc is a function that implements Minifer.

func (MinifierFunc) Minify

func (f MinifierFunc) Minify(m *M, w io.Writer, r io.Reader, params map[string]string) error

Minify calls f(m, w, r, params)

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier