mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-25 21:58:28 +01:00
Minify exclusions (#31)
* add minify exclusions support * load exclusions * fix bug * load minify * fix yaml type coersion * skip dot files when building
This commit is contained in:
parent
c2c981c156
commit
7bc5baa253
3 changed files with 49 additions and 29 deletions
|
@ -31,10 +31,11 @@ type Config struct {
|
||||||
Lang string
|
Lang string
|
||||||
HighlightTheme string
|
HighlightTheme string
|
||||||
|
|
||||||
Minify bool
|
Minify bool
|
||||||
LiveReload bool
|
MinifyExclusions []string
|
||||||
LinkStatic bool
|
LiveReload bool
|
||||||
IncludeDrafts bool
|
LinkStatic bool
|
||||||
|
IncludeDrafts bool
|
||||||
|
|
||||||
ServerHost string
|
ServerHost string
|
||||||
ServerPort int
|
ServerPort int
|
||||||
|
@ -50,20 +51,21 @@ func Load(rootDir string) (*Config, error) {
|
||||||
// TODO allow to disable minify
|
// TODO allow to disable minify
|
||||||
|
|
||||||
config := &Config{
|
config := &Config{
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
SrcDir: filepath.Join(rootDir, "src"),
|
SrcDir: filepath.Join(rootDir, "src"),
|
||||||
TargetDir: filepath.Join(rootDir, "target"),
|
TargetDir: filepath.Join(rootDir, "target"),
|
||||||
LayoutsDir: filepath.Join(rootDir, "layouts"),
|
LayoutsDir: filepath.Join(rootDir, "layouts"),
|
||||||
IncludesDir: filepath.Join(rootDir, "includes"),
|
IncludesDir: filepath.Join(rootDir, "includes"),
|
||||||
DataDir: filepath.Join(rootDir, "data"),
|
DataDir: filepath.Join(rootDir, "data"),
|
||||||
PostFormat: "blog/:title.org",
|
PostFormat: "blog/:title.org",
|
||||||
Lang: "en",
|
Lang: "en",
|
||||||
HighlightTheme: "github",
|
HighlightTheme: "github",
|
||||||
Minify: true,
|
Minify: true,
|
||||||
LiveReload: false,
|
MinifyExclusions: make([]string, 0),
|
||||||
LinkStatic: false,
|
LiveReload: false,
|
||||||
IncludeDrafts: false,
|
LinkStatic: false,
|
||||||
pageDefaults: map[string]interface{}{},
|
IncludeDrafts: false,
|
||||||
|
pageDefaults: map[string]interface{}{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// load overrides from config.yml
|
// load overrides from config.yml
|
||||||
|
@ -83,18 +85,23 @@ func Load(rootDir string) (*Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// set user-provided overrides of declared config keys
|
// set user-provided overrides of declared config keys
|
||||||
// TODO less copypasty way of declaring config overrides
|
// FIXME less copypasty way of declaring config overrides
|
||||||
if url, found := config.overrides["url"]; found {
|
if url, found := config.overrides["url"]; found {
|
||||||
config.SiteUrl = url.(string)
|
config.SiteUrl = url.(string)
|
||||||
}
|
}
|
||||||
if format, found := config.overrides["post_format"]; found {
|
if format, found := config.overrides["post_format"]; found {
|
||||||
config.PostFormat = format.(string)
|
config.PostFormat = format.(string)
|
||||||
}
|
}
|
||||||
if format, found := config.overrides["lang"]; found {
|
if lang, found := config.overrides["lang"]; found {
|
||||||
config.Lang = format.(string)
|
config.Lang = lang.(string)
|
||||||
}
|
}
|
||||||
if format, found := config.overrides["highlight_theme"]; found {
|
if theme, found := config.overrides["highlight_theme"]; found {
|
||||||
config.HighlightTheme = format.(string)
|
config.HighlightTheme = theme.(string)
|
||||||
|
}
|
||||||
|
if exclusions, found := config.overrides["minify_exclusions"]; found {
|
||||||
|
for _, exclusion := range exclusions.([]interface{}) {
|
||||||
|
config.MinifyExclusions = append(config.MinifyExclusions, exclusion.(string))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
|
|
|
@ -2,6 +2,7 @@ package markup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/tdewolff/minify/v2"
|
"github.com/tdewolff/minify/v2"
|
||||||
|
@ -14,20 +15,28 @@ import (
|
||||||
var SUPPORTED_MINIFIERS = []string{".css", ".html", ".js", ".xml"}
|
var SUPPORTED_MINIFIERS = []string{".css", ".html", ".js", ".xml"}
|
||||||
|
|
||||||
type Minifier struct {
|
type Minifier struct {
|
||||||
minifier *minify.M
|
minifier *minify.M
|
||||||
|
exclusions []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadMinifier() Minifier {
|
func LoadMinifier(exclusions []string) Minifier {
|
||||||
minifier := minify.New()
|
minifier := minify.New()
|
||||||
minifier.AddFunc(".css", css.Minify)
|
minifier.AddFunc(".css", css.Minify)
|
||||||
minifier.AddFunc(".html", html.Minify)
|
minifier.AddFunc(".html", html.Minify)
|
||||||
minifier.AddFunc(".js", js.Minify)
|
minifier.AddFunc(".js", js.Minify)
|
||||||
minifier.AddFunc(".xml", xml.Minify)
|
minifier.AddFunc(".xml", xml.Minify)
|
||||||
return Minifier{minifier}
|
return Minifier{minifier, exclusions}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Minifier) Minify(extension string, contentReader io.Reader) io.Reader {
|
func (m *Minifier) Minify(path string, contentReader io.Reader) io.Reader {
|
||||||
|
|
||||||
|
for _, exclusion := range m.exclusions {
|
||||||
|
if matched, _ := filepath.Match(exclusion, path); matched {
|
||||||
|
return contentReader
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension := filepath.Ext(path)
|
||||||
if !slices.Contains(SUPPORTED_MINIFIERS, extension) {
|
if !slices.Contains(SUPPORTED_MINIFIERS, extension) {
|
||||||
return contentReader
|
return contentReader
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ func load(config config.Config) (*site, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
site.minifier = markup.LoadMinifier()
|
site.minifier = markup.LoadMinifier(config.MinifyExclusions)
|
||||||
|
|
||||||
return &site, nil
|
return &site, nil
|
||||||
}
|
}
|
||||||
|
@ -256,6 +256,10 @@ func (site *site) build() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(filepath.Base(path), ".") {
|
||||||
|
// skip dot files and directories
|
||||||
|
return nil
|
||||||
|
}
|
||||||
subpath, _ := filepath.Rel(site.config.SrcDir, path)
|
subpath, _ := filepath.Rel(site.config.SrcDir, path)
|
||||||
targetPath := filepath.Join(site.config.TargetDir, subpath)
|
targetPath := filepath.Join(site.config.TargetDir, subpath)
|
||||||
|
|
||||||
|
@ -338,7 +342,7 @@ func (site *site) buildFile(path string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if site.config.Minify {
|
if site.config.Minify {
|
||||||
contentReader = site.minifier.Minify(targetExt, contentReader)
|
contentReader = site.minifier.Minify(subpath, contentReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the file contents over to target
|
// write the file contents over to target
|
||||||
|
|
Loading…
Reference in a new issue