From e9ee82e764edc02776d8cccc4476b049e1fe0290 Mon Sep 17 00:00:00 2001 From: Dmitry Kolosov Date: Wed, 4 Nov 2020 19:46:19 +0300 Subject: [PATCH] Fingerprint and minify all resources, move custom css and js to assets dir --- README.md | 4 ++-- layouts/partials/head.html | 12 +++--------- layouts/partials/resource.html | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 layouts/partials/resource.html diff --git a/README.md b/README.md index 30f9673..1e5df98 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,9 @@ params: images: - images/og-featured.png # relative path to "static" directory customCSS: - - css/my.css # relative path to "static" directory + - css/my.css # relative path to "assets" or "static" directory customJS: - - js/main.js # relative path to "static" directory + - js/main.js # relative path to "assets" or "static" directory dateFormat: "2006-01-02" paginationSinglePost: true style: light-without-switcher diff --git a/layouts/partials/head.html b/layouts/partials/head.html index bc8bcf0..8975477 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -14,20 +14,14 @@ -{{ $cssTemplate := resources.Get "css/main.css" }} -{{ $style := $cssTemplate | resources.ExecuteAsTemplate "css/style.css" . | resources.ToCSS }} -{{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} - {{ $style = $style | resources.Minify }} -{{ end }} -{{ $style = $style | resources.Fingerprint "sha256" }} - +{{ partial "resource.html" (dict "context" . "type" "css" "filename" "css/main.css") }} {{ range .Site.Params.customCSS -}} - + {{ partial "resource.html" (dict "context" $ "type" "css" "filename" . ) }} {{- end }} {{ range .Site.Params.customJS -}} - + {{ partial "resource.html" (dict "context" $ "type" "js" "filename" . ) }} {{- end }} {{ template "_internal/opengraph.html" . }} diff --git a/layouts/partials/resource.html b/layouts/partials/resource.html new file mode 100644 index 0000000..146c902 --- /dev/null +++ b/layouts/partials/resource.html @@ -0,0 +1,29 @@ +{{ $targetFilename := "file" }} +{{ if eq .type "css" }} + {{ $targetFilename = "css/style.css"}} +{{ else if eq .type "js" }} + {{ $targetFilename = "js/script.js"}} +{{ end }} + +{{ $resource := resources.Get .filename }} + +{{ if $resource }} + {{ $resource := $resource | resources.ExecuteAsTemplate $targetFilename . }} + {{ if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} + {{ $resource = $resource | resources.Minify }} + {{ end }} + {{ $resource = $resource | resources.Fingerprint "sha256" }} + + {{ if eq .type "css" }} + + {{ else if eq .type "js" }} + + {{ end }} + +{{ else }} + {{ if eq .type "css" }} + + {{ else if eq .type "js" }} + + {{ end }} +{{ end }} \ No newline at end of file