litestar-vite-plugin 0.8.3 → 0.10.0

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.
package/README.md CHANGED
@@ -18,6 +18,8 @@ from pathlib import Path
18
18
  from litestar import Controller, get, Litestar
19
19
  from litestar.response import Template
20
20
  from litestar.status_codes import HTTP_200_OK
21
+ from litestar.template.config import TemplateConfig
22
+ from litestar.contrib.jinja import JinjaTemplateEngine
21
23
  from litestar_vite import ViteConfig, VitePlugin
22
24
 
23
25
  class WebController(Controller):
@@ -29,9 +31,9 @@ class WebController(Controller):
29
31
  async def index(self) -> Template:
30
32
  return Template(template_name="index.html.j2")
31
33
 
32
-
33
- vite = VitePlugin(config=ViteConfig(template_dir='templates/'))
34
- app = Litestar(plugins=[vite], route_handlers=[WebController])
34
+ template_config = TemplateConfig(engine=JinjaTemplateEngine(directory='templates/'))
35
+ vite = VitePlugin(config=ViteConfig())
36
+ app = Litestar(plugins=[vite], template_config=template_config, route_handlers=[WebController])
35
37
 
36
38
  ```
37
39
 
@@ -52,6 +52,12 @@ interface PluginConfig {
52
52
  * @default null
53
53
  */
54
54
  detectTls?: string | boolean | null;
55
+ /**
56
+ * Automatically detect the index.html file.
57
+ *
58
+ * @default true
59
+ */
60
+ autoDetectIndex?: boolean;
55
61
  /**
56
62
  * Transform the code while serving.
57
63
  */
package/dist/js/index.js CHANGED
@@ -5,7 +5,7 @@ import colors from "picocolors";
5
5
  import { loadEnv } from "vite";
6
6
  import fullReload from "vite-plugin-full-reload";
7
7
  let exitHandlersBound = false;
8
- const refreshPaths = ["**/*.py", "**/*.j2", "**/*.html.j2", "**/*.html", "**/assets/**/*"];
8
+ const refreshPaths = ["src/**", "resources/**", "assets/**"].filter((path2) => fs.existsSync(path2.replace(/\*\*$/, "")));
9
9
  function litestar(config) {
10
10
  const pluginConfig = resolvePluginConfig(config);
11
11
  return [resolveLitestarPlugin(pluginConfig), ...resolveFullReloadConfig(pluginConfig)];
@@ -83,9 +83,25 @@ function resolveLitestarPlugin(pluginConfig) {
83
83
  }
84
84
  return void 0;
85
85
  },
86
- configureServer(server) {
86
+ async configureServer(server) {
87
87
  const envDir = resolvedConfig.envDir || process.cwd();
88
88
  const appUrl = loadEnv(resolvedConfig.mode, envDir, "APP_URL").APP_URL ?? "undefined";
89
+ const shouldServeIndex = () => {
90
+ if (!pluginConfig.autoDetectIndex) return false;
91
+ const possiblePaths = [
92
+ path.join(server.config.root, "index.html"),
93
+ path.join(server.config.root, pluginConfig.resourceDirectory, "index.html"),
94
+ path.join(server.config.root, "public", "index.html")
95
+ ];
96
+ for (const indexPath of possiblePaths) {
97
+ try {
98
+ fs.accessSync(indexPath);
99
+ return true;
100
+ } catch {
101
+ }
102
+ }
103
+ return false;
104
+ };
89
105
  server.httpServer?.once("listening", () => {
90
106
  const address = server.httpServer?.address();
91
107
  const isAddressInfo = (x) => typeof x === "object";
@@ -93,11 +109,20 @@ function resolveLitestarPlugin(pluginConfig) {
93
109
  viteDevServerUrl = userConfig.server?.origin ? userConfig.server.origin : resolveDevServerUrl(address, server.config, userConfig);
94
110
  fs.mkdirSync(path.dirname(pluginConfig.hotFile), { recursive: true });
95
111
  fs.writeFileSync(pluginConfig.hotFile, viteDevServerUrl);
112
+ const hasIndex = shouldServeIndex();
96
113
  setTimeout(() => {
97
114
  server.config.logger.info(`
98
115
  ${colors.red(`${colors.bold("LITESTAR")} ${litestarVersion()}`)} ${colors.dim("plugin")} ${colors.bold(`v${pluginVersion()}`)}`);
99
116
  server.config.logger.info("");
100
- server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("APP_URL")}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`);
117
+ if (hasIndex) {
118
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("Serve Index")}: Serving application index with Vite`);
119
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("DEV URL")}: ${colors.cyan(viteDevServerUrl)}`);
120
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("APP_URL")}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`);
121
+ } else {
122
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("Serve Index")}: Serving Litestar index with Vite`);
123
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("DEV URL")}: ${colors.cyan(viteDevServerUrl)}`);
124
+ server.config.logger.info(` ${colors.green("\u279C")} ${colors.bold("APP_URL")}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`);
125
+ }
101
126
  }, 100);
102
127
  }
103
128
  });
@@ -114,7 +139,7 @@ function resolveLitestarPlugin(pluginConfig) {
114
139
  exitHandlersBound = true;
115
140
  }
116
141
  return () => server.middlewares.use((req, res, next) => {
117
- if (req.url === "/index.html") {
142
+ if (!shouldServeIndex() && req.url === "/index.html") {
118
143
  res.statusCode = 404;
119
144
  res.end(
120
145
  fs.readFileSync(path.join(dirname(), "dev-server-index.html")).toString().replace(/{{ APP_URL }}/g, appUrl)
@@ -187,6 +212,7 @@ function resolvePluginConfig(config) {
187
212
  refresh: resolvedConfig.refresh ?? false,
188
213
  hotFile: resolvedConfig.hotFile ?? path.join(resolvedConfig.bundleDirectory ?? "public", "hot"),
189
214
  detectTls: resolvedConfig.detectTls ?? false,
215
+ autoDetectIndex: resolvedConfig.autoDetectIndex ?? true,
190
216
  transformOnServe: resolvedConfig.transformOnServe ?? ((code) => code)
191
217
  };
192
218
  }
@@ -105,8 +105,9 @@ export function isRoute(url, routeName) {
105
105
  const regexPattern = routePattern.replace(/\//g, "\\/").replace(/\{([^}]+):([^}]+)\}/g, (match, paramName, paramType) => {
106
106
  switch (paramType) {
107
107
  case "str":
108
- case "path":
109
108
  return "([^/]+)";
109
+ case "path":
110
+ return "(.*)";
110
111
  case "uuid":
111
112
  return "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
112
113
  default:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.8.3",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "description": "Litestar plugin for Vite.",
6
6
  "keywords": ["litestar", "vite", "vite-plugin"],
@@ -42,13 +42,13 @@
42
42
  "happy-dom": "^15.11.7",
43
43
  "typescript": "^5.7.2",
44
44
  "vite": "^6.0.1",
45
- "vitest": "^2.1.6"
45
+ "vitest": "^2.1.8"
46
46
  },
47
47
  "peerDependencies": {
48
- "vite": ">=5.0.0"
48
+ "vite": "^5.0.0 || ^6.0.0"
49
49
  },
50
50
  "engines": {
51
- "node": "^18.0.0 || >=20.0.0"
51
+ "node": "^18.0.0 || ^20.0.0 || >=22.0.0"
52
52
  },
53
53
  "dependencies": {
54
54
  "picocolors": "^1.1.1",