litestar-vite 0.7.1__py3-none-any.whl → 0.8.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of litestar-vite might be problematic. Click here for more details.

@@ -0,0 +1,136 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from contextlib import contextmanager
5
+ from pathlib import Path
6
+ from typing import TYPE_CHECKING, Iterator, cast
7
+
8
+ from litestar.plugins import CLIPlugin, InitPluginProtocol
9
+ from litestar.static_files import (
10
+ create_static_files_router, # pyright: ignore[reportUnknownVariableType]
11
+ )
12
+
13
+ from litestar_vite.config import ViteConfig
14
+
15
+ if TYPE_CHECKING:
16
+ from click import Group
17
+ from litestar import Litestar
18
+ from litestar.config.app import AppConfig
19
+
20
+ from litestar_vite.config import ViteTemplateConfig
21
+ from litestar_vite.template_engine import ViteTemplateEngine
22
+
23
+
24
+ def set_environment(config: ViteConfig) -> None:
25
+ """Configure environment for easier integration"""
26
+ os.environ.setdefault("ASSET_URL", config.asset_url)
27
+ os.environ.setdefault("VITE_ALLOW_REMOTE", str(True))
28
+ os.environ.setdefault("VITE_PORT", str(config.port))
29
+ os.environ.setdefault("VITE_HOST", config.host)
30
+ os.environ.setdefault("VITE_PROTOCOL", config.protocol)
31
+ os.environ.setdefault("APP_URL", f"http://localhost:{os.environ.get('LITESTAR_PORT',8000)}")
32
+ if config.dev_mode:
33
+ os.environ.setdefault("VITE_DEV_MODE", str(config.dev_mode))
34
+
35
+
36
+ class VitePlugin(InitPluginProtocol, CLIPlugin):
37
+ """Vite plugin."""
38
+
39
+ __slots__ = ("_config",)
40
+
41
+ def __init__(self, config: ViteConfig | None = None) -> None:
42
+ """Initialize ``Vite``.
43
+
44
+ Args:
45
+ config: configuration to use for starting Vite. The default configuration will be used if it is not provided.
46
+ """
47
+ if config is None:
48
+ config = ViteConfig()
49
+ self._config = config
50
+
51
+ @property
52
+ def config(self) -> ViteConfig:
53
+ return self._config
54
+
55
+ @property
56
+ def template_config(self) -> ViteTemplateConfig[ViteTemplateEngine]:
57
+ from litestar_vite.config import ViteTemplateConfig
58
+ from litestar_vite.template_engine import ViteTemplateEngine
59
+
60
+ return ViteTemplateConfig[ViteTemplateEngine](
61
+ engine=ViteTemplateEngine,
62
+ config=self._config,
63
+ directory=self._config.template_dir,
64
+ )
65
+
66
+ def on_cli_init(self, cli: Group) -> None:
67
+ from litestar_vite.cli import vite_group
68
+
69
+ cli.add_command(vite_group)
70
+
71
+ def on_app_init(self, app_config: AppConfig) -> AppConfig:
72
+ """Configure application for use with Vite.
73
+
74
+ Args:
75
+ app_config: The :class:`AppConfig <.config.app.AppConfig>` instance.
76
+ """
77
+
78
+ if self._config.template_dir is not None:
79
+ app_config.template_config = self.template_config
80
+
81
+ if self._config.set_static_folders:
82
+ static_dirs = [Path(self._config.bundle_dir), Path(self._config.resource_dir)]
83
+ if Path(self._config.public_dir).exists() and self._config.public_dir != self._config.bundle_dir:
84
+ static_dirs.append(Path(self._config.public_dir))
85
+ app_config.route_handlers.append(
86
+ create_static_files_router(
87
+ directories=cast( # type: ignore[arg-type]
88
+ "list[Path]",
89
+ static_dirs if self._config.dev_mode else [Path(self._config.bundle_dir)],
90
+ ),
91
+ path=self._config.asset_url,
92
+ name="vite",
93
+ html_mode=False,
94
+ include_in_schema=False,
95
+ opt={"exclude_from_auth": True},
96
+ ),
97
+ )
98
+ return app_config
99
+
100
+ @contextmanager
101
+ def server_lifespan(self, app: Litestar) -> Iterator[None]:
102
+ import threading
103
+
104
+ from litestar.cli._utils import console
105
+
106
+ from litestar_vite.commands import execute_command
107
+
108
+ if self._config.use_server_lifespan and self._config.dev_mode:
109
+ command_to_run = self._config.run_command if self._config.hot_reload else self._config.build_watch_command
110
+ if self.config.hot_reload:
111
+ console.rule("[yellow]Starting Vite process with HMR Enabled[/]", align="left")
112
+ else:
113
+ console.rule("[yellow]Starting Vite watch and build process[/]", align="left")
114
+ if self._config.set_environment:
115
+ set_environment(config=self._config)
116
+ vite_thread = threading.Thread(
117
+ name="vite",
118
+ target=execute_command,
119
+ args=[],
120
+ kwargs={"command_to_run": command_to_run, "cwd": self._config.root_dir},
121
+ )
122
+ try:
123
+ vite_thread.start()
124
+ yield
125
+ finally:
126
+ if vite_thread.is_alive():
127
+ vite_thread.join(timeout=5)
128
+ console.print("[yellow]Vite process stopped.[/]")
129
+
130
+ else:
131
+ manifest_path = Path(f"{self._config.bundle_dir}/{self._config.manifest_name}")
132
+ if manifest_path.exists():
133
+ console.rule(f"[yellow]Serving assets using manifest at `{manifest_path!s}`.[/]", align="left")
134
+ else:
135
+ console.rule(f"[yellow]Serving assets without manifest at `{manifest_path!s}`.[/]", align="left")
136
+ yield
litestar_vite/py.typed ADDED
File without changes
@@ -0,0 +1,103 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any, Mapping, TypeVar
4
+
5
+ import markupsafe
6
+ from litestar.contrib.jinja import JinjaTemplateEngine
7
+ from litestar.template.base import TemplateEngineProtocol
8
+
9
+ from litestar_vite.loader import ViteAssetLoader
10
+
11
+ if TYPE_CHECKING:
12
+ from pathlib import Path
13
+
14
+ from jinja2 import Environment
15
+ from jinja2 import Template as JinjaTemplate
16
+
17
+ from litestar_vite.config import ViteConfig
18
+
19
+ T = TypeVar("T", bound=TemplateEngineProtocol["JinjaTemplate", Mapping[str, Any]])
20
+
21
+
22
+ class ViteTemplateEngine(JinjaTemplateEngine):
23
+ """Jinja Template Engine with Vite Integration.
24
+
25
+ This class extends :class:`litestar.contrib.jinja.JinjaTemplateEngine` to provide Vite asset integration
26
+ and hot module reloading support.
27
+
28
+ Raises:
29
+ TemplateNotFoundException: If template is not found.
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ directory: Path | list[Path] | None = None,
35
+ engine_instance: Environment | None = None,
36
+ config: ViteConfig | None = None,
37
+ ) -> None:
38
+ """Jinja2 based TemplateEngine.
39
+
40
+ Args:
41
+ directory: Direct path or list of directory paths from which to serve templates.
42
+ engine_instance: A jinja Environment instance.
43
+ config: Vite config
44
+ """
45
+ super().__init__(directory=directory, engine_instance=engine_instance)
46
+ if config is None:
47
+ msg = "Please configure the `ViteConfig` instance."
48
+ raise ValueError(msg)
49
+ self.config = config
50
+ self.asset_loader = ViteAssetLoader.initialize_loader(config=self.config)
51
+ self.engine.globals.update({"vite_hmr": self.get_hmr_client, "vite": self.get_asset_tag}) # pyright: ignore[reportCallIssue,reportUnknownMemberType,reportArgumentType]
52
+
53
+ def get_hmr_client(self) -> markupsafe.Markup:
54
+ """Generate the script tag for the Vite WS client for HMR.
55
+
56
+ Only used when hot module reloading is enabled, in production this method returns an empty string.
57
+
58
+ Returns:
59
+ markupsafe.Markup: The script tag or an empty string.
60
+ """
61
+ return markupsafe.Markup(
62
+ f"{self.asset_loader.generate_react_hmr_tags()}{self.asset_loader.generate_ws_client_tags()}",
63
+ )
64
+
65
+ def get_asset_tag(
66
+ self,
67
+ path: str | list[str],
68
+ scripts_attrs: dict[str, str] | None = None,
69
+ **_: Any,
70
+ ) -> markupsafe.Markup:
71
+ """Generate all assets include tags for the file in argument.
72
+
73
+ Generates all scripts tags for this file and all its dependencies
74
+ (JS and CSS) by reading the manifest file (for production only).
75
+ In development Vite imports all dependencies by itself.
76
+ Place this tag in <head> section of your page.
77
+
78
+ Args:
79
+ path: Path to a Vite asset to include.
80
+ scripts_attrs: Dictionary of attributes to add to script tags.
81
+ **_: Additional keyword arguments (ignored).
82
+
83
+ Returns:
84
+ markupsafe.Markup: HTML markup containing all required asset tags.
85
+ """
86
+ if isinstance(path, str):
87
+ path = [path]
88
+ return markupsafe.Markup(
89
+ "".join([self.asset_loader.generate_asset_tags(p, scripts_attrs=scripts_attrs) for p in path]),
90
+ )
91
+
92
+ @classmethod
93
+ def from_environment(cls, config: ViteConfig, jinja_environment: Environment) -> ViteTemplateEngine: # type: ignore[override]
94
+ """Create a JinjaTemplateEngine from an existing jinja Environment instance.
95
+
96
+ Args:
97
+ config: Vite config
98
+ jinja_environment (jinja2.environment.Environment): A jinja Environment instance.
99
+
100
+ Returns:
101
+ JinjaTemplateEngine instance
102
+ """
103
+ return cls(directory=None, config=config, engine_instance=jinja_environment)
File without changes
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta charset="utf-8" />
6
+ <!--IE compatibility-->
7
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
8
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
9
+ </head>
10
+
11
+ <body>
12
+ {{ vite_hmr() }}
13
+ {{ vite('resources/main.ts') }}
14
+ </body>
15
+
16
+ </html>
@@ -0,0 +1 @@
1
+ import "./styles.css"
@@ -0,0 +1,11 @@
1
+ {
2
+ "private": true,
3
+ "type": "module",
4
+ "scripts": {
5
+ "dev": "vite",
6
+ "watch": "vite build --watch",
7
+ "build": "vite build{% if enable_ssr %} && vite build --ssr{% endif %}"
8
+ },
9
+ "dependencies": {{ dependencies }},
10
+ "devDependencies": {{ dev_dependencies }}
11
+ }
File without changes
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "module": "ES2020",
6
+ "moduleResolution": "bundler",
7
+ "strict": true,
8
+ "jsx": "preserve",
9
+ "resolveJsonModule": true,
10
+ "isolatedModules": true,
11
+ "esModuleInterop": true,
12
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
13
+ "skipLibCheck": true,
14
+ "noEmit": true,
15
+ "composite": true,
16
+ "allowSyntheticDefaultImports": true,
17
+ "baseUrl": "{{ resource_path }}",
18
+ "paths": {
19
+ "@/*": ["./*"]
20
+ },
21
+ "types": ["vite/client"]
22
+ },
23
+ "include": [
24
+ {% if include_react %}"{{ resource_path }}/**/*.tsx","{{ resource_path }}/**/*.jsx",{% endif %}{% if include_vue %} "{{ resource_path }}/**/*.vue",{% endif %}
25
+ "**/*.d.ts",
26
+ "{{ resource_path }}/**/*.js" ,
27
+ "{{ resource_path }}/**/*.ts" ,
28
+ "vite.config.ts"
29
+ ]
30
+ }
@@ -0,0 +1,38 @@
1
+ import { defineConfig } from "vite";
2
+ {% if include_vue %}import vue from "@vitejs/plugin-vue";{% endif %}
3
+ {% if include_react %}import react from "@vitejs/plugin-react";{% endif %}
4
+ import litestar from "litestar-vite-plugin";
5
+
6
+ const ASSET_URL = process.env.ASSET_URL || "{{ asset_url }}";
7
+ const VITE_PORT = process.env.VITE_PORT || "5173";
8
+ const VITE_HOST = process.env.VITE_HOST || "localhost";
9
+ export default defineConfig({
10
+ base: `${ASSET_URL}`,
11
+ {% if root_path and root_path != '.' %} root: "{{ root_path }}",{% endif %}
12
+ server: {
13
+ host: "0.0.0.0",
14
+ port: +`${VITE_PORT}`,
15
+ cors: true,
16
+ hmr: {
17
+ host: `${VITE_HOST}`,
18
+ },
19
+ },
20
+ plugins: [
21
+ {% if include_vue %}vue(),{% endif %}
22
+ {% if include_react %}react(),{% endif %}
23
+ litestar({
24
+ input: [
25
+ {% if entry_point %}"{{ entry_point | join('\", \"') }}"{% endif %}
26
+ ],
27
+ assetUrl: `${ASSET_URL}`,
28
+ bundleDirectory: "{{ bundle_path }}",
29
+ resourceDirectory: "{{ resource_path }}",
30
+ hotFile: "{{ hot_file }}"
31
+ }),
32
+ ],
33
+ resolve: {
34
+ alias: {
35
+ "@": "{{ resource_path }}"
36
+ },
37
+ },
38
+ });
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: litestar-vite
3
- Version: 0.7.1
3
+ Version: 0.8.1
4
4
  Summary: Vite plugin for Litestar
5
5
  Project-URL: Changelog, https://cofin.github.io/litestar-vite/latest/changelog
6
6
  Project-URL: Discord, https://discord.gg/X3FJqy8d2j
@@ -0,0 +1,30 @@
1
+ litestar_vite/__init__.py,sha256=S2zgWHas3O16FhXukd1j3tGCMP9skj7Rp-BvMQA7sd8,402
2
+ litestar_vite/__metadata__.py,sha256=_Wo-vNQuj5co9J4FwJAB2rRafbFo8ztTHrXmEPrYrV8,514
3
+ litestar_vite/cli.py,sha256=foXJ-xW1bvUEsT7nPo1hbN0FLaDzHWPG4zpmqN__rY0,10976
4
+ litestar_vite/commands.py,sha256=mqbgCzN0eRLAKfPFxMzpgwfzFrYv8kManl_uY4cEkqE,4961
5
+ litestar_vite/config.py,sha256=CN5XpRObgAd7nZd4rhF-a_07M_s2FFUsImq-iZjc8RU,7685
6
+ litestar_vite/loader.py,sha256=gK0RlenM-enNV_pS-jEwW9hanAmq053m2P75rfQuGCg,8227
7
+ litestar_vite/plugin.py,sha256=p4VPKYdWw5qIYZ1OwfdMRcwM_Q-vfoDEmIbu3IyF7oI,5166
8
+ litestar_vite/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ litestar_vite/template_engine.py,sha256=lFFJsD-sgf3LIaFDoD1NysRUg49WV-ELE53SHiBDeeA,3837
10
+ litestar_vite/inertia/__init__.py,sha256=Fab61KXbBnyub2Nx-2AHYv2U6QiYUIrqhEZia_f9xik,863
11
+ litestar_vite/inertia/_utils.py,sha256=ijO9Lgka7ZPIAHkby9szbTGoSg0nDShC2bqWT9cDxi0,1956
12
+ litestar_vite/inertia/config.py,sha256=0Je9SLg0acv0eRvudk3aJLj5k1DjPxULoVOwAfpjnUc,1232
13
+ litestar_vite/inertia/exception_handler.py,sha256=0FiW8jVib0xT453BzMPOeJa7bRwnxkOUdJ91kiFHPIo,5308
14
+ litestar_vite/inertia/middleware.py,sha256=NEDcAoT7GMWA9hEGvANZ3MG5_p3MmZX57RF95T71les,1716
15
+ litestar_vite/inertia/plugin.py,sha256=OgXmmvjl7KWlt4KEkYhS3mU2EY4iYN9JtJ20S3Qlht8,2349
16
+ litestar_vite/inertia/request.py,sha256=Ogt_ikauWrsgKafaip7IL1YhbybwjdBAQ0PQS7cImoQ,3848
17
+ litestar_vite/inertia/response.py,sha256=Au7jQnqjY3wJAHNmiwteCZxKTKGCpnkjMTSzBVRwkXs,15927
18
+ litestar_vite/inertia/routes.py,sha256=QksJm2RUfL-WbuhOieYnPXXWO5GYnPtmsYEm6Ef8Yeo,1782
19
+ litestar_vite/inertia/types.py,sha256=tLp0pm1N__hcWC875khf6wH1nuFlKS9-VjDqgsRkXnw,702
20
+ litestar_vite/templates/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
+ litestar_vite/templates/index.html.j2,sha256=Xue1cTl6piEOZkW3UG68SnW80NeL9PxaOp-Xr23kpr4,322
22
+ litestar_vite/templates/main.ts.j2,sha256=Nzr5m_hXMAjeDL_4yQNP3DMCf7Blh3dwg5m-67GJVbY,22
23
+ litestar_vite/templates/package.json.j2,sha256=0JWgdTuaSZ25EmCltF_zbqDdpxfvCLeYuzBjXrziXNw,299
24
+ litestar_vite/templates/styles.css.j2,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
+ litestar_vite/templates/tsconfig.json.j2,sha256=q1REIuVyXUHCy4Zi2kgTkmrhdT98vyY89k-WTrImOj8,843
26
+ litestar_vite/templates/vite.config.ts.j2,sha256=FZ4OJaB8Kjby_nlx4_LCP8eCe1LRi8kW2GspCiVMfDY,1115
27
+ litestar_vite-0.8.1.dist-info/METADATA,sha256=Ylm0LhfvoJgcOjM7pmYbeGRgEbt2sZqzIKzA2j9ManY,6022
28
+ litestar_vite-0.8.1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
29
+ litestar_vite-0.8.1.dist-info/licenses/LICENSE,sha256=HeTiEfEgvroUXZe_xAmYHxtTBgw--mbXyZLsWDYabHc,1069
30
+ litestar_vite-0.8.1.dist-info/RECORD,,
@@ -1,4 +0,0 @@
1
- litestar_vite-0.7.1.dist-info/METADATA,sha256=_a1MNUenKxT_hVewYV5_MQEMzTo6pP5SagrcV42EBqY,6022
2
- litestar_vite-0.7.1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
3
- litestar_vite-0.7.1.dist-info/licenses/LICENSE,sha256=HeTiEfEgvroUXZe_xAmYHxtTBgw--mbXyZLsWDYabHc,1069
4
- litestar_vite-0.7.1.dist-info/RECORD,,