add minifier

This commit is contained in:
facundoolano 2024-02-16 15:14:30 -03:00
parent 008954a98b
commit 663463202c
5 changed files with 50 additions and 6 deletions

4
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/niklasfasching/go-org v1.7.0
github.com/osteele/liquid v1.3.2
github.com/tdewolff/minify/v2 v2.20.16
github.com/yuin/goldmark v1.7.0
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
gopkg.in/yaml.v2 v2.4.0
@ -15,5 +16,6 @@ require (
require (
github.com/osteele/tuesday v1.0.3 // indirect
golang.org/x/sys v0.4.0 // indirect
github.com/tdewolff/parse/v2 v2.7.11 // indirect
golang.org/x/sys v0.16.0 // indirect
)

11
go.sum
View file

@ -14,13 +14,20 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tdewolff/minify/v2 v2.20.16 h1:/C8dtRkxLTIyUlKlBz46gDiktCrE8a6+c1gTrnPFz+U=
github.com/tdewolff/minify/v2 v2.20.16/go.mod h1:/FvxV9KaTrFu35J9I2FhRvWSBxcHj8sDSdwBFh5voxM=
github.com/tdewolff/parse/v2 v2.7.11 h1:v+W45LnzmjndVlfqPCT5gGjAAZKd1GJGOPJveTIkBY8=
github.com/tdewolff/parse/v2 v2.7.11/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

32
site/minify.go Normal file
View file

@ -0,0 +1,32 @@
package site
import (
"io"
"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 = minify.M
func (site *Site) loadMinifier() {
site.minifier = *minify.New()
site.minifier.AddFunc(".css", css.Minify)
site.minifier.AddFunc(".html", html.Minify)
site.minifier.AddFunc(".js", js.Minify)
site.minifier.AddFunc(".xml", xml.Minify)
}
func (site *Site) minify(extension string, contentReader io.Reader) io.Reader {
if !site.Config.Minify || !slices.Contains(SUPPORTED_MINIFIERS, extension) {
return contentReader
}
return site.minifier.Reader(extension, contentReader)
}

View file

@ -29,6 +29,8 @@ type Site struct {
templateEngine *templates.Engine
templates map[string]*templates.Template
minifier Minifier
}
func Load(config config.Config) (*Site, error) {
@ -53,6 +55,8 @@ func Load(config config.Config) (*Site, error) {
return nil, err
}
site.loadMinifier()
return &site, nil
}
@ -233,9 +237,7 @@ func (site *Site) Build() error {
}
// if enabled, minify web files
if site.Config.Minify && (targetExt == ".html" || targetExt == ".css" || targetExt == ".js") {
// TODO minify output
}
contentReader = site.minify(targetExt, contentReader)
// write the file contents over to target
fmt.Println("writing", targetPath)

View file

@ -315,6 +315,7 @@ func newProject() *config.Config {
os.Mkdir(dataDir, 0777)
config, _ := config.Load(projectDir)
config.Minify = false
return config
}