| 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.AllProvider) (Client, error) { |
| 78 |
conf := cfg.GetConfigSection("minify").(MinifyConfig) |
| 79 |
m := minify.New() |
| 80 |
|
| 81 |
// We use the Type definition of the media types defined in the site if found. |
| 82 |
addMinifier(m, mediaTypes, "css", getMinifier(conf, "css")) |
| 83 |
|
| 84 |
addMinifier(m, mediaTypes, "js", getMinifier(conf, "js")) |
| 85 |
m.AddRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), getMinifier(conf, "js")) |
| 86 |
|
| 87 |
addMinifier(m, mediaTypes, "json", getMinifier(conf, "json")) |
| 88 |
m.AddRegexp(regexp.MustCompile(`^(application|text)/(x-|(ld|manifest)\+)?json$`), getMinifier(conf, "json")) |
| 89 |
|
| 90 |
addMinifier(m, mediaTypes, "svg", getMinifier(conf, "svg")) |
| 91 |
|
| 92 |
addMinifier(m, mediaTypes, "xml", getMinifier(conf, "xml")) |
| 93 |
|
| 94 |
// HTML |
| 95 |
addMinifier(m, mediaTypes, "html", getMinifier(conf, "html")) |
| 96 |
for _, of := range outputFormats { |
| 97 |
if of.IsHTML { |
| 98 |
m.Add(of.MediaType.Type, getMinifier(conf, "html")) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return Client{m: m, MinifyOutput: conf.MinifyOutput}, nil |
| 103 |
} |
| 104 |
|
| 105 |
// getMinifier returns the appropriate minify.MinifierFunc for the MIME |
| 106 |
// type suffix s, given the config c. |
| 107 |
func getMinifier(c MinifyConfig, s string) minify.Minifier { |
| 108 |
switch { |
| 109 |
case s == "css" && !c.DisableCSS: |
| 110 |
return &c.Tdewolff.CSS |
| 111 |
case s == "js" && !c.DisableJS: |
| 112 |
return &c.Tdewolff.JS |
| 113 |
case s == "json" && !c.DisableJSON: |
| 114 |
return &c.Tdewolff.JSON |
| 115 |
case s == "svg" && !c.DisableSVG: |
| 116 |
return &c.Tdewolff.SVG |
| 117 |
case s == "xml" && !c.DisableXML: |
| 118 |
return &c.Tdewolff.XML |
| 119 |
case s == "html" && !c.DisableHTML: |
| 120 |
return &c.Tdewolff.HTML |
| 121 |
default: |
| 122 |
return noopMinifier{} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func addMinifier(m *minify.M, mt media.Types, suffix string, min minify.Minifier) { |
| 127 |
types := mt.BySuffix(suffix) |
| 128 |
for _, t := range types { |
| 129 |
m.Add(t.Type, min) |
| 130 |
} |
| 131 |
} |