github.com/robinwassen/hugo@v0.47.1/minifiers/minifiers.go (about)

     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/output"
    24  	"github.com/gohugoio/hugo/transform"
    25  
    26  	"github.com/gohugoio/hugo/media"
    27  	"github.com/tdewolff/minify"
    28  	"github.com/tdewolff/minify/css"
    29  	"github.com/tdewolff/minify/html"
    30  	"github.com/tdewolff/minify/js"
    31  	"github.com/tdewolff/minify/json"
    32  	"github.com/tdewolff/minify/svg"
    33  	"github.com/tdewolff/minify/xml"
    34  )
    35  
    36  // Client wraps a minifier.
    37  type Client struct {
    38  	m *minify.M
    39  }
    40  
    41  // Transformer returns a func that can be used in the transformer publishing chain.
    42  // TODO(bep) minify config etc
    43  func (m Client) Transformer(mediatype media.Type) transform.Transformer {
    44  	_, params, min := m.m.Match(mediatype.Type())
    45  	if min == nil {
    46  		// No minifier for this MIME type
    47  		return nil
    48  	}
    49  
    50  	return func(ft transform.FromTo) error {
    51  		// Note that the source io.Reader will already be buffered, but it implements
    52  		// the Bytes() method, which is recognized by the Minify library.
    53  		return min.Minify(m.m, ft.To(), ft.From(), params)
    54  	}
    55  }
    56  
    57  // Minify tries to minify the src into dst given a MIME type.
    58  func (m Client) Minify(mediatype media.Type, dst io.Writer, src io.Reader) error {
    59  	return m.m.Minify(mediatype.Type(), dst, src)
    60  }
    61  
    62  // New creates a new Client with the provided MIME types as the mapping foundation.
    63  // The HTML minifier is also registered for additional HTML types (AMP etc.) in the
    64  // provided list of output formats.
    65  func New(mediaTypes media.Types, outputFormats output.Formats) Client {
    66  	m := minify.New()
    67  	htmlMin := &html.Minifier{
    68  		KeepDocumentTags:        true,
    69  		KeepConditionalComments: true,
    70  	}
    71  
    72  	// We use the Type definition of the media types defined in the site if found.
    73  	addMinifierFunc(m, mediaTypes, "text/css", "css", css.Minify)
    74  	addMinifierFunc(m, mediaTypes, "application/javascript", "js", js.Minify)
    75  	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
    76  	addMinifierFunc(m, mediaTypes, "application/json", "json", json.Minify)
    77  	addMinifierFunc(m, mediaTypes, "image/svg+xml", "svg", svg.Minify)
    78  	addMinifierFunc(m, mediaTypes, "application/xml", "xml", xml.Minify)
    79  	addMinifierFunc(m, mediaTypes, "application/rss", "xml", xml.Minify)
    80  
    81  	// HTML
    82  	addMinifier(m, mediaTypes, "text/html", "html", htmlMin)
    83  	for _, of := range outputFormats {
    84  		if of.IsHTML {
    85  			addMinifier(m, mediaTypes, of.MediaType.Type(), "html", htmlMin)
    86  		}
    87  	}
    88  	return Client{m: m}
    89  
    90  }
    91  
    92  func addMinifier(m *minify.M, mt media.Types, typeString, suffix string, min minify.Minifier) {
    93  	resolvedTypeStr := resolveMediaTypeString(mt, typeString, suffix)
    94  	m.Add(resolvedTypeStr, min)
    95  	if resolvedTypeStr != typeString {
    96  		m.Add(typeString, min)
    97  	}
    98  }
    99  
   100  func addMinifierFunc(m *minify.M, mt media.Types, typeString, suffix string, fn minify.MinifierFunc) {
   101  	resolvedTypeStr := resolveMediaTypeString(mt, typeString, suffix)
   102  	m.AddFunc(resolvedTypeStr, fn)
   103  	if resolvedTypeStr != typeString {
   104  		m.AddFunc(typeString, fn)
   105  	}
   106  }
   107  
   108  func resolveMediaTypeString(types media.Types, typeStr, suffix string) string {
   109  	if m, found := resolveMediaType(types, typeStr, suffix); found {
   110  		return m.Type()
   111  	}
   112  	// Fall back to the default.
   113  	return typeStr
   114  }
   115  
   116  // Make sure we match the matching pattern with what the user have actually defined
   117  // in his or hers media types configuration.
   118  func resolveMediaType(types media.Types, typeStr, suffix string) (media.Type, bool) {
   119  	if m, found := types.GetByType(typeStr); found {
   120  		return m, true
   121  	}
   122  
   123  	if m, found := types.GetFirstBySuffix(suffix); found {
   124  		return m, true
   125  	}
   126  
   127  	return media.Type{}, false
   128  
   129  }