Giter Site home page Giter Site logo

tdewolff / minify Goto Github PK

View Code? Open in Web Editor NEW
3.6K 48.0 207.0 8.49 MB

Go minifiers for web formats

Home Page: https://go.tacodewolff.nl/minify

License: MIT License

Go 96.36% Shell 0.42% Makefile 0.42% Dockerfile 0.07% Python 1.08% HTML 0.03% JavaScript 0.34% C 1.26% Batchfile 0.01%
minifier go html js json xml svg css minify javascript

minify's Introduction

Minify API reference Go Report Card codecov

Online demo if you need to minify files now.

Binaries of CLI for various platforms. See CLI for more installation instructions.

Python bindings install with pip install tdewolff-minify

JavaScript bindings install with npm i @tdewolff/minify

.NET bindings install with Install-Package NMinify or dotnet add package NMinify, thanks to Jonas Kamsker for the port


Did you know that the shortest valid piece of HTML5 is <!doctype html><title>x</title>? See for yourself at the W3C Validator!

Minify is a minifier package written in Go. It provides HTML5, CSS3, JS, JSON, SVG and XML minifiers and an interface to implement any other minifier. Minification is the process of removing bytes from a file (such as whitespace) without changing its output and therefore shrinking its size and speeding up transmission over the internet and possibly parsing. The implemented minifiers are designed for high performance (see https://github.com/privatenumber/minification-benchmarks where this library is (one of) the fastest JS minifiers).

The core functionality associates mimetypes with minification functions, allowing embedded resources (like CSS or JS within HTML files) to be minified as well. Users can add new implementations that are triggered based on a mimetype (or pattern), or redirect to an external command (like ClosureCompiler, UglifyCSS, ...).

Sponsors

I'm actively looking for support in the form of donations or sponsorships to keep developing this library and highly appreciate any gesture. Please see the Sponsors button in GitHub for ways to contribute, or contact me directly.

SiteGround

Table of Contents

Roadmap

  • Use ASM/SSE to further speed-up core parts of the parsers/minifiers
  • Improve JS minifiers by shortening variables and proper semicolon omission
  • Speed-up SVG minifier, it is very slow
  • Proper parser error reporting and line number + column information
  • Generation of source maps (uncertain, might slow down parsers too much if it cannot run separately nicely)
  • Create a cmd to pack webfiles (much like webpack), ie. merging CSS and JS files, inlining small external files, minification and gzipping. This would work on HTML files.

Prologue

Minifiers or bindings to minifiers exist in almost all programming languages. Some implementations are merely using several regular expressions to trim whitespace and comments (even though regex for parsing HTML/XML is ill-advised, for a good read see Regular Expressions: Now You Have Two Problems). Some implementations are much more profound, such as the YUI Compressor and Google Closure Compiler for JS. As most existing implementations either use JavaScript, use regexes, and don't focus on performance, they are pretty slow.

This minifier proves to be that fast and extensive minifier that can handle HTML and any other filetype it may contain (CSS, JS, ...). It is usually orders of magnitude faster than existing minifiers.

Installation

Make sure you have Git and Go (1.18 or higher) installed, run

mkdir Project
cd Project
go mod init
go get -u github.com/tdewolff/minify/v2

Then add the following imports to be able to use the various minifiers

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"
)

You can optionally run go mod tidy to clean up the go.mod and go.sum files.

See CLI tool for installation instructions of the binary.

Docker

If you want to use Docker, please see https://hub.docker.com/r/tdewolff/minify.

$ docker run -it tdewolff/minify --help

API stability

There is no guarantee for absolute stability, but I take issues and bugs seriously and don't take API changes lightly. The library will be maintained in a compatible way unless vital bugs prevent me from doing so. There has been one API change after v1 which added options support and I took the opportunity to push through some more API clean up as well. There are no plans whatsoever for future API changes.

Testing

For all subpackages and the imported parse package, test coverage of 100% is pursued. Besides full coverage, the minifiers are fuzz tested using github.com/dvyukov/go-fuzz, see the wiki for the most important bugs found by fuzz testing. These tests ensure that everything works as intended and that the code does not crash (whatever the input). If you still encounter a bug, please file a bug report!

Performance

The benchmarks directory contains a number of standardized samples used to compare performance between changes. To give an indication of the speed of this library, I've ran the tests on my Thinkpad T460 (i5-6300U quad-core 2.4GHz running Arch Linux) using Go 1.15.

name                              time/op
CSS/sample_bootstrap.css-4          2.70ms ± 0%
CSS/sample_gumby.css-4              3.57ms ± 0%
CSS/sample_fontawesome.css-4         767µs ± 0%
CSS/sample_normalize.css-4          85.5µs ± 0%
HTML/sample_amazon.html-4           15.2ms ± 0%
HTML/sample_bbc.html-4              3.90ms ± 0%
HTML/sample_blogpost.html-4          420µs ± 0%
HTML/sample_es6.html-4              15.6ms ± 0%
HTML/sample_stackoverflow.html-4    3.73ms ± 0%
HTML/sample_wikipedia.html-4        6.60ms ± 0%
JS/sample_ace.js-4                  28.7ms ± 0%
JS/sample_dot.js-4                   357µs ± 0%
JS/sample_jquery.js-4               10.0ms ± 0%
JS/sample_jqueryui.js-4             20.4ms ± 0%
JS/sample_moment.js-4               3.47ms ± 0%
JSON/sample_large.json-4            3.25ms ± 0%
JSON/sample_testsuite.json-4        1.74ms ± 0%
JSON/sample_twitter.json-4          24.2µs ± 0%
SVG/sample_arctic.svg-4             34.7ms ± 0%
SVG/sample_gopher.svg-4              307µs ± 0%
SVG/sample_usa.svg-4                57.4ms ± 0%
SVG/sample_car.svg-4                18.0ms ± 0%
SVG/sample_tiger.svg-4              5.61ms ± 0%
XML/sample_books.xml-4              54.7µs ± 0%
XML/sample_catalog.xml-4            33.0µs ± 0%
XML/sample_omg.xml-4                7.17ms ± 0%

name                              speed
CSS/sample_bootstrap.css-4        50.7MB/s ± 0%
CSS/sample_gumby.css-4            52.1MB/s ± 0%
CSS/sample_fontawesome.css-4      61.2MB/s ± 0%
CSS/sample_normalize.css-4        70.8MB/s ± 0%
HTML/sample_amazon.html-4         31.1MB/s ± 0%
HTML/sample_bbc.html-4            29.5MB/s ± 0%
HTML/sample_blogpost.html-4       49.8MB/s ± 0%
HTML/sample_es6.html-4            65.6MB/s ± 0%
HTML/sample_stackoverflow.html-4  55.0MB/s ± 0%
HTML/sample_wikipedia.html-4      67.5MB/s ± 0%
JS/sample_ace.js-4                22.4MB/s ± 0%
JS/sample_dot.js-4                14.5MB/s ± 0%
JS/sample_jquery.js-4             24.8MB/s ± 0%
JS/sample_jqueryui.js-4           23.0MB/s ± 0%
JS/sample_moment.js-4             28.6MB/s ± 0%
JSON/sample_large.json-4           234MB/s ± 0%
JSON/sample_testsuite.json-4       394MB/s ± 0%
JSON/sample_twitter.json-4        63.0MB/s ± 0%
SVG/sample_arctic.svg-4           42.4MB/s ± 0%
SVG/sample_gopher.svg-4           19.0MB/s ± 0%
SVG/sample_usa.svg-4              17.8MB/s ± 0%
SVG/sample_car.svg-4              29.3MB/s ± 0%
SVG/sample_tiger.svg-4            12.2MB/s ± 0%
XML/sample_books.xml-4            81.0MB/s ± 0%
XML/sample_catalog.xml-4          58.6MB/s ± 0%
XML/sample_omg.xml-4               159MB/s ± 0%

HTML

HTML (with JS and CSS) minification typically shaves off about 10%.

The HTML5 minifier uses these minifications:

  • strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline)
  • strip superfluous quotes, or uses single/double quotes whichever requires fewer escapes
  • strip default attribute values and attribute boolean values
  • strip some empty attributes
  • strip unrequired tags (html, head, body, ...)
  • strip unrequired end tags (tr, td, li, ... and often p)
  • strip default protocols (http:, https: and javascript:)
  • strip all comments (including conditional comments, old IE versions are not supported anymore by Microsoft)
  • shorten doctype and meta charset
  • lowercase tags, attributes and some values to enhance gzip compression

Options:

  • KeepSpecialComments preserve all special comments, including Server Side Includes such as <!--#include file="header.html" --> and IE conditional comments such as <!--[if IE 6]><![endif]--> and <![if IE 6]><![endif]>, see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax
  • KeepDefaultAttrVals preserve default attribute values such as <script type="application/javascript">
  • KeepDocumentTags preserve html, head and body tags
  • KeepEndTags preserve all end tags
  • KeepQuotes preserve quotes around attribute values
  • KeepWhitespace preserve whitespace between inline tags but still collapse multiple whitespace characters into one
  • TemplateDelims preserve context within and surrounding the given opening and closing delimiters

After recent benchmarking and profiling it became really fast and minifies pages in the 10ms range, making it viable for on-the-fly minification.

However, be careful when doing on-the-fly minification. Minification typically trims off 10% and does this at worst around about 20MB/s. This means users have to download slower than 2MB/s to make on-the-fly minification worthwhile. This may or may not apply in your situation. Rather use caching!

Whitespace removal

The whitespace removal mechanism collapses all sequences of whitespace (spaces, newlines, tabs) to a single space. If the sequence contained a newline or carriage return it will collapse into a newline character instead. It trims all text parts (in between tags) depending on whether it was preceded by a space from a previous piece of text and whether it is followed up by a block element or an inline element. In the former case we can omit spaces while for inline elements whitespace has significance.

Make sure your HTML doesn't depend on whitespace between block elements that have been changed to inline or inline-block elements using CSS. Your layout should not depend on those whitespaces as the minifier will remove them. An example is a menu consisting of multiple <li> that have display:inline-block applied and have whitespace in between them. It is bad practise to rely on whitespace for element positioning anyways!

CSS

Minification typically shaves off about 10%-15%. This CSS minifier will not do structural changes to your stylesheets. Although this could result in smaller files, the complexity is quite high and the risk of breaking website is high too.

The CSS minifier will only use safe minifications:

  • remove comments and unnecessary whitespace (but keep /*! ... */ which usually contains the license)
  • remove trailing semicolons
  • optimize margin, padding and border-width number of sides
  • shorten numbers by removing unnecessary + and zeros and rewriting with/without exponent
  • remove dimension and percentage for zero values
  • remove quotes for URLs
  • remove quotes for font families and make lowercase
  • rewrite hex colors to/from color names, or to three digit hex
  • rewrite rgb(, rgba(, hsl( and hsla( colors to hex or name
  • use four digit hex for alpha values (transparent#0000)
  • replace normal and bold by numbers for font-weight and font
  • replace none0 for border, background and outline
  • lowercase all identifiers except classes, IDs and URLs to enhance gzip compression
  • shorten MS alpha function
  • rewrite data URIs with base64 or ASCII whichever is shorter
  • calls minifier for data URI mediatypes, thus you can compress embedded SVG files if you have that minifier attached
  • shorten aggregate declarations such as background and font

It does purposely not use the following techniques:

  • (partially) merge rulesets
  • (partially) split rulesets
  • collapse multiple declarations when main declaration is defined within a ruleset (don't put font-weight within an already existing font, too complex)
  • remove overwritten properties in ruleset (this not always overwrites it, for example with !important)
  • rewrite properties into one ruleset if possible (like margin-top, margin-right, margin-bottom and margin-leftmargin)
  • put nested ID selector at the front (body > div#elem p#elem p)
  • rewrite attribute selectors for IDs and classes (div[id=a]div#a)
  • put space after pseudo-selectors (IE6 is old, move on!)

There are a couple of comparison tables online, such as CSS Minifier Comparison, CSS minifiers comparison and CleanCSS tests. Comparing speed between each, this minifier will usually be between 10x-300x faster than existing implementations, and even rank among the top for minification ratios. It falls short with the purposely not implemented and often unsafe techniques.

Options:

  • KeepCSS2 prohibits using CSS3 syntax (such as exponents in numbers, or rgba(rgb(), might be incomplete
  • Precision number of significant digits to preserve for numbers, 0 means no trimming

JS

The JS minifier typically shaves off about 35% -- 65% of filesize depening on the file, which is a compression close to many other minifiers. Common speeds of PHP and JS implementations are about 100-300kB/s (see Uglify2, Adventures in PHP web asset minimization). This implementation is orders of magnitude faster at around ~25MB/s.

The following features are implemented:

  • remove superfluous whitespace
  • remove superfluous semicolons
  • shorten true, false, and undefined to !0, !1 and void 0
  • rename variables and functions to shorter names (not in global scope)
  • move var declarations to the top of the global/function scope (if more than one)
  • collapse if/else statements to expressions
  • minify conditional expressions to simpler ones
  • merge sequential expression statements to one, including into return and throw
  • remove superfluous grouping in expressions
  • shorten or remove string escapes
  • convert object key or index expression from string to identifier or decimal
  • merge concatenated strings
  • rewrite numbers (binary, octal, decimal, hexadecimal) to shorter representations

Options:

  • KeepVarNames keeps variable names as they are and omits shortening variable names
  • Precision number of significant digits to preserve for numbers, 0 means no trimming
  • Version ECMAScript version to use for output, 0 is the latest

Comparison with other tools

Performance is measured with time [command] ran 10 times and selecting the fastest one, on a Thinkpad T460 (i5-6300U quad-core 2.4GHz running Arch Linux) using Go 1.15.

  • minify: minify -o script.min.js script.js
  • esbuild: esbuild --minify --outfile=script.min.js script.js
  • terser: terser script.js --compress --mangle -o script.min.js
  • UglifyJS: uglifyjs --compress --mangle -o script.min.js script.js
  • Closure Compiler: closure-compiler -O SIMPLE --js script.js --js_output_file script.min.js --language_in ECMASCRIPT_NEXT -W QUIET --jscomp_off=checkVars optimization level SIMPLE instead of ADVANCED to make similar assumptions as do the other tools (do not rename/assume anything of global level variables)

Compression ratio (lower is better)

All tools give very similar results, although UglifyJS compresses slightly better.

Tool ace.js dot.js jquery.js jqueryui.js moment.js
minify 53.7% 64.8% 34.2% 51.3% 34.8%
esbuild 53.8% 66.3% 34.4% 53.1% 34.8%
terser 53.2% 65.2% 34.2% 51.8% 34.7%
UglifyJS 53.1% 64.7% 33.8% 50.7% 34.2%
Closure Compiler 53.4% 64.0% 35.7% 53.6% 34.3%

Time (lower is better)

Most tools are extremely slow, with minify and esbuild being orders of magnitudes faster.

Tool ace.js dot.js jquery.js jqueryui.js moment.js
minify 49ms 5ms 22ms 35ms 13ms
esbuild 64ms 9ms 31ms 51ms 17ms
terser 2900s 180ms 1400ms 2200ms 730ms
UglifyJS 3900ms 210ms 2000ms 3100ms 910ms
Closure Compiler 6100ms 2500ms 4400ms 5300ms 3500ms

JSON

Minification typically shaves off about 15% of filesize for common indented JSON such as generated by JSON Generator.

The JSON minifier only removes whitespace, which is the only thing that can be left out, and minifies numbers (1000 => 1e3).

Options:

  • Precision number of significant digits to preserve for numbers, 0 means no trimming
  • KeepNumbers do not minify numbers if set to true, by default numbers will be minified

SVG

The SVG minifier uses these minifications:

  • trim and collapse whitespace between all tags
  • strip comments, empty doctype, XML prelude, metadata
  • strip SVG version
  • strip CDATA sections wherever possible
  • collapse tags with no content to a void tag
  • minify style tag and attributes with the CSS minifier
  • minify colors
  • shorten lengths and numbers and remove default px unit
  • shorten path data
  • use relative or absolute positions in path data whichever is shorter

TODO:

  • convert attributes to style attribute whenever shorter
  • merge path data? (same style and no intersection -- the latter is difficult)

Options:

  • Precision number of significant digits to preserve for numbers, 0 means no trimming

XML

The XML minifier uses these minifications:

  • strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline)
  • strip comments
  • collapse tags with no content to a void tag
  • strip CDATA sections wherever possible

Options:

  • KeepWhitespace preserve whitespace between inline tags but still collapse multiple whitespace characters into one

Usage

Any input stream is being buffered by the minification functions. This is how the underlying buffer package inherently works to ensure high performance. The output stream however is not buffered. It is wise to preallocate a buffer as big as the input to which the output is written, or otherwise use bufio to buffer to a streaming writer.

New

Retrieve a minifier struct which holds a map of mediatype → minifier functions.

m := minify.New()

The following loads all provided minifiers.

m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
m.AddFunc("image/svg+xml", svg.Minify)
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

You can set options to several minifiers.

m.Add("text/html", &html.Minifier{
	KeepDefaultAttrVals: true,
	KeepWhitespace: true,
})

From reader

Minify from an io.Reader to an io.Writer for a specific mediatype.

if err := m.Minify(mediatype, w, r); err != nil {
	panic(err)
}

From bytes

Minify from and to a []byte for a specific mediatype.

b, err = m.Bytes(mediatype, b)
if err != nil {
	panic(err)
}

From string

Minify from and to a string for a specific mediatype.

s, err = m.String(mediatype, s)
if err != nil {
	panic(err)
}

To reader

Get a minifying reader for a specific mediatype.

mr := m.Reader(mediatype, r)
if _, err := mr.Read(b); err != nil {
	panic(err)
}

To writer

Get a minifying writer for a specific mediatype. Must be explicitly closed because it uses an io.Pipe underneath.

mw := m.Writer(mediatype, w)
if mw.Write([]byte("input")); err != nil {
	panic(err)
}
if err := mw.Close(); err != nil {
	panic(err)
}

Middleware

Minify resources on the fly using middleware. It passes a wrapped response writer to the handler that removes the Content-Length header. The minifier is chosen based on the Content-Type header or, if the header is empty, by the request URI file extension. This is on-the-fly processing, you should preferably cache the results though!

fs := http.FileServer(http.Dir("www/"))
http.Handle("/", m.Middleware(fs))

Custom minifier

Add a minifier for a specific mimetype.

type CustomMinifier struct {
	KeepLineBreaks bool
}

func (c *CustomMinifier) Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
	// ...
	return nil
}

m.Add(mimetype, &CustomMinifier{KeepLineBreaks: true})
// or
m.AddRegexp(regexp.MustCompile("/x-custom$"), &CustomMinifier{KeepLineBreaks: true})

Add a minify function for a specific mimetype.

m.AddFunc(mimetype, func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
	// ...
	return nil
})
m.AddFuncRegexp(regexp.MustCompile("/x-custom$"), func(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
	// ...
	return nil
})

Add a command cmd with arguments args for a specific mimetype.

m.AddCmd(mimetype, exec.Command(cmd, args...))
m.AddCmdRegexp(regexp.MustCompile("/x-custom$"), exec.Command(cmd, args...))

Mediatypes

Using the params map[string]string argument one can pass parameters to the minifier such as seen in mediatypes (type/subtype; key1=val2; key2=val2). Examples are the encoding or charset of the data. Calling Minify will split the mimetype and parameters for the minifiers for you, but MinifyMimetype can be used if you already have them split up.

Minifiers can also be added using a regular expression. For example a minifier with image/.* will match any image mime.

Examples

Common minifiers

Basic example that minifies from stdin to stdout and loads the default HTML, CSS and JS minifiers. Optionally, one can enable java -jar build/compiler.jar to run for JS (for example the ClosureCompiler). Note that reading the file into a buffer first and writing to a pre-allocated buffer would be faster (but would disable streaming).

package main

import (
	"log"
	"os"
	"os/exec"

	"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"
)

func main() {
	m := minify.New()
	m.AddFunc("text/css", css.Minify)
	m.AddFunc("text/html", html.Minify)
	m.AddFunc("image/svg+xml", svg.Minify)
	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

	if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {
		panic(err)
	}
}

External minifiers

Below are some examples of using common external minifiers.

Closure Compiler

See Closure Compiler Application. Not tested.

m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"),
    exec.Command("java", "-jar", "build/compiler.jar"))

UglifyJS

See UglifyJS.

m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"),
    exec.Command("uglifyjs"))

esbuild

See esbuild.

m.AddCmdRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"),
    exec.Command("esbuild", "$in.js", "--minify", "--outfile=$out.js"))

Custom minifier

Custom minifier showing an example that implements the minifier function interface. Within a custom minifier, it is possible to call any minifier function (through m minify.Minifier) recursively when dealing with embedded resources.

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"strings"

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

func main() {
	m := minify.New()
	m.AddFunc("text/plain", func(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
		// remove 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.
}

ResponseWriter

Middleware

func main() {
	m := minify.New()
	m.AddFunc("text/css", css.Minify)
	m.AddFunc("text/html", html.Minify)
	m.AddFunc("image/svg+xml", svg.Minify)
	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

	fs := http.FileServer(http.Dir("www/"))
	http.Handle("/", m.MiddlewareWithError(fs))
}

func handleError(w http.ResponseWriter, r *http.Request, err error) {
    http.Error(w, err.Error(), http.StatusInternalServerError)
}

In order to properly handle minify errors, it is necessary to close the response writer since all writes are concurrently handled. There is no need to check errors on writes since they will be returned on closing.

func main() {
	m := minify.New()
	m.AddFunc("text/html", html.Minify)
	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)

	input := `<script>const i = 1_000_</script>` // Faulty JS
	req := httptest.NewRequest(http.MethodGet, "/", nil)
	rec := httptest.NewRecorder()
	m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		_, _ = w.Write([]byte(input))

		if err = w.(io.Closer).Close(); err != nil {
			panic(err)
		}
	})).ServeHTTP(rec, req)
}

ResponseWriter

func Serve(w http.ResponseWriter, r *http.Request) {
	mw := m.ResponseWriter(w, r)
	defer mw.Close()
	w = mw

	http.ServeFile(w, r, path.Join("www", r.URL.Path))
}

Custom response writer

ResponseWriter example which returns a ResponseWriter that minifies the content and then writes to the original ResponseWriter. Any write after applying this filter will be minified.

type MinifyResponseWriter struct {
	http.ResponseWriter
	io.WriteCloser
}

func (m MinifyResponseWriter) Write(b []byte) (int, error) {
	return m.WriteCloser.Write(b)
}

// MinifyResponseWriter must be closed explicitly by calling site.
func MinifyFilter(mediatype string, res http.ResponseWriter) MinifyResponseWriter {
	m := minify.New()
	// add minfiers

	mw := m.Writer(mediatype, res)
	return MinifyResponseWriter{res, mw}
}
// Usage
func(w http.ResponseWriter, req *http.Request) {
	w = MinifyFilter("text/html", w)
	if _, err := io.WriteString(w, "<p class="message"> This HTTP response will be minified. </p>"); err != nil {
		panic(err)
	}
	if err := w.Close(); err != nil {
		panic(err)
	}
	// Output: <p class=message>This HTTP response will be minified.
}

Templates

Here's an example of a replacement for template.ParseFiles from template/html, which automatically minifies each template before parsing it.

Be aware that minifying templates will work in most cases but not all. Because the HTML minifier only works for valid HTML5, your template must be valid HTML5 of itself. Template tags are parsed as regular text by the minifier.

func compileTemplates(filenames ...string) (*template.Template, error) {
	m := minify.New()
	m.AddFunc("text/html", html.Minify)

	var tmpl *template.Template
	for _, filename := range filenames {
		name := filepath.Base(filename)
		if tmpl == nil {
			tmpl = template.New(name)
		} else {
			tmpl = tmpl.New(name)
		}

		b, err := ioutil.ReadFile(filename)
		if err != nil {
			return nil, err
		}

		mb, err := m.Bytes("text/html", b)
		if err != nil {
			return nil, err
		}
		tmpl.Parse(string(mb))
	}
	return tmpl, nil
}

Example usage:

templates := template.Must(compileTemplates("view.html", "home.html"))

FAQ

Newlines remain in minified output

While you might expect the minified output to be on a single line for it to be fully minified, this is not true. In many cases, using a literal newline doesn't affect the file size, and in some cases it may even reduce the file size.

A typical example is HTML. Whitespace is significant in HTML, meaning that spaces and newlines between or around tags may affect how they are displayed. There is no distinction between a space or a newline and they may be interchanged without affecting the displayed HTML. Remember that a space (0x20) and a newline (0x0A) are both one byte long, so that there is no difference in file size when interchanging them. This minifier removes unnecessary whitespace by replacing stretches of spaces and newlines by a single whitespace character. Specifically, if the stretch of white space characters contains a newline, it will replace it by a newline and otherwise by a space. This doesn't affect the file size, but may help somewhat for debugging or file transmission objectives.

Another example is JavaScript. Single or double quoted string literals may not contain newline characters but instead need to escape them as \n. These are two bytes instead of a single newline byte. Using template literals it is allowed to have literal newline characters and we can use that fact to shave-off one byte! The result is that the minified output contains newlines instead of escaped newline characters, which makes the final file size smaller. Of course, changing from single or double quotes to template literals depends on other factors as well, and this minifier makes a calculation whether the template literal results in a shorter file size or not before converting a string literal.

License

Released under the MIT license.

minify's People

Contributors

adamkorcz avatar adtac avatar akshgpt7 avatar alasdairsmith avatar alexander-zierhut avatar anthonyfok avatar cooloppo avatar davidkorczynski avatar dchenk avatar dchest avatar dependabot[bot] avatar earthboundkid avatar edwardbetts avatar iris-ersc avatar jmooring avatar jontonsoup4 avatar jtatum avatar kalch avatar keevan avatar keftcha avatar mmorel-35 avatar moorereason avatar perrin4869 avatar pydsigner avatar rreverser avatar salvatorepreviti avatar tdewolff avatar theunrepentantgeek avatar variadico avatar xliiauo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minify's Issues

HTML: better support for template syntax

Hi,

I just noticed that if I have a "Type" field in my data struct and use minify for my go template files, it lowercases "Type".

So this:

<option value="0" {{ if eq .Type 0 }}selected{{ end }}>Foo</option>

Becomes this:

<option value=0 {{ if eq .type 0 }}selected{{ end }}>Foo</option>

And it seems like it's not possible to use text/template and minify for a javascript file together, does it? I know, that's probably quite an edge case :)

EDIT: Just tested this, doesn't work unfortunately, but I could still use minify as a package and minify the generates JS, so that's not too much of a problem for my case.

SVG: renders differently after minification

View sample.svg and sample.min.svg from this input. Both Chrome and Firefox have the minified SVG missing the outer frame. Simplifying the dot input seems to hide the problem.

sample.dot:

digraph layout {
  sample [shape=record, label="a|{b|{c|{d|{{e|{f|g}}|{h|{i|l|m|n|o|p}}}}}}"];
}

m.go:

package main

import (
    "log"
    "os"
    "regexp"

    "github.com/tdewolff/minify"
    "github.com/tdewolff/minify/css"
    "github.com/tdewolff/minify/html"
    "github.com/tdewolff/minify/js"
    "github.com/tdewolff/minify/json"
    "github.com/tdewolff/minify/svg"
    "github.com/tdewolff/minify/xml"
)

func main() {
    m := minify.New()

    m.AddFunc("text/css", css.Minify)
    m.AddFunc("text/html", html.Minify)
    m.AddFunc("text/javascript", js.Minify)
    m.AddFunc("image/svg+xml", svg.Minify)
    m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
    m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

    if err := m.Minify("image/svg+xml", os.Stdout, os.Stdin); err != nil {
        log.Fatal(err)
    }
}

Run:

$ dot -Tsvg sample.dot -o sample.svg && go run m.go <sample.svg >sample.min.svg

(dot as in graphviz dot; generated sample.svg included below)

Screenshot of viewing sample.svg:
image

Screenshot of viewing sample.min.svg:
image

sample.svg generated by dot:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.36.0 (20140111.2315)
 -->
<!-- Title: layout Pages: 1 -->
<svg width="234pt" height="102pt"
 viewBox="0.00 0.00 234.00 102.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 98)">
<title>layout</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-98 230,-98 230,4 -4,4"/>
<!-- sample -->
<g id="node1" class="node"><title>sample</title>
<polygon fill="none" stroke="black" points="-0.5,-1 -0.5,-93 226.5,-93 226.5,-1 -0.5,-1"/>
<text text-anchor="middle" x="11" y="-43.3" font-family="Times,serif" font-size="14.00">a</text>
<polyline fill="none" stroke="black" points="22.5,-1 22.5,-93 "/>
<text text-anchor="middle" x="124.5" y="-77.8" font-family="Times,serif" font-size="14.00">b</text>
<polyline fill="none" stroke="black" points="22.5,-70 226.5,-70 "/>
<text text-anchor="middle" x="34" y="-31.8" font-family="Times,serif" font-size="14.00">c</text>
<polyline fill="none" stroke="black" points="45.5,-1 45.5,-70 "/>
<text text-anchor="middle" x="136" y="-54.8" font-family="Times,serif" font-size="14.00">d</text>
<polyline fill="none" stroke="black" points="45.5,-47 226.5,-47 "/>
<text text-anchor="middle" x="67.5" y="-31.8" font-family="Times,serif" font-size="14.00">e</text>
<polyline fill="none" stroke="black" points="45.5,-24 89.5,-24 "/>
<text text-anchor="middle" x="56" y="-8.8" font-family="Times,serif" font-size="14.00">f</text>
<polyline fill="none" stroke="black" points="66.5,-1 66.5,-24 "/>
<text text-anchor="middle" x="78" y="-8.8" font-family="Times,serif" font-size="14.00">g</text>
<polyline fill="none" stroke="black" points="89.5,-1 89.5,-47 "/>
<text text-anchor="middle" x="158" y="-31.8" font-family="Times,serif" font-size="14.00">h</text>
<polyline fill="none" stroke="black" points="89.5,-24 226.5,-24 "/>
<text text-anchor="middle" x="99.5" y="-8.8" font-family="Times,serif" font-size="14.00">i</text>
<polyline fill="none" stroke="black" points="109.5,-1 109.5,-24 "/>
<text text-anchor="middle" x="119.5" y="-8.8" font-family="Times,serif" font-size="14.00">l</text>
<polyline fill="none" stroke="black" points="129.5,-1 129.5,-24 "/>
<text text-anchor="middle" x="143.5" y="-8.8" font-family="Times,serif" font-size="14.00">m</text>
<polyline fill="none" stroke="black" points="157.5,-1 157.5,-24 "/>
<text text-anchor="middle" x="169" y="-8.8" font-family="Times,serif" font-size="14.00">n</text>
<polyline fill="none" stroke="black" points="180.5,-1 180.5,-24 "/>
<text text-anchor="middle" x="192" y="-8.8" font-family="Times,serif" font-size="14.00">o</text>
<polyline fill="none" stroke="black" points="203.5,-1 203.5,-24 "/>
<text text-anchor="middle" x="215" y="-8.8" font-family="Times,serif" font-size="14.00">p</text>
</g>
</g>
</svg>

sample.min.svg:

<svg width="234pt" height="102pt" viewBox="0 0 234 102" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 98)"><title>layout</title><polygon fill="#fff" stroke="none" points="-4,4 -4,-98 230,-98 230,4 -4,4"/><g id="node1" class="node"><title>sample</title><polygon fill="none" stroke="#000" points="--.5,-1 -0.5,-93 226.5,-93 226.5,-1 -0.5,-1"/><text text-anchor="middle" x="11" y="-43.3" font-family="Times,serif" font-size="14">a</text><polyline fill="none" stroke="#000" points="22.5,-1 22.5,-93"/><text text-anchor="middle" x="124.5" y="-77.8" font-family="Times,serif" font-size="14">b</text><polyline fill="none" stroke="#000" points="22.5,-70 226.5,-70"/><text text-anchor="middle" x="34" y="-31.8" font-family="Times,serif" font-size="14">c</text><polyline fill="none" stroke="#000" points="45.5,-1 45.5,-70"/><text text-anchor="middle" x="136" y="-54.8" font-family="Times,serif" font-size="14">d</text><polyline fill="none" stroke="#000" points="45.5,-47 226.5,-47"/><text text-anchor="middle" x="67.5" y="-31.8" font-family="Times,serif" font-size="14">e</text><polyline fill="none" stroke="#000" points="45.5,-24 89.5,-24"/><text text-anchor="middle" x="56" y="-8.8" font-family="Times,serif" font-size="14">f</text><polyline fill="none" stroke="#000" points="66.5,-1 66.5,-24"/><text text-anchor="middle" x="78" y="-8.8" font-family="Times,serif" font-size="14">g</text><polyline fill="none" stroke="#000" points="89.5,-1 89.5,-47"/><text text-anchor="middle" x="158" y="-31.8" font-family="Times,serif" font-size="14">h</text><polyline fill="none" stroke="#000" points="89.5,-24 226.5,-24"/><text text-anchor="middle" x="99.5" y="-8.8" font-family="Times,serif" font-size="14">i</text><polyline fill="none" stroke="#000" points="109.5,-1 109.5,-24"/><text text-anchor="middle" x="119.5" y="-8.8" font-family="Times,serif" font-size="14">l</text><polyline fill="none" stroke="#000" points="129.5,-1 129.5,-24"/><text text-anchor="middle" x="143.5" y="-8.8" font-family="Times,serif" font-size="14">m</text><polyline fill="none" stroke="#000" points="157.5,-1 157.5,-24"/><text text-anchor="middle" x="169" y="-8.8" font-family="Times,serif" font-size="14">n</text><polyline fill="none" stroke="#000" points="180.5,-1 180.5,-24"/><text text-anchor="middle" x="192" y="-8.8" font-family="Times,serif" font-size="14">o</text><polyline fill="none" stroke="#000" points="203.5,-1 203.5,-24"/><text text-anchor="middle" x="215" y="-8.8" font-family="Times,serif" font-size="14">p</text></g></g></svg>

Experiment with concurrency

Try to run the lexer and parser concurrently. This means all data must be copied and the channels give overhead, so this might or might not give an actual speed up. Make sure to set GOMAXPROCS.

BUG: HTML removed html tags

minify seems to remove html tags.

# Input HTML
h := `
    <html><body>
    <p>  this is a hello world.  </p>
    </body></html>
`
# Output
b, _ := minify.Bytes(m, "text/html", buf.Bytes())
println("MINIFY: [" + string(b) + "]")

MINIFY: [<p>this is a hello world.]

Also tried on the "PI 2 online live demo" site, the result is the same.

CSS Invalid Property Value

Got a CSS Invalid Proprty Value error on Chrome after minifying:

Minify result z-index: 1e3;
original value 1000.

cmd seems could not build

github.com/tdewolff/minify/svg

../svg/table.go:19: undefined: svg.Fill
../svg/table.go:20: undefined: svg.Stroke
../svg/table.go:21: undefined: svg.Stop_Color
../svg/table.go:22: undefined: svg.Flood_Color
../svg/table.go:23: undefined: svg.Lighting_Color
FAIL github.com/tdewolff/minify/html [build failed]

Typo in README.md?

m.Add("text/js", css.Minify)

I think it should be

m.Add("text/js", js.Minify)

?

Minify within CDATA sections

Text inside CDATA is treated as text and not as HTML if type equals text/html. It is not properly minified.

For an example see the bottom of bbc.com

Just question about the minify tool

This isn't an issue, more of a question. The minify tool looks cool, but what do you think about making it more like go build?

For example, let's say I have a folder like this.

$ tree .
css
├── foo.css
├── bar.css
├── fizz.css
└── buzz.css

What if I could use the minify tool to "build" or "compile"—but really to minify—the files in that folder. So you could say something like minify . and then all of your CSS files would be minified.

$ minify .
$ tree
css
├── foo.css
├── foo.min.css
├── bar.css
├── bar.min.css
├── fizz.css
├── fizz.min.css
├── buzz.css
└── buzz.min.css

Then, each time you call minify ., the .min.css files get overwritten with the latest version of the non-minified version.

Does that sound like something interesting? Or not at all? I could help with a pull request if you'd like. Otherwise, cool, just wondering. :)

doc: New() is not "the same as doing `Minify{}`"

// New returns a new Minify. It is the same as doing `Minify{}`.

// New returns a new Minify. It is the same as doing `Minify{}`.
func New() *Minify {
    return &Minify{
        map[string]Func{},
        []regexpFunc{},
    }
}

New() initializes the map. var m minify.Minify; m.AddFunc(...) panics with

panic: assignment to entry in nil map

HTML minify is breaking SVG images

Minify is transforming sibling PATH tags in parent/child of each other. For example:

<path id="x" d="M60.91,81.762 ..."></path>
<path id="y" d="M113.695,72.72 ..."></path>

Become:

<path id="x" d="M60.91,81.762 ...">
<path id="y" d="M113.695,72.72 ...">

Which is wrong. PATH tags are block tags.

I've tried to patch the code by adding the PATH tag to the html/table.go blockTagMap var but no luck, the end result was the same.

Thanks!

Omitting more HTML tags

Figure out more (close) tags that can be omitted, for example close tags before the end of the document. Or before closing parents.

Speed up minification

Minification of wikipedia takes 50ms which is quite long, identify bottleneck and improve performance.

JS: semicolon syntax errors in output

Running JQueryUI v1.11.4 file through the JS minifier using the latest code from master generates a file with syntax errors. Here is a gist with the file before and after being run through the minifier:

https://gist.github.com/nwidger/616248f0c8bdd7d7a513

While not the only error, this is the first error that Firefox spits out:

SyntaxError: expected expression, got ')' jquery-ui.min.js:1:73117

It looks to me like the semicolon removal algorithm is a little too greedy. A for loop goes from looking like this:

for(g=1,v=y;;){...}

to this:

for(g=1,v=y;){...}

Trying to evaluate this code in Firefox's JS console gives me a syntax error:

for(g=1,v=y;){}
SyntaxError: expected expression, got ')'

Do one thing, do it well.

Hello there,

I really like the work you're doing but putting everything in a single package is not only not idiomatic Go but I believe it is generally advised against.

Is there any reason you haven't broke the CSS, HTML into their own packages and possibly provide a common interface?

I wanted to create a binding for a build system, slurp, which I am using regularly but come across this issue, so I thought I would hear what you think.

<h2> tag problem

Most strange error happened with <h2 id="site-description">.

Only on one page, they minified as

<п id=site-description>Разработка сайтов и мобильных приложений</h2>

Notice п instead of h2

This tag located in header, that block have same content on whole site.
But that bug appears only on one concrete page.

Site contents is cyrillic in UTF-8, generated by Hugo static site generator.
The generated content looks completely the same.

I will include two files, the good.html and the bad.html.
After minifation, the described problem appears in bad.

P.S. Sorry for .txt sources, github does not allow attaching a .html

Pass options to minifiers

Opinions needed, please tell me what you think

Passing options to the minifier would serve more users that have certain limitations or requirements. Options for HTML that come to mind:

  • keep default attribute values
  • keep whitespace (but collapse to one space)
  • pass current scheme (http or https) so URLs can be shortened better
  • pass the template delimiters to support HTML templates, i.e. '{{' and '}}'

There would be several ways to implement this. Would some or all of these settings be set on the encompassing Minifier level (all minifiers), would the settings be set on a minifier function level, or would the settings be set on a mimetype level?

Some settings are on the mimetype level: encoding of the mimetype (i.e. base64) and whether the CSS is inline or a stylesheet. These differ per source and are passed to the minifier function but not passed down to embedded resources. These are embedded in the mediatype now (i.e. text/css;inline which is used for <p style="color:#ff0;"> which is different from regular CSS stylesheets).

The options mentioned before would apply to a minifier function. One would pass options to AddFunc or derivatives. As shown below this happens in analogy to the Go stdlib HTTP handlers. You can pass the function or the struct (context, in this case options) containing the function. But this comes at a price: the API will be changed for most exported functions, invalidating currently working code.

Example of new code:

m := minify.New()

// add with options
m.Add("text/html", &html.Minifier{DefaultAttrVal: true})

// or add function directly (default options)
m.AddFunc("text/html", html.Minify)

// use minifier based on mimetype
if err := m.Minify(w, r, "text/html", map[string]string{"charset": "UTF-8"}); err != nil {
    panic(err)
}
// or use minifier directly
if err := html.Minify(m, w, r, map[string]string{"charset": "UTF-8"}); err != nil {
    panic(err)
}

Note that w and r are first parameters now, to adhere the style used in the Go stdlib (output first, input second, arguments next). I got rid of the mediatype in html.Minify and others, because if you call that function directly the mimetype is obvious/uninteresting.

All Minify functions have different signatures now! And AddRegexp has been renamed to AddPattern.

See PR #56

Rewriting attribute selectors is lossy

Using div[id=a] instead of div#a is a valid technique for matching an id with lower specificity than the id selector.

Using div[class=a] is not the same as div.a. The former matches a div with only class a and no others.

This minification is not safe. It changes the semantics of the rules.

Incorrect minification after quote within comment

From moment.js 2.8.4 line 842

            for (i = 0; i < 12; i++) {
                // make the regex if we don't have it already
                mom = moment.utc([2000, i]);

result:

for (i = 0; i < 12; i++) {
                //make the regex if we don;(function(root,factory){if(typeof define==='function'&&define.amd){define(['moment','jquery','exports'],function(momentjs,$,exports){root.daterangepicker=factory(root,exports,momentjs,$);});}else if(typeof exports!=='undefined'){var momentjs=require('moment');var jQuery;try{jQuery=require('jquery');}catch(err){jQuery=window.jQuery;if(!jQuery)throw new Error('jQuery dependency not found');}
factory(root,exports,momentjs,jQuery);}else

comment should be removed..

HTML: rewrite entities

Entities such as [ampersand]amp; can be rewritten by [ampersand] is some cases, other entities might behave similarly.

JS: minify better; write a parser

Write a JS parser to enable:

Still keep the current fast JSMin-inspired version?

It would be awesome if you'd consider donating through Patreon or PayPal so that I can free up time to implement a much improved JS minifier. I don't think FOSS will ever earn me even a coffee haha!

CSS minification messes up descendant selectors

Basically I'm using purecss in a project and I need to redefine the font family as this section of their doc explains.

The issue is that this

html, button, input, select, textarea, .pure-g [class*="pure-u"] {
  font-family: 'Fira Sans', 'Helvetica Neue', 'Helvetica', 'Arial', 'sans-serif'; 
}

gets turned to that

html,button,input,select,textarea,.pure-g[class*=pure-u]{font-family:fira sans,helvetica neue,helvetica,arial,'sans-serif'}

And in the CSS world .pure-g[class*=pure-u] is not the same as .pure-g [class*=pure-u].

WebKit/Blink/Gecko visual testing of minifiers

Add visual testing of minifiers by using WebKit/Blink/Gecko browser engines. Render the original and minified website and compare the output; outputs must be equal!

Can even build a web crawler to continuously check whether any website breaks by the minifiers.

JS/CSS: source maps

Not sure if it's on your road map but source maps would be nice.

Competing tools already support them:

Personally I don't care so much about CSS source maps, but its pretty vital for Javascript debugging. Unfortunately source map generation can be tricky, especially when merging multiple files or going through several layers. (coffescript -> javascript -> merged javascript -> compressed javascript)

cmd tool not work

after last updates 'cmd/minify' stop working, they does nothing.

just return same files without minification

Minify numbers better

Numbers with exponents are not properly minified.
Additionally, using exponents to shorten numbers should be used.

Inkscape SVGs keep <metadata> ending tags (and more)

screen shot 2015-09-06 at 04 13 40

The starting tags have been removed, but some ending tags still remain. The offending original markup is this:

<metadata
   id="metadata4166">
  <rdf:RDF>
    <cc:Work
       rdf:about="">
      <dc:format>image/svg+xml</dc:format>
      <dc:type
         rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      <dc:title />
    </cc:Work>
  </rdf:RDF>
</metadata>

(Your work is very much appreciated - keep it up)

HTML: removing leading whitespace can cause issues (even when done carefully)

I ran into a confusing issue with the HTML minifier recently having to do with the (careful) stripping of leading whitespace.

My HTML looked like this:

<p><span>Please <span id="verb"></span><span> to the store.</span></p>

And then I had JS fill in the missing value on page load, e.g. document.getElementById('verb').innerText='walk';

The HTML minifier does a very careful job of tracking whether the last non-empty element had trailing whitespace, and if it does, then it removes the leading whitespace from the next element. I've dabbled in HTML minification before a little without ever realizing that this could potentially be safe, so I was impressed, and I think it's really clever.

But alas, if the minifier doesn't or can't know the actual contents of all the elements, then it's not safe. Because "verb" is only filled in after-the-fact, it strips out the leading whitespace before "to the store", resulting in the user seeing something like this:

Please walkto the store.

For reference, I fixed this (by removing this optimization) on my own branch at tillberg@d6c88a2.

(side note: maybe it would be neat to package this and a couple of the other not-100%-safe optimizations under an optional html.MinifyDangerous method? most users could then benefit easily from a really fantastic implementation of basic HTML optimizations, while users seeking to cut every last possible byte could carefully apply the dangerous ones.)

Streaming to external tools?

Leave the stdin/stdout connection open to enhance speed and compression (using ClosureCompiler, providing more data allows it to make more assumptions).

This is really difficult because we write results to the writer immediately. The external tool must support this and then still it's hard to implement...

GZip Support

it would be great that you also have GZip Support for Assets, we then wouldn't need any of Grunt, Gulp for our static projects.

Incorrect minification after RegEx

From bootstrap.js 3.3.1 line 1402

      var autoToken = /\s?auto?\s?/i
      var autoPlace = autoToken.test(placement)

resulting minification:

var autoToken=/\s?auto?\s?/ivar autoPlace=autoToken.test(placement)

it should be:

var autoToken=/\s?auto?\s?/i;var autoPlace=autoToken.test(placement)

Change minify settings

It would be nice to tell the minifier, which features we want, and which we don't.

For example, the feature 'strip default attribute values and attribute boolean values' (HTML Minifier) breaks compatibility with the input[type=text] fields when using Foundation. It would be nice to be able to say: StripDefaultValues = false or something similar.

Somewhat slow

Minification time of HTML files has doubled somehow throughout the commits today, need profiling.

HTML: don't remove http(s) from links

I'm minifing HTML for a site that's available via HTTPS, but there's a link pointing to a site not supporting HTTPS. Minify removes "http:" from links like this:

> echo '<a href="http://kbct.de/">foo</a>' | minify -x .html
<a href=//kbct.de/>foo</a>

Now that link adopts the protocol from the page it's on, in my case HTTPS, which breaks the link.

CSS background none -> 0 is not valid

The current:

background: none repeat scroll 0 0;

Will be minified to:

background:0 repeat scroll 0 0;

Which is incorrect... It should be background: none and not zero since that will have different results.

CSS hsl() to rgb

See if hsl() colors can be rewritten smaller in #rrggbb format

Basic JS minifier

Quick and basic JS minifier if external tools are too slow for your application

CLI improvements

  • Treat directories as an input source and not as a single argument. Pass them like files in the [input] array so you can have multiple input directories.
  • Add -q quiet to suppress anything send to stdout and stderr except the (minified) file
  • Add -v verbose to print to stderr the actions it does and minification stats.\
  • Better errors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.