Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify files on production #429

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $scriptFile := $scripts -}}
{{- $script := resources.Get $scriptFile | 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" $scriptFile) -}}

<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">