From 98525ba201229b1062f512b0cca7a1e093c6de61 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Tue, 19 Mar 2024 08:15:07 +0200 Subject: [PATCH] Minify files on production Also: * switch to `RelPermalink` * switch to defer for JS * use a loop --- package.json | 2 +- src/layouts/partials/scripts.html | 19 +++++++++++++------ src/layouts/partials/stylesheet.html | 8 ++++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e8fdda84a..f383a9189 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "scripts": { "start": "npm run serve", - "build": "hugo --cleanDestinationDir --printPathWarnings --printUnusedTemplates", + "build": "hugo --cleanDestinationDir --minify --printPathWarnings --printUnusedTemplates", "serve": "hugo server --port 4000 --disableFastRender --noHTTPCache --renderToMemory --printPathWarnings --printUnusedTemplates", "serve-only": "npx sirv-cli _site --port 4000", "test": "npm-run-all build --parallel --continue-on-error --aggregate-output test-*", diff --git a/src/layouts/partials/scripts.html b/src/layouts/partials/scripts.html index 624da97e1..e134eab5a 100644 --- a/src/layouts/partials/scripts.html +++ b/src/layouts/partials/scripts.html @@ -5,14 +5,21 @@ {{- end -}} {{- $bootstrapJs := resources.Get "/js/vendor/bootstrap.bundle.min.js" | fingerprint | resources.Copy "/assets/js/vendor/bootstrap.bundle.min.js" -}} - + -{{- $application := resources.Get "js/application.js" | js.Build $esbuildOptions | fingerprint | resources.Copy "/assets/js/application.js" -}} -{{- $lazyload := resources.Get "js/lazyload.js" | js.Build $esbuildOptions | fingerprint | resources.Copy "/assets/js/lazyload.js" -}} +{{- $scripts := slice "js/application.js" "js/lazyload.js" -}} +{{- range $script := $scripts -}} +{{- $_script := resources.Get $script | js.Build $esbuildOptions | fingerprint -}} - - +{{- if hugo.IsProduction -}} + {{- $_script = $_script | resources.Minify -}} +{{- end -}} + +{{- $_script = $_script | resources.Copy (path.Join "/assets" $script) -}} + + +{{- end -}} {{ range .Page.Params.extra_js -}} - + {{- end -}} diff --git a/src/layouts/partials/stylesheet.html b/src/layouts/partials/stylesheet.html index fbfc9f35f..1f8ff3720 100644 --- a/src/layouts/partials/stylesheet.html +++ b/src/layouts/partials/stylesheet.html @@ -5,6 +5,10 @@ {{- $sassOptions = merge $sassOptions (dict "outputStyle" "compressed") -}} {{- end -}} -{{- $style := resources.Get "scss/style.scss" | toCSS $sassOptions | postCSS $postcssOptions | fingerprint -}} +{{- $style := resources.Get "scss/style.scss" | toCSS $sassOptions | postCSS $postcssOptions -}} +{{- if hugo.IsProduction -}} + {{- $style = $style | resources.Minify -}} +{{- end -}} +{{- $style = $style | fingerprint -}} - +