Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts: js minify #500

Closed
maltegrosse opened this issue Feb 7, 2024 · 10 comments
Closed

scripts: js minify #500

maltegrosse opened this issue Feb 7, 2024 · 10 comments
Labels
enhancement New feature or request NeedsDecision Issue needs some more discussion so a decision can be made scripts

Comments

@maltegrosse
Copy link

first of all, amazing tool!

is it possible to implement some minify tools, especially regarding the javascript code. Seems like a good go library exists:
https://github.com/tdewolff/minify

@stephenafamo
Copy link
Contributor

If this is to be considered, I think esbuild may be a better tool to rely on.

@nathan-hello
Copy link
Contributor

You could use your favorite javascript minifier in a build step, and point your <script> tags to the foo.min.js file instead of foo.js.

@maltegrosse
Copy link
Author

Following the templ guide, in my example, every custom templ component (if needed) has its own script part.
So the js is only rendered if its really needed, not a general js file is provided.

Is this a design issue (from my own) I havent considered?

@joerdav joerdav changed the title feature request: js minify scripts: js minify Feb 8, 2024
@joerdav joerdav added enhancement New feature or request scripts NeedsDecision Issue needs some more discussion so a decision can be made labels Feb 8, 2024
@joerdav
Copy link
Collaborator

joerdav commented Feb 8, 2024

Currently the only way to achieve this would be to ditch templ scripts and create your own JS files that are minified and referenced by your templates.

I think that the code in templ scripts should be small so may not really benefit from minification, which I believe is ideal for projects with large JS assets which are usually inflated by dependencies.

@a-h
Copy link
Owner

a-h commented Feb 13, 2024

I'm playing around with the use of tdewolff's JS parser to enable a new type of <script> parsing.

It would open up the door to this sort of thing in templ, and maybe supersede script and CSS templates over the longer term:

templ Component(y any) {
  <script>
    var x = {{ y }};
    alert(y);
  </script>
  <div>Hello</div>
  <a href="google.com" onclick="if {{ y }}.data { alert('no'); }">Click</a>
}

So yes, tdewolff's minifier might work nicely.

@stephenafamo
Copy link
Contributor

It would also be great if templ's LSP can perhaps also proxy to another LSP for JS (similar to how Go code is checked with gopls)

@a-h
Copy link
Owner

a-h commented Feb 13, 2024

That is also something that I'm looking at @stephenafamo - I put an example of what that could look like at #498 (comment)

The example demonstrates the concept of being able to have just the HTML rendered, which would massively simplify the use of HTML and other LSPs.

@tommyo
Copy link

tommyo commented Apr 26, 2024

I think the same applies to css. Being able to run the component level css through postcss at generate time opens up a whole ecosystem of plugins without templ having to be aware.

So maybe a hook/middleware in the generate logic that calls out to plugins, allowing custom logic?

Alternatively, a vite plugin that triggers templ generate so that vite handles the esbuild, postcss and bundling. I think that may even help the LSP.

I'm going to dig around under the hood today and see what that would take.

@joerdav
Copy link
Collaborator

joerdav commented May 31, 2024

Due to the recommendation to move away from script templates I think we can close this.

The decision is due to scenarios exactly like this, we cannot support all of the workflows that people like to process js and css, so making the interop with the outputs of said processes should be the priority.

@joerdav joerdav closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@phanirithvij
Copy link

phanirithvij commented May 31, 2024

It's not recommended to pass go values to scripts anymore?
The alternative then is to have an API endpoint and a fetch call in the js?
Aren't we losing a very useful feature?

Edit: never mind, please ignore my comment. I didn't read the full documentation https://templ.guide/syntax-and-usage/script-templates#passing-server-side-data-to-scripts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request NeedsDecision Issue needs some more discussion so a decision can be made scripts
Projects
None yet
Development

No branches or pull requests

7 participants