Add minify config

Fixes #6750
Updates #6892
This commit is contained in:
SatowTakeshi
2020-02-29 18:44:05 +09:00
committed by Bjørn Erik Pedersen
parent 99958f90fe
commit 574c2959b8
12 changed files with 346 additions and 47 deletions

View File

@@ -29,8 +29,12 @@ type Client struct {
// New creates a new Client given a specification. Note that it is the media types
// configured for the site that is used to match files to the correct minifier.
func New(rs *resources.Spec) *Client {
return &Client{rs: rs, m: minifiers.New(rs.MediaTypes, rs.OutputFormats)}
func New(rs *resources.Spec) (*Client, error) {
m, err := minifiers.New(rs.MediaTypes, rs.OutputFormats, rs.Cfg)
if err != nil {
return nil, err
}
return &Client{rs: rs, m: m}, nil
}
type minifyTransformation struct {
@@ -43,9 +47,7 @@ func (t *minifyTransformation) Key() internal.ResourceTransformationKey {
}
func (t *minifyTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
if err := t.m.Minify(ctx.InMediaType, ctx.To, ctx.From); err != nil {
return err
}
_ = t.m.Minify(ctx.InMediaType, ctx.To, ctx.From)
ctx.AddOutPathIdentifier(".min")
return nil
}

View File

@@ -27,7 +27,7 @@ func TestTransform(t *testing.T) {
spec, err := htesting.NewTestResourceSpec()
c.Assert(err, qt.IsNil)
client := New(spec)
client, _ := New(spec)
r, err := htesting.NewResourceTransformerForSpec(spec, "hugo.html", "<h1> Hugo Rocks! </h1>")
c.Assert(err, qt.IsNil)
@@ -41,3 +41,23 @@ func TestTransform(t *testing.T) {
c.Assert(content, qt.Equals, "<h1>Hugo Rocks!</h1>")
}
func TestNoMinifier(t *testing.T) {
c := qt.New(t)
spec, _ := htesting.NewTestResourceSpec()
spec.Cfg.Set("minifiers.enableXML", false)
client, _ := New(spec)
original := "<title> Hugo Rocks! </title>"
r, err := htesting.NewResourceTransformerForSpec(spec, "hugo.xml", original)
c.Assert(err, qt.IsNil)
transformed, err := client.Minify(r)
c.Assert(err, qt.IsNil)
content, err := transformed.(resource.ContentProvider).Content()
// error should be ignored because general users cannot control codes under `theme`s
c.Assert(err, qt.IsNil)
c.Assert(content, qt.Equals, original)
}