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

Add Hugo Pipes logic for local and vendored scripts. #31233

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions config.yml
Expand Up @@ -40,10 +40,12 @@ module:
target: static/apple-touch-icon.png
- source: site/static/docs/5.0/assets/img/favicons/favicon.ico
target: static/favicon.ico
# docsearch is referenced in a `script` tag so we just mount it in the static folder

# the following are processed via Hugo pipes so we need them in the assets folder
## the following will be printed with Hugo pipes
- source: node_modules/docsearch.js/dist/cdn/docsearch.min.js
target: static/docs/5.0/assets/js/vendor/docsearch.min.js
# the following are concatenated via Hugo pipes with docs.js so we need them in the assets folder
target: assets/js/vendor/docsearch.min.js
## The following vendor files will be bundled together on top of local js files as /docs.js
- source: node_modules/anchor-js/anchor.min.js
target: assets/js/vendor/anchor.min.js
- source: node_modules/bs-custom-file-input/dist/bs-custom-file-input.min.js
Expand Down
74 changes: 74 additions & 0 deletions site/layouts/partials/func/get-js-assets.html
@@ -0,0 +1,74 @@
{{/*
get-js-assets
Returns a list of processed Hugo Assets to be used in templates

@author @regisphilibert

@context Page (.)

@access public

@example - Go Template
{{- range partialCached "func/get-js-assets" . $variant -}}
<script src="{{ .RelPermalink }}"></script>
{{- end -}}
*/}}

{{/* We'll return a slice so templates can safely use `range` */}}
{{ $jsAssets := slice }}

{{/* Storing the reused mount warning message */}}
{{ $missing_file_warning := "%s not found. Check your mounts settings and/or run `npm i`." }}
{{ $doc_version := site.Params.docs_version }}
{{/* Doc Search */}}
{{ if eq .Page.Layout "docs" -}}
{{ with resources.GetMatch "js/vendor/docsearch.min.js" }}
{{/* As this is a WIP, we don't customize the asset URL,
but we will after Hugo .74 and resources.Put (https://github.com/gohugoio/hugo/issues/7406):
{{ with resources.GetMatch "js/vendor/docsearch.min.js" }}
{{ $docsearch := . | resources.Put (printf /docs/%s/assets/js/vendor/docsearch.min.js" $doc_version) }}
{{ $jsAssets = $jsAssets | append $docsearch }}
*/}}
{{ $jsAssets = $jsAssets | append . }}
{{ else }}
{{ errorf $missing_file_warning "docsearch.min.js" }}
{{ end }}
{{- end }}

{{/* --- Bundle */}}
{{ $bundle := slice }}

{{/* ----- Mounted from vendors */}}
{{- $vendor_assets := slice -}}
{{/* As we need to list the desired mounted files to:
1. Check for missing mounts and throw an error
2. Control order if need be
3. Exclude docsearch (though there would be other ways) */}}
{{ $vendor_filenames := slice "clipboard.min.js" "anchor.min.js" }}
{{ range $filename := $vendor_filenames }}
{{ with resources.GetMatch (print "js/vendor/" .) }}
{{ $vendor_assets = $vendor_assets | append . }}
{{ else }}
{{ errorf $missing_file_warning $filename }}
{{ end }}
{{ end }}
{{ with $vendor_assets }}
{{ $bundle = $bundle | append . }}
{{ end }}

{{/* ----- Local assets */}}
{{ with resources.Match "js/*.js" }}
{{ $bundle = $bundle | append . }}
{{ end }}

{{/* Above code should have populated $bundle slice */}}
{{ with $bundle }}
{{ $targetBundlePath := printf "/docs/%s/assets/js/docs.js" $doc_version }}
{{ $bundle_asset := $bundle | resources.Concat $targetBundlePath }}
{{ if eq hugo.Environment "production" }}
{{ $bundle_asset = $bundle_asset | resources.Minify }}
{{ end }}
{{ $jsAssets = $jsAssets | append $bundle_asset }}
{{ end }}

{{ return $jsAssets }}
18 changes: 4 additions & 14 deletions site/layouts/partials/scripts.html
Expand Up @@ -4,17 +4,7 @@
<script src="/docs/{{ .Site.Params.docs_version }}/dist/js/bootstrap.bundle.js"></script>
{{- end }}

{{ if eq .Page.Layout "docs" -}}
<script src="/docs/{{ .Site.Params.docs_version }}/assets/js/vendor/docsearch.min.js"></script>
{{- end }}

{{- $vendor := resources.Match "js/vendor/*.js" -}}
{{- $js := resources.Match "js/*.js" -}}
{{- $targetDocsJSPath := printf "/docs/%s/assets/js/docs.js" .Site.Params.docs_version -}}
{{- $docsJs := append $js $vendor | resources.Concat $targetDocsJSPath -}}

{{- if eq hugo.Environment "production" -}}
{{- $docsJs = $docsJs | resources.Minify -}}
{{- end }}

<script src="{{ $docsJs.Permalink | relURL }}"></script>
{{- $variant := cond (eq .Page.Layout "docs") "docs" "default" -}}
{{- range partialCached "func/get-js-assets" . $variant -}}
<script src="{{ .RelPermalink }}"></script>
{{- end -}}