jorge/markup/minify.go
Facundo Olano 7bc5baa253
Minify exclusions (#31)
* add minify exclusions support

* load exclusions

* fix bug

* load minify

* fix yaml type coersion

* skip dot files when building
2024-03-15 19:55:50 -03:00

44 lines
1 KiB
Go

package markup
import (
"io"
"path/filepath"
"slices"
"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/xml"
)
var SUPPORTED_MINIFIERS = []string{".css", ".html", ".js", ".xml"}
type Minifier struct {
minifier *minify.M
exclusions []string
}
func LoadMinifier(exclusions []string) Minifier {
minifier := minify.New()
minifier.AddFunc(".css", css.Minify)
minifier.AddFunc(".html", html.Minify)
minifier.AddFunc(".js", js.Minify)
minifier.AddFunc(".xml", xml.Minify)
return Minifier{minifier, exclusions}
}
func (m *Minifier) Minify(path string, contentReader io.Reader) io.Reader {
for _, exclusion := range m.exclusions {
if matched, _ := filepath.Match(exclusion, path); matched {
return contentReader
}
}
extension := filepath.Ext(path)
if !slices.Contains(SUPPORTED_MINIFIERS, extension) {
return contentReader
}
return m.minifier.Reader(extension, contentReader)
}