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 encore_entry_exists() twig functions to check if entrypoint has files #147

Merged
merged 1 commit into from
Feb 14, 2022

Conversation

acrobat
Copy link
Contributor

@acrobat acrobat commented Oct 15, 2021

I've added 2 helper twig functions to check if the entrypoint exists and if it has files.

Closes #100

@weaverryan
Copy link
Member

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 encore_entry_script_tags('foo') without generating an error that the entry doesn't exist?

Cheers!

@acrobat
Copy link
Contributor Author

acrobat commented Oct 17, 2021

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 encore_entry_script_tags('admin') for the admin entry (which might not be defined in a user project) throws an exception.

if (!isset($entriesData['entrypoints'][$entryName]) && $this->strictMode) {
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));
if (isset($entriesData['entrypoints'][$withoutExtension])) {
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s". Try "%s" instead (without the extension).', $entryName, $withoutExtension));
}
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s" in "%s". Found: %s.', $entryName, $this->entrypointJsonPath, implode(', ', array_keys($entriesData['entrypoints']))));

So that's the reasoning behind this PR, but as you suggested not throwing or catching the exception in encore_entry_script_tags and encore_entry_link_tags would also fix our case. I don't know which approach you prefer, but I'm able to update the PR if needed!

@@ -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']),
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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!

@Exellent1988
Copy link

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.

@acrobat
Copy link
Contributor Author

acrobat commented Nov 20, 2021

@weaverryan I think this PR is ready for a final review, let me know if there are any changes needed!

@acrobat
Copy link
Contributor Author

acrobat commented Jan 6, 2022

Friendly ping @weaverryan

try {
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
$entrypointLookup->getJavaScriptFiles($entryName);
$entrypointLookup->reset();
Copy link
Member

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

Copy link
Contributor Author

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!

@acrobat acrobat force-pushed the check-if-entrypoint-has-files branch 2 times, most recently from 23a52a0 to f9ca828 Compare February 10, 2022 20:55
@acrobat acrobat force-pushed the check-if-entrypoint-has-files branch from f9ca828 to 8718558 Compare February 11, 2022 07:05
@weaverryan
Copy link
Member

Thanks @acrobat!

@weaverryan weaverryan merged commit b6f5e22 into symfony:main Feb 14, 2022
@acrobat acrobat deleted the check-if-entrypoint-has-files branch February 14, 2022 13:55
@weaverryan weaverryan changed the title Add twig helper functions to check if entrypoint has files Add encore_entry_exists() twig functions to check if entrypoint has files Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to check if an entry exists for a given entrypoint
4 participants