From 23f31c4c10c107151a39cb1d5b4f68d7dabdbe8f Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 8 Aug 2024 09:55:08 -1000 Subject: [PATCH] Script: Implement a proper type check Use `printf "%T"` to get a string that indicates the type of the `resource` context item. Build a resource if its a string. Pass a minify argument through to the build_js template. --- layouts/partials/resource_builders/script.html | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/layouts/partials/resource_builders/script.html b/layouts/partials/resource_builders/script.html index a899a44..7ceaaf4 100644 --- a/layouts/partials/resource_builders/script.html +++ b/layouts/partials/resource_builders/script.html @@ -3,20 +3,27 @@ Emit an HTML script element referring to a JavaScript resource. The resource can be either a path or a Hugo Resource object. If the former, the resource will be looked up and built. - + @context {string|Resource} resource The Resource -@context {bool} isModule +@context {bool} minify + If true and the resource is a string, it will be minified as part of the build + step. +@context {bool} module If true, add information to the script element indicating it should be loaded as a module */ -}} {{- $resource := .resource -}} +{{- $shouldMinify := .minify -}} {{- $isModule := .module -}} -{{ if $resource.resource }} - {{ $resource = partial "resource_builders/build_js.html" $resource }} +{{ if eq (printf "%T" $resource) "string" }} + {{ + $resource = partial "resource_builders/build_js.html" + (dict "resource" $resource "minify" $shouldMinify) + }} {{ end }} {{ with $resource }}