diff --git a/go.mod b/go.mod index 32605da..47e5a03 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index c81bd77..8aa6335 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/site/minify.go b/site/minify.go new file mode 100644 index 0000000..d532adb --- /dev/null +++ b/site/minify.go @@ -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) +} diff --git a/site/site.go b/site/site.go index 6b3a7f2..c1e1b7a 100644 --- a/site/site.go +++ b/site/site.go @@ -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) diff --git a/site/site_test.go b/site/site_test.go index 6a861f8..4e7003c 100644 --- a/site/site_test.go +++ b/site/site_test.go @@ -315,6 +315,7 @@ func newProject() *config.Config { os.Mkdir(dataDir, 0777) config, _ := config.Load(projectDir) + config.Minify = false return config }