minify: github.com/tdewolff/minify Index | Examples | Files | Directories

package minify

import "github.com/tdewolff/minify"

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

Index

Examples

Package Files

common.go minify.go

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.

func DataURI Uses

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 Uses

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 Uses

func Mediatype(b []byte) []byte

Mediatype minifies a given mediatype by removing all whitespace.

func Number Uses

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

Number minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.

type M Uses

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 Uses

func New() *M

New returns a new M.

func (*M) Add Uses

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

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

func (*M) AddCmd Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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'.

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.

func (*M) MinifyMimetype Uses

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 Uses

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.

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 Uses

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 Uses

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 Uses

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.

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 Uses

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 Uses

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

MinifierFunc is a function that implements Minifer.

func (MinifierFunc) Minify Uses

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

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

Directories

PathSynopsis
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/.

Package minify imports 14 packages (graph) and is imported by 91 packages. Updated 2019-03-16. Refresh now. Tools for package owners.