Skip to content

Commit

Permalink
Minify files on production
Browse files Browse the repository at this point in the history
Also:

* switch to `RelPermalink`
* switch to defer for JS
* use a loop
  • Loading branch information
XhmikosR committed Mar 19, 2024
1 parent e357e77 commit 98525ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-*",
Expand Down
19 changes: 13 additions & 6 deletions src/layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
{{- end -}}

{{- $bootstrapJs := resources.Get "/js/vendor/bootstrap.bundle.min.js" | fingerprint | resources.Copy "/assets/js/vendor/bootstrap.bundle.min.js" -}}
<script async src="{{ $bootstrapJs.Permalink | relURL }}"></script>
<script defer src="{{ $bootstrapJs.RelPermalink }}"></script>

{{- $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 -}}

<script async src="{{ $application.Permalink | relURL }}"></script>
<script async src="{{ $lazyload.Permalink | relURL }}"></script>
{{- if hugo.IsProduction -}}
{{- $_script = $_script | resources.Minify -}}
{{- end -}}

{{- $_script = $_script | resources.Copy (path.Join "/assets" $script) -}}

<script defer src="{{ $_script.RelPermalink }}"></script>
{{- end -}}

{{ range .Page.Params.extra_js -}}
<script{{ with .async }} async{{ end }} src="{{ .src }}"{{ with .integrity }} {{ printf "integrity=%q" . | safeHTMLAttr }} crossorigin="anonymous"{{ end }}></script>
<script{{ with .async }} async{{ end }}{{ with .defer }} defer{{ end }} src="{{ .src }}"{{ with .integrity }} {{ printf "integrity=%q" . | safeHTMLAttr }} crossorigin="anonymous"{{ end }}></script>
{{- end -}}
8 changes: 6 additions & 2 deletions src/layouts/partials/stylesheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}

<link href="{{ $style.Permalink | relURL }}" rel="stylesheet">
<link href="{{ $style.RelPermalink }}" rel="stylesheet">

0 comments on commit 98525ba

Please sign in to comment.