github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/minify/minify.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package minify
     4  
     5  import (
     6  	"regexp"
     7  
     8  	"../protocol"
     9  
    10  	"github.com/tdewolff/minify"
    11  	tcss "github.com/tdewolff/minify/css"
    12  	thtml "github.com/tdewolff/minify/html"
    13  	"github.com/tdewolff/minify/js"
    14  	"github.com/tdewolff/minify/json"
    15  	"github.com/tdewolff/minify/svg"
    16  	"github.com/tdewolff/minify/xml"
    17  )
    18  
    19  var minifier = minify.New()
    20  
    21  func init() {
    22  	minifier.AddFunc("text/css", tcss.Minify)
    23  	minifier.AddFunc("text/html", thtml.Minify)
    24  	minifier.AddFunc("image/svg+xml", svg.Minify)
    25  	minifier.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
    26  	minifier.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
    27  	minifier.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
    28  }
    29  
    30  // Minify replace file data with minify of them.
    31  func Minify(data protocol.Codec) (err protocol.Error) {
    32  	var minifiedData, goErr = minifier.Bytes(data.MediaType().MediaType(), data.Marshal())
    33  	if goErr != nil {
    34  		return
    35  	}
    36  	err = data.Unmarshal(minifiedData)
    37  	return
    38  }