This works as long as canonifyURLs is not enabled. With canonifyURLs=true these ULRs are not canonified which is causing issues. Is this a bug or something that can be fixed in the theme?
Well then, since that approach does not work, consider fetching these JS files as resources and then use the built-in resource.Permalink instead of fetching these files with the index function and trying to create the permalink the way you do.
Because canonifyURLs can be set by the end user of the theme. There is AFAIK no way to prevent this and as I said it works for every other code section where relURL is used.
The theme works with relULR by default without issues but if someone wants/has to use canoncial users canonifyURLs should/can be used. Isn’t this the intended use case for this config option?
Still you should load them as proper resources and since you explained your use case above use the resource.RelPermalink that should be turned into an absolute URL, if canonifyURLs is enabled.
Anyway your setup is complex. I am sure that others may have more insights if you are not able to solve the issue with the suggestion I already offered.
Also you can open a GitHub issue if you think that this is a bug.
Make the issue title the same as this topic and report that when creating a resource from an asset file with resources.ExecuteAsTemplate that contains the resource.RelPermalink of another resource and references to data files with relURL then the URLs to these files are not made canonical when canonifyURLs is enabled.
Also explain that your use case is for a theme and that you wish to provide users with the choice of using either a canonical or a relative URL for their project’s assets.
Feel free to post the link to the GitHub issue in this topic for reference.
My answer didn’t take your entire problem into account.
My short take on this:
canonifyURLs works ONLY for HTML and XML output (not JS files)
it’s task is to make relative URLs into absolute
in my head you might as well spell them out absolutely considering the limitations of canonifyURLs (and markdown was, I believe, the reason that setting was added in the first place, hence my remark about render hooks).