minifiers.go (4475B)
1 // Copyright 2018 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package minifiers contains minifiers mapped to MIME types. This package is used
15 // in both the resource transformation, i.e. resources.Minify, and in the publishing
16 // chain.
17 package minifiers
18
19 import (
20 "io"
21 "regexp"
22
23 "github.com/gohugoio/hugo/config"
24 "github.com/gohugoio/hugo/output"
25 "github.com/gohugoio/hugo/transform"
26
27 "github.com/gohugoio/hugo/media"
28 "github.com/tdewolff/minify/v2"
29 )
30
31 // Client wraps a minifier.
32 type Client struct {
33 // Whether output minification is enabled (HTML in /public)
34 MinifyOutput bool
35
36 m *minify.M
37 }
38
39 // Transformer returns a func that can be used in the transformer publishing chain.
40 // TODO(bep) minify config etc
41 func (m Client) Transformer(mediatype media.Type) transform.Transformer {
42 _, params, min := m.m.Match(mediatype.Type())
43 if min == nil {
44 // No minifier for this MIME type
45 return nil
46 }
47
48 return func(ft transform.FromTo) error {
49 // Note that the source io.Reader will already be buffered, but it implements
50 // the Bytes() method, which is recognized by the Minify library.
51 return min.Minify(m.m, ft.To(), ft.From(), params)
52 }
53 }
54
55 // Minify tries to minify the src into dst given a MIME type.
56 func (m Client) Minify(mediatype media.Type, dst io.Writer, src io.Reader) error {
57 return m.m.Minify(mediatype.Type(), dst, src)
58 }
59
60 // noopMinifier implements minify.Minifier [1], but doesn't minify content. This means
61 // that we can avoid missing minifiers for any MIME types in our minify.M, which
62 // causes minify to return errors, while still allowing minification to be
63 // disabled for specific types.
64 //
65 // [1]: https://pkg.go.dev/github.com/tdewolff/minify#Minifier
66 type noopMinifier struct{}
67
68 // Minify copies r into w without transformation.
69 func (m noopMinifier) Minify(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
70 _, err := io.Copy(w, r)
71 return err
72 }
73
74 // New creates a new Client with the provided MIME types as the mapping foundation.
75 // The HTML minifier is also registered for additional HTML types (AMP etc.) in the
76 // provided list of output formats.
77 func New(mediaTypes media.Types, outputFormats output.Formats, cfg config.Provider) (Client, error) {
78 conf, err := decodeConfig(cfg)
79
80 m := minify.New()
81 if err != nil {
82 return Client{}, err
83 }
84
85 // We use the Type definition of the media types defined in the site if found.
86 addMinifier(m, mediaTypes, "css", getMinifier(conf, "css"))
87
88 addMinifier(m, mediaTypes, "js", getMinifier(conf, "js"))
89 m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), getMinifier(conf, "js"))
90
91 addMinifier(m, mediaTypes, "json", getMinifier(conf, "json"))
92 m.AddRegexp(regexp.MustCompile(`^(application|text)/(x-|(ld|manifest)\+)?json$`), getMinifier(conf, "json"))
93
94 addMinifier(m, mediaTypes, "svg", getMinifier(conf, "svg"))
95
96 addMinifier(m, mediaTypes, "xml", getMinifier(conf, "xml"))
97
98 // HTML
99 addMinifier(m, mediaTypes, "html", getMinifier(conf, "html"))
100 for _, of := range outputFormats {
101 if of.IsHTML {
102 m.Add(of.MediaType.Type(), getMinifier(conf, "html"))
103 }
104 }
105
106 return Client{m: m, MinifyOutput: conf.MinifyOutput}, nil
107 }
108
109 // getMinifier returns the appropriate minify.MinifierFunc for the MIME
110 // type suffix s, given the config c.
111 func getMinifier(c minifyConfig, s string) minify.Minifier {
112 switch {
113 case s == "css" && !c.DisableCSS:
114 return &c.Tdewolff.CSS
115 case s == "js" && !c.DisableJS:
116 return &c.Tdewolff.JS
117 case s == "json" && !c.DisableJSON:
118 return &c.Tdewolff.JSON
119 case s == "svg" && !c.DisableSVG:
120 return &c.Tdewolff.SVG
121 case s == "xml" && !c.DisableXML:
122 return &c.Tdewolff.XML
123 case s == "html" && !c.DisableHTML:
124 return &c.Tdewolff.HTML
125 default:
126 return noopMinifier{}
127 }
128 }
129
130 func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) {
131 types := mt.BySuffix(suffix)
132 for _, t := range types {
133 m.Add(t.Type(), min)
134 }
135 }