-
-
Notifications
You must be signed in to change notification settings - Fork 83
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 encore_entry_exists() twig functions to check if entrypoint has files #147
Add encore_entry_exists() twig functions to check if entrypoint has files #147
Conversation
Hi! Can you explain your use-case a bit more? If your entry doesn't have an CSS or JS files, they already won't be rendered. But perhaps you have some situation where an entrypoint may or may not be even defined (?) and you need to be able to have Cheers! |
Hi @weaverryan, Yes indeed the last part is our use case. We are in the progress of replacing a custom build layer (on top of webpack) with symfony encore in our Kunstmaan CMS. If you install the cms your project has 2 "setup's" an "app" entry (or even split in multiple smaller entries) for the frontend of the website and an "admin" entry which users can define (but are not required to) to override css/js of the admin interface. The switch to symfony encore is almost ready, but we had indeed the issue that the call webpack-encore-bundle/src/Asset/EntrypointLookup.php Lines 95 to 102 in 5a86aad
So that's the reasoning behind this PR, but as you suggested not throwing or catching the exception in |
src/Twig/EntryFilesTwigExtension.php
Outdated
@@ -29,6 +30,8 @@ public function getFunctions(): array | |||
return [ | |||
new TwigFunction('encore_entry_js_files', [$this, 'getWebpackJsFiles']), | |||
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']), | |||
new TwigFunction('encore_has_entry_js_files', [$this, 'hasWebpackJsFiles']), | |||
new TwigFunction('encore_has_entry_css_files', [$this, 'hasWebpackCssFiles']), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a single function: encore_entry_exists()? Because it is not about whether or not there are css or js files in this entry - just whether the entry is defined at all, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would work too! I will adjust the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the code!
08ff217
to
4d98bd9
Compare
is there any progress regaring the pull request? I'm also quite interested in this function to check if an tag has files / exists at all. |
@weaverryan I think this PR is ready for a final review, let me know if there are any changes needed! |
Friendly ping @weaverryan |
src/Twig/EntryFilesTwigExtension.php
Outdated
try { | ||
$entrypointLookup = $this->getEntrypointLookup($entrypointName); | ||
$entrypointLookup->getJavaScriptFiles($entryName); | ||
$entrypointLookup->reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I don't love this part.
Instead, what if we add an entryExists()
method to EntrypointLookup
. It would NOT be part of the interface - but a bonus method on that class.
Then, in here, we would have something like:
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
if (!$entrypointLookup instanceof EntrypointLookup) {
throw new \LogicException(sprintf('Cannot use entryExists() unless the entrypoint lookup is an instance of "%s", EntrypointLookup::class));
}
return $entrypointLookup->entryExists($entryName);
This will avoid the "reset()" part, which could, in theory, have some side effects (if you have, somehow, already rendered some files from this entrypointlookup).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good solution to me, I've updated the PR with the requested changes!
23a52a0
to
f9ca828
Compare
f9ca828
to
8718558
Compare
Thanks @acrobat! |
I've added 2 helper twig functions to check if the entrypoint exists and if it has files.
Closes #100