Code Monkey home page Code Monkey logo

Comments (10)

a-h avatar a-h commented on September 26, 2024 3

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.

from templ.

a-h avatar a-h commented on September 26, 2024 3

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.

from templ.

joerdav avatar joerdav commented on September 26, 2024 1

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.

from templ.

phanirithvij avatar phanirithvij commented on September 26, 2024 1

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

from templ.

stephenafamo avatar stephenafamo commented on September 26, 2024

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

from templ.

nathan-hello avatar nathan-hello commented on September 26, 2024

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.

from templ.

maltegrosse avatar maltegrosse commented on September 26, 2024

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?

from templ.

stephenafamo avatar stephenafamo commented on September 26, 2024

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)

from templ.

tommyo avatar tommyo commented on September 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.

from templ.

joerdav avatar joerdav commented on September 26, 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.

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.