Just for anyone else looking into this, here’s what worked (and didn’t):
Given a page bundle with resources as follows (note that there is also a css template resource to be included via resources.ExecuteAsTemplate
and .Concat
):
assets/
templates/
dynamic.css
content/
myBundle/
index.md
css/
pageSpecific.css
js/
pageSpecific.js
with the following in the index.md front matter:
_build:
publishResources: false
In the head (or footer for js) partial, the following will include the pageSpecific Page Resources without copying them directly over:
{{- /* note final path in following not ever used */ -}}
{{- $dynamicCSS := resources.Get "templates/dynamic.css" | resources.ExecuteAsTemplate "/css/compare-dynamic.css" . }}
{{- $cssResources := .Resources.Match "css/*.css" -}}
{{- $cssResources = $cssResources | append $dynamicCSS}}
{{- $finalCSSPath := print .Page.RelPermalink "css/myBundle.css" }}
{{- $css := $cssResources | resources.Concat $finalCSSPath }}
<link rel="stylesheet" type="text/css" href="{{ $css.RelPermalink }}">
I’m guessing that since .RelPermalink is being called on the output of resources.Concat, that the publishResources: false
isn’t being overridden for the pageSpecific.*
files. If you didn’t have a template resource to concat together, you might be able to work around .RelPermalink by using Concat on a slice with only one file. Can’t think why it wouldn’t work.
While playing around with different combinations, if you didn’t want to turn off publishResources
for the whole bundle (say you had some images in there), the you could have the output from the Concat go to css/pageSpecific.css
which overwrites the copied resource. That does only leave you with files in the publishDir that are actually linked:
{{- $finalCSSPath := print .Page.RelPermalink "css/pageSpecific.css" }}
A couple limitations of that:
- you can only replace one file that way. So if you were picking up more than one css file with the
.ResourcesMatch
in the page bundle, the others that would be copied and not linked to.
- this relies on the undocumented order of if the resource is copied before the Concat written out. It did work for me, but not sure it would always.
Final note, I did try using .Resource.Content
but it didn’t work when I tried to feed it into a Concat to get it to output (figured was a long shot). Without that I didn’t know a way to output the contents to a file.
Hope that helps someone else sometime.