Skip to content

Commit

Permalink
feat: move imports into functions to prevent import issues in other apps
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Dec 10, 2023
1 parent b056c42 commit f0ff3e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
19 changes: 17 additions & 2 deletions litestar_vite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from litestar.cli._utils import (
LitestarEnv,
LitestarGroup,
console,
)
from rich.prompt import Confirm

if TYPE_CHECKING:
from litestar import Litestar
Expand Down Expand Up @@ -88,6 +86,11 @@ def vite_init(
no_prompt: bool,
) -> None: # sourcery skip: low-code-quality
"""Run vite build."""
from litestar.cli._utils import (
console,
)
from rich.prompt import Confirm

from litestar_vite.commands import init_vite
from litestar_vite.plugin import VitePlugin

Expand Down Expand Up @@ -149,6 +152,10 @@ def vite_init(
@option("--verbose", type=bool, help="Enable verbose output.", default=False, is_flag=True)
def vite_install(app: Litestar, verbose: bool) -> None: # noqa: ARG001
"""Run vite build."""
from litestar.cli._utils import (
console,
)

from litestar_vite.commands import run_vite
from litestar_vite.plugin import VitePlugin

Expand All @@ -164,6 +171,10 @@ def vite_install(app: Litestar, verbose: bool) -> None: # noqa: ARG001
@option("--verbose", type=bool, help="Enable verbose output.", default=False, is_flag=True)
def vite_build(app: Litestar, verbose: bool) -> None: # noqa: ARG001
"""Run vite build."""
from litestar.cli._utils import (
console,
)

from litestar_vite.commands import run_vite
from litestar_vite.plugin import VitePlugin

Expand All @@ -179,6 +190,10 @@ def vite_build(app: Litestar, verbose: bool) -> None: # noqa: ARG001
@option("--verbose", type=bool, help="Enable verbose output.", default=False, is_flag=True)
def vite_serve(app: Litestar, verbose: bool) -> None: # noqa: ARG001
"""Run vite serve."""
from litestar.cli._utils import (
console,
)

from litestar_vite.commands import run_vite
from litestar_vite.plugin import VitePlugin

Expand Down
16 changes: 10 additions & 6 deletions litestar_vite/plugin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
from __future__ import annotations

import multiprocessing
from contextlib import contextmanager
from typing import TYPE_CHECKING, Iterator

from litestar.plugins import CLIPlugin, InitPluginProtocol

from litestar_vite.cli import vite_group
from litestar_vite.commands import run_vite
from litestar_vite.config import ViteTemplateConfig
from litestar_vite.template_engine import ViteTemplateEngine

if TYPE_CHECKING:
from click import Group
from litestar import Litestar
Expand All @@ -33,6 +27,8 @@ def __init__(self, config: ViteConfig) -> None:
self._config = config

def on_cli_init(self, cli: Group) -> None:
from litestar_vite.cli import vite_group

cli.add_command(vite_group)
return super().on_cli_init(cli)

Expand All @@ -42,6 +38,10 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
Args:
app_config: The :class:`AppConfig <.config.app.AppConfig>` instance.
"""

from litestar_vite.config import ViteTemplateConfig
from litestar_vite.template_engine import ViteTemplateEngine

app_config.template_config = ViteTemplateConfig( # type: ignore[assignment]
engine=ViteTemplateEngine,
config=self._config,
Expand All @@ -51,6 +51,10 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:

@contextmanager
def server_lifespan(self, app: Litestar) -> Iterator[None]:
import multiprocessing

from litestar_vite.commands import run_vite

if self._config.use_server_lifespan:
command_to_run = self._config.run_command

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ license = {text = "MIT"}
name = "litestar-vite"
readme = "README.md"
requires-python = ">=3.8"
version = "0.1.4"
version = "0.1.5"

[project.urls]
Changelog = "https://cofin.github.io/litestar-vite/latest/changelog"
Expand Down

0 comments on commit f0ff3e8

Please sign in to comment.