You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to minify golang text/template (JS) without parsing #815
I have a GO JS template with a lot of conditions for roled access, and the presence of those conditions does not enable me to run it with the "--type js" flag.
Nor do I want to hook in this library to my app's runtime and minify the parsed template each time.
I want to minify the unparsed JS text/template before my go app is compiled, so that only the minified template is handed over for parsing.
This is much harder than you might think, because {{ can be part of a valid JavaScript syntax and how do we know whether it's a template or not? Is that JavaScript part of HTML, or is it a stand-alone JS file? I'm not sure if this can be implemented in a nice way...
Yes, that is about as good as you can fix this problem I suppose. I don't know how other engines handle templated JavaScript files? I don't think we can solve this in the general case for other users though...
Activity
x528491x commentedon Apr 27, 2025
Basically, excluding anything enclosed between "{{" and "}}" from being minified.
tdewolff commentedon Apr 28, 2025
This is much harder than you might think, because
{{can be part of a valid JavaScript syntax and how do we know whether it's a template or not? Is that JavaScript part of HTML, or is it a stand-alone JS file? I'm not sure if this can be implemented in a nice way...x528491x commentedon Apr 28, 2025
Mine is a standalone JS file and have made no use of {{ }} in the JS syntax.
My hacky solution so far is to regex find for all {{.*}} within the file and load it into an array.
Then replace those matches with a PLACEHOLDER string within the JS file.
Then run minify --type js, and replace the placeholders that remain with the elements from the array.
tdewolff commentedon Apr 28, 2025
Yes, that is about as good as you can fix this problem I suppose. I don't know how other engines handle templated JavaScript files? I don't think we can solve this in the general case for other users though...
GAS85 commentedon Sep 14, 2026
I found a way for my project GAS85/ownPastebin@fe7d746#diff-dd2c0eb6ea5cfc6c4bd4eac30934e2d5746747af48fef6da689e85b752f39557R44, but it is not universal and each time when I change something, I need to test it again.
Besides, what about JSON? It also can be template and there is no JS inside. Should be supported.