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

Add option for {% js %} tag to output javascript into file #4916

Closed
piotrpog opened this issue Sep 11, 2019 · 7 comments

Comments

@piotrpog
Copy link

commented Sep 11, 2019

As in issue title - add option for {% js %} tag to output javascript into file.

This would make easier consuming generated JS code by build systems like gulp.

@brandonkelly

This comment has been minimized.

Copy link
Member

commented Sep 11, 2019

Can you give me an example of how this would work?

@piotrpog

This comment has been minimized.

Copy link
Author

commented Sep 11, 2019

@brandonkelly
Right now all JS is concated and appended right before closing <body> tag.

What i propose is outputting JS into file. If no file exist, it would be created.

@brandonkelly

This comment has been minimized.

Copy link
Member

commented Sep 11, 2019

Gotcha. You could accomplish this by creating a template ending in .js, and output all your dynamic JS code in there, then pull it in using view.registerJsFile().

{% do view.registerJsFile(url('path/to/template.js')) %}
@piotrpog

This comment has been minimized.

Copy link
Author

commented Sep 11, 2019

@brandonkelly
Thanks!

@piotrpog

This comment has been minimized.

Copy link
Author

commented Sep 11, 2019

@brandonkelly
I just looked into it a bit. Doesn't it require that i put all JS into this file myself?
I wanted to keep using {% js %} tag as i used to in multiple twig components.

@brandonkelly

This comment has been minimized.

Copy link
Member

commented Sep 11, 2019

If the actual JS is going to vary based on the request, then it’s better for it to be included in the response HTML. Moving it to a separate file that changes with each request would add a bunch of unnecessary overhead.

@piotrpog

This comment has been minimized.

Copy link
Author

commented Sep 11, 2019

@brandonkelly
I think you are right, I forgot that different pages/sites will have different JS.
It would be better to somehow hook gulp or any other tool into build process but this is probably job for the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.