v2 – github.com/tdewolff/minify/v2 Index | Examples | Files | Directories

package minify

import "github.com/tdewolff/minify/v2"

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

Index

Examples

Constants

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

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.

Functions

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 number (see parse.Number) and removes superfluous characters. It does not parse or output exponents.

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 (see parse.Number) and removes superfluous characters.

Types

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)
	// Output: Becausemycoffeewastoocold,Iheateditinthemicrowave.
}

Output:

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)

Source Files

common.go minify.go

Directories

PathSynopsis
benchmarks
cmd
cmd/minify
cssPackage css minifies CSS3 following the specifications at http://www.w3.org/TR/css-syntax-3/.
htmlPackage html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html.
jsPackage js minifies ECMAScript5.1 following the specifications at http://www.ecma-international.org/ecma-262/5.1/.
jsonPackage json minifies JSON following the specifications at http://json.org/.
svgPackage svg minifies SVG1.1 following the specifications at http://www.w3.org/TR/SVG11/.
xmlPackage xml minifies XML1.0 following the specifications at http://www.w3.org/TR/xml/.
Version
v2.5.1
Published
Jul 16, 2019
Platform
windows/amd64
Imports
14 packages
Last checked
now

Tools for package owners.