Repository: litestar-org/litestar

Issue:
Bug: minijinja import too early on FlashPlugin
### Description

error is: 
```
❯ python main.py
Traceback (most recent call last):
  File "/home/lotso/.cache/pypoetry/virtualenvs/tt-n-gxmD69-py3.12/lib/python3.12/site-packages/litestar/contrib/minijinja.py", line 21, in <module>
    from minijinja import Environment  # type:ignore[import-untyped]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'minijinja'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/lotso/toto/tt/main.py", line 2, in <module>
    from litestar.plugins.flash import FlashPlugin
  File "/home/lotso/.cache/pypoetry/virtualenvs/tt-n-gxmD69-py3.12/lib/python3.12/site-packages/litestar/plugins/flash.py", line 8, in <module>
    from litestar.contrib.minijinja import MiniJinjaTemplateEngine
  File "/home/lotso/.cache/pypoetry/virtualenvs/tt-n-gxmD69-py3.12/lib/python3.12/site-packages/litestar/contrib/minijinja.py", line 24, in <module>
    raise MissingDependencyException("minijinja") from e
litestar.exceptions.base_exceptions.MissingDependencyException: Package 'minijinja' is not installed but required. You can install it by running 'pip install litestar[minijinja]' to install litestar with the required extra or 'pip install minijinja' to install the package separately
```
the imports for minijinja seems to be too early in the game, 



### URL to code causing the issue

_No response_

### MCVE

```python
from litestar import Litestar
from litestar.plugins.flash import FlashPlugin


app = Litestar(route_handlers=[], plugins=[FlashPlugin])
```


### Steps to reproduce

```bash
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
```


### Screenshots

```bash
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
```


### Logs

_No response_

### Litestar Version

2.8.2

### Platform

- [X] Linux
- [ ] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)

<!-- POLAR PLEDGE BADGE START -->
---
> [!NOTE]  
> While we are open for sponsoring on [GitHub Sponsors](https://github.com/sponsors/litestar-org/) and 
> [OpenCollective](https://opencollective.com/litestar), we also utilize [Polar.sh](https://polar.sh/) to engage in pledge-based sponsorship.
>
> Check out all issues funded or available for funding [on our Polar.sh dashboard](https://polar.sh/litestar-org)
> * If you would like to see an issue prioritized, make a pledge towards it!
> * We receive the pledge once the issue is completed & verified
> * This, along with engagement in the community, helps us know which features are a priority to our users.

<a href="https://polar.sh/litestar-org/litestar/issues/3415">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/litestar/issues/3415/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/litestar/issues/3415/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->



Discussion:

Resolving patch:
diff --git a/litestar/plugins/flash.py b/litestar/plugins/flash.py
--- a/litestar/plugins/flash.py
+++ b/litestar/plugins/flash.py
@@ -1,16 +1,22 @@
 """Plugin for creating and retrieving flash messages."""
 
+from __future__ import annotations
+
 from dataclasses import dataclass
-from typing import Any, Mapping
+from typing import TYPE_CHECKING, Any, Mapping
 
-from litestar.config.app import AppConfig
-from litestar.connection import ASGIConnection
-from litestar.contrib.minijinja import MiniJinjaTemplateEngine
+from litestar.exceptions import MissingDependencyException
 from litestar.plugins import InitPluginProtocol
-from litestar.template import TemplateConfig
 from litestar.template.base import _get_request_from_context
 from litestar.utils.scope.state import ScopeState
 
+if TYPE_CHECKING:
+    from collections.abc import Callable
+
+    from litestar.config.app import AppConfig
+    from litestar.connection import ASGIConnection
+    from litestar.template import TemplateConfig
+
 
 @dataclass
 class FlashConfig:
@@ -39,14 +45,18 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
         Returns:
             The application configuration with the message callable registered.
         """
-        if isinstance(self.config.template_config.engine_instance, MiniJinjaTemplateEngine):
-            from litestar.contrib.minijinja import _transform_state
-
-            self.config.template_config.engine_instance.register_template_callable(
-                "get_flashes", _transform_state(get_flashes)
-            )
+        template_callable: Callable[[Any], Any]
+        try:
+            from litestar.contrib.minijinja import MiniJinjaTemplateEngine, _transform_state
+        except MissingDependencyException:  # pragma: no cover
+            template_callable = get_flashes
         else:
-            self.config.template_config.engine_instance.register_template_callable("get_flashes", get_flashes)
+            if isinstance(self.config.template_config.engine_instance, MiniJinjaTemplateEngine):
+                template_callable = _transform_state(get_flashes)
+            else:
+                template_callable = get_flashes
+
+        self.config.template_config.engine_instance.register_template_callable("get_flashes", template_callable)  # pyright: ignore[reportGeneralTypeIssues]
         return app_config
 
 


Test patch: