Skip to content

How to minify golang text/template (JS) without parsing #815

Open

Description

@x528491x

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.

How do I do this?

Activity

  1. x528491x commented on Apr 27, 2025

    @x528491x
    Author

    Basically, excluding anything enclosed between "{{" and "}}" from being minified.

  2. tdewolff commented on Apr 28, 2025

    @tdewolff
    Owner

    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...

  3. x528491x commented on Apr 28, 2025

    @x528491x
    Author

    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.

  4. tdewolff commented on Apr 28, 2025

    @tdewolff
    Owner

    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...

  5. GAS85 commented on Sep 14, 2026

    @GAS85

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions