litestar-vite-plugin 0.4.1 → 0.4.3

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/dist/index.d.ts CHANGED
@@ -49,9 +49,9 @@ interface PluginConfig {
49
49
  /**
50
50
  * Utilize TLS certificates.
51
51
  *
52
- * @default false
52
+ * @default null
53
53
  */
54
- detectTls?: string | boolean;
54
+ detectTls?: string | boolean | null;
55
55
  /**
56
56
  * Transform the code while serving.
57
57
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litestar-vite-plugin",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Litestar plugin for Vite.",
5
5
  "keywords": [
6
6
  "litestar",
@@ -48,14 +48,14 @@
48
48
  "esbuild": "0.16.10",
49
49
  "eslint": "^8.14.0",
50
50
  "typescript": "^4.6.4",
51
- "vite": "^4.0.0",
51
+ "vite": "^5.0.0",
52
52
  "vitest": "^0.34.4"
53
53
  },
54
54
  "peerDependencies": {
55
- "vite": "^3.0.0 || ^4.0.0 || ^5.0.0"
55
+ "vite": "^5.0.0"
56
56
  },
57
57
  "engines": {
58
- "node": ">=14"
58
+ "node": "^18.0.0 || >=20.0.0"
59
59
  },
60
60
  "dependencies": {
61
61
  "picocolors": "^1.0.0",
@@ -1,185 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1" />
6
-
7
- <title>Litestar Vite</title>
8
-
9
- <!-- Fonts -->
10
- <link
11
- href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap"
12
- rel="stylesheet"
13
- />
14
-
15
- <script src="https://cdn.tailwindcss.com"></script>
16
- </head>
17
- <body class="antialiased">
18
- <div
19
- class="relative flex min-h-screen flex-col justify-center overflow-hidden bg-gray-100 text-gray-600 dark:text-gray-400 dark:bg-gray-900 py-6 sm:py-12"
20
- >
21
- <div
22
- class="relative bg-white dark:bg-gray-800 px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-xl sm:rounded-lg sm:px-10"
23
- >
24
- <div class="mx-auto">
25
- <div class="flex items-center justify-center space-x-6">
26
- <a href="https://litestar.dev">
27
- <svg
28
- viewBox="0 52 410 250"
29
- xmlns="http://www.w3.org/2000/svg"
30
- class="h-16 w-auto"
31
- >
32
- <defs>
33
- <clipPath id="9eb7762d41">
34
- <path
35
- d="M 15.933594 105 L 328 105 L 328 259 L 15.933594 259 Z M 15.933594 105 "
36
- clip-rule="nonzero"
37
- />
38
- </clipPath>
39
- <clipPath id="183d3cc178">
40
- <path
41
- d="M 142 78.769531 L 359.433594 78.769531 L 359.433594 296.269531 L 142 296.269531 Z M 142 78.769531 "
42
- clip-rule="nonzero"
43
- />
44
- </clipPath>
45
- </defs>
46
- <g clip-path="url(#9eb7762d41)">
47
- <path
48
- fill="#edb641"
49
- d="M 147.625 240.3125 C 161.5 233.984375 173.554688 227.011719 183.425781 220.550781 C 202.304688 208.203125 226.4375 185.242188 227.761719 183.410156 L 218.917969 177.503906 L 211.257812 172.386719 L 235.503906 171.441406 L 243.296875 171.136719 L 245.414062 163.640625 L 252.007812 140.304688 L 260.402344 163.054688 L 263.097656 170.363281 L 270.890625 170.058594 L 295.136719 169.113281 L 276.078125 184.117188 L 269.953125 188.9375 L 272.652344 196.25 L 281.046875 218.996094 L 260.871094 205.523438 L 254.390625 201.195312 L 248.265625 206.015625 L 229.207031 221.023438 L 232.480469 209.425781 L 235.796875 197.691406 L 236.207031 196.234375 C 213.003906 213.585938 180.546875 230.304688 161.140625 236.488281 C 156.6875 237.90625 152.183594 239.179688 147.625 240.3125 Z M 101.992188 258.078125 C 136.382812 256.734375 177.355469 248 217.675781 222.363281 L 209.90625 249.867188 L 254.910156 214.4375 L 302.539062 246.246094 L 282.71875 192.539062 L 327.71875 157.109375 L 270.46875 159.34375 L 250.648438 105.636719 L 235.085938 160.726562 L 177.835938 162.964844 L 210.980469 185.097656 C 189.164062 204.921875 134.445312 247.195312 61.957031 250.03125 C 47.300781 250.601562 31.914062 249.558594 15.933594 246.394531 C 15.933594 246.394531 52.011719 260.035156 101.992188 258.078125 "
50
- fill-opacity="1"
51
- fill-rule="nonzero"
52
- />
53
- </g>
54
- <g clip-path="url(#183d3cc178)">
55
- <path
56
- fill="#edb641"
57
- d="M 250.789062 78.96875 C 190.78125 78.96875 142.140625 127.570312 142.140625 187.519531 C 142.140625 198.875 143.886719 209.816406 147.121094 220.101562 C 151.847656 217.75 156.363281 215.316406 160.660156 212.84375 C 158.394531 204.789062 157.183594 196.296875 157.183594 187.519531 C 157.183594 135.871094 199.089844 93.996094 250.789062 93.996094 C 302.484375 93.996094 344.390625 135.871094 344.390625 187.519531 C 344.390625 239.171875 302.484375 281.042969 250.789062 281.042969 C 222.75 281.042969 197.597656 268.722656 180.441406 249.210938 C 175.453125 251.152344 170.402344 252.917969 165.289062 254.511719 C 185.183594 279.816406 216.082031 296.070312 250.789062 296.070312 C 310.792969 296.070312 359.433594 247.472656 359.433594 187.519531 C 359.433594 127.570312 310.792969 78.96875 250.789062 78.96875 "
58
- fill-opacity="1"
59
- fill-rule="nonzero"
60
- />
61
- </g>
62
- <path
63
- fill="#edb641"
64
- d="M 92.292969 173.023438 L 98.289062 191.460938 L 117.691406 191.460938 L 101.992188 202.855469 L 107.988281 221.292969 L 92.292969 209.898438 L 76.59375 221.292969 L 82.589844 202.855469 L 66.894531 191.460938 L 86.296875 191.460938 L 92.292969 173.023438 "
65
- fill-opacity="1"
66
- fill-rule="nonzero"
67
- />
68
- <path
69
- fill="#edb641"
70
- d="M 120.214844 112.25 L 125.390625 128.167969 L 142.140625 128.167969 L 128.589844 138 L 133.765625 153.917969 L 120.214844 144.082031 L 106.664062 153.917969 L 111.839844 138 L 98.289062 128.167969 L 115.039062 128.167969 L 120.214844 112.25 "
71
- fill-opacity="1"
72
- fill-rule="nonzero"
73
- />
74
- <path
75
- fill="#edb641"
76
- d="M 34.695312 209.136719 L 37.71875 218.421875 L 47.492188 218.421875 L 39.585938 224.160156 L 42.605469 233.449219 L 34.695312 227.707031 L 26.792969 233.449219 L 29.8125 224.160156 L 21.90625 218.421875 L 31.679688 218.421875 L 34.695312 209.136719 "
77
- fill-opacity="1"
78
- fill-rule="nonzero"
79
- />
80
- </svg>
81
- </a>
82
- <svg
83
- xmlns="http://www.w3.org/2000/svg"
84
- class="h-6 w-6 text-gray-500"
85
- fill="none"
86
- viewBox="0 0 24 24"
87
- stroke="currentColor"
88
- stroke-width="2"
89
- >
90
- <path
91
- stroke-linecap="round"
92
- stroke-linejoin="round"
93
- d="M12 4v16m8-8H4"
94
- />
95
- </svg>
96
- <a href="https://vitejs.dev">
97
- <svg
98
- viewBox="0 0 410 404"
99
- fill="none"
100
- xmlns="http://www.w3.org/2000/svg"
101
- class="h-16 w-auto"
102
- >
103
- <path
104
- d="M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z"
105
- fill="url(#paint0_linear)"
106
- />
107
- <path
108
- d="M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z"
109
- fill="url(#paint1_linear)"
110
- />
111
- <defs>
112
- <linearGradient
113
- id="paint0_linear"
114
- x1="6.00017"
115
- y1="32.9999"
116
- x2="235"
117
- y2="344"
118
- gradientUnits="userSpaceOnUse"
119
- >
120
- <stop stop-color="#41D1FF" />
121
- <stop offset="1" stop-color="#BD34FE" />
122
- </linearGradient>
123
- <linearGradient
124
- id="paint1_linear"
125
- x1="194.651"
126
- y1="8.81818"
127
- x2="236.076"
128
- y2="292.989"
129
- gradientUnits="userSpaceOnUse"
130
- >
131
- <stop stop-color="#FFEA83" />
132
- <stop
133
- offset="0.0833333"
134
- stop-color="#FFDD35"
135
- />
136
- <stop offset="1" stop-color="#FFA800" />
137
- </linearGradient>
138
- </defs>
139
- </svg>
140
- </a>
141
- </div>
142
- <div class="divide-y divide-gray-300 dark:divide-gray-700">
143
- <div class="py-8 text-base leading-7">
144
- <p>
145
- This is the Vite development server that
146
- provides Hot Module Replacement for your
147
- Litestar application.
148
- </p>
149
- <p class="mt-6">
150
- To access your Litestar application, you will
151
- need to run a local development server.
152
- </p>
153
- </div>
154
- <div class="pt-8 text-base leading-7">
155
- <p>
156
- Your Litestar application's configured
157
- <code
158
- class="text-sm font-bold text-gray-900 dark:text-gray-100"
159
- >APP_URL</code
160
- >
161
- is:<br />
162
- <a
163
- href="{{ APP_URL }}"
164
- class="font-semibold text-red-500 hover:text-red-600"
165
- >{{ APP_URL }}</a
166
- >
167
- </p>
168
- <p class="mt-6">
169
- Want more information on Litestar's Vite
170
- integration?
171
- </p>
172
- <p>
173
- <a
174
- href="https://litestar.dev/docs/vite"
175
- class="font-semibold text-red-500 hover:text-red-600"
176
- >Read the docs &rarr;</a
177
- >
178
- </p>
179
- </div>
180
- </div>
181
- </div>
182
- </div>
183
- </div>
184
- </body>
185
- </html>
package/dist/index.cjs DELETED
@@ -1,418 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
- var src_exports = {};
26
- __export(src_exports, {
27
- default: () => litestar,
28
- refreshPaths: () => refreshPaths
29
- });
30
- module.exports = __toCommonJS(src_exports);
31
- var import_meta_url = typeof document === "undefined" ? new (require("url".replace("", ""))).URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
32
- var import_fs = __toESM(require("fs"));
33
- var import_url = require("url");
34
- var import_path = __toESM(require("path"));
35
- var import_picocolors = __toESM(require("picocolors"));
36
- var import_vite = require("vite");
37
- var import_vite_plugin_full_reload = __toESM(require("vite-plugin-full-reload"));
38
- let exitHandlersBound = false;
39
- const refreshPaths = [
40
- "**/*.py",
41
- "**/*.j2",
42
- "**/*.html.j2",
43
- "**/*.html",
44
- "**/assets/**/*"
45
- ];
46
- function litestar(config) {
47
- const pluginConfig = resolvePluginConfig(config);
48
- return [
49
- resolveLitestarPlugin(pluginConfig),
50
- ...resolveFullReloadConfig(pluginConfig)
51
- ];
52
- }
53
- function resolveLitestarPlugin(pluginConfig) {
54
- let viteDevServerUrl;
55
- let resolvedConfig;
56
- let userConfig;
57
- const defaultAliases = {
58
- "@": pluginConfig.resourceDirectory || "/resources/"
59
- };
60
- return {
61
- name: "litestar",
62
- enforce: "post",
63
- config: (config, { command, mode }) => {
64
- userConfig = config;
65
- const ssr = !!userConfig.build?.ssr;
66
- const env = (0, import_vite.loadEnv)(mode, userConfig.envDir || process.cwd(), "");
67
- const assetUrl = env.ASSET_URL || pluginConfig.assetUrl;
68
- const serverConfig = command === "serve" ? resolveDevelopmentEnvironmentServerConfig(
69
- pluginConfig.detectTls
70
- ) ?? resolveEnvironmentServerConfig(env) : void 0;
71
- ensureCommandShouldRunInEnvironment(command, env);
72
- return {
73
- base: userConfig.base ?? (command === "build" ? resolveBase(pluginConfig, assetUrl) : pluginConfig.assetUrl),
74
- publicDir: userConfig.publicDir ?? false,
75
- build: {
76
- manifest: userConfig.build?.manifest ?? !ssr,
77
- outDir: userConfig.build?.outDir ?? resolveOutDir(pluginConfig, ssr),
78
- rollupOptions: {
79
- input: userConfig.build?.rollupOptions?.input ?? resolveInput(pluginConfig, ssr)
80
- },
81
- assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0
82
- },
83
- server: {
84
- origin: userConfig.server?.origin ?? "__litestar_vite_placeholder__",
85
- ...process.env.VITE_ALLOW_REMOTE ? {
86
- host: userConfig.server?.host ?? "0.0.0.0",
87
- port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),
88
- strictPort: userConfig.server?.strictPort ?? true
89
- } : void 0,
90
- ...serverConfig ? {
91
- host: userConfig.server?.host ?? serverConfig.host,
92
- hmr: userConfig.server?.hmr === false ? false : {
93
- ...serverConfig.hmr,
94
- ...userConfig.server?.hmr === true ? {} : userConfig.server?.hmr
95
- },
96
- https: userConfig.server?.https === false ? false : {
97
- ...serverConfig.https,
98
- ...userConfig.server?.https === true ? {} : userConfig.server?.https
99
- }
100
- } : void 0
101
- },
102
- resolve: {
103
- alias: Array.isArray(userConfig.resolve?.alias) ? [
104
- ...userConfig.resolve?.alias ?? [],
105
- ...Object.keys(defaultAliases).map((alias) => ({
106
- find: alias,
107
- replacement: defaultAliases[alias]
108
- }))
109
- ] : {
110
- ...defaultAliases,
111
- ...userConfig.resolve?.alias
112
- }
113
- },
114
- ssr: {
115
- noExternal: noExternalInertiaHelpers(userConfig)
116
- }
117
- };
118
- },
119
- configResolved(config) {
120
- resolvedConfig = config;
121
- },
122
- transform(code) {
123
- if (resolvedConfig.command === "serve") {
124
- code = code.replace(
125
- /__litestar_vite_placeholder__/g,
126
- viteDevServerUrl
127
- );
128
- return pluginConfig.transformOnServe(code, viteDevServerUrl);
129
- }
130
- },
131
- configureServer(server) {
132
- const envDir = resolvedConfig.envDir || process.cwd();
133
- const appUrl = (0, import_vite.loadEnv)(resolvedConfig.mode, envDir, "APP_URL").APP_URL ?? "undefined";
134
- server.httpServer?.once("listening", () => {
135
- const address = server.httpServer?.address();
136
- const isAddressInfo = (x) => typeof x === "object";
137
- if (isAddressInfo(address)) {
138
- viteDevServerUrl = resolveDevServerUrl(
139
- address,
140
- server.config,
141
- userConfig
142
- );
143
- import_fs.default.writeFileSync(pluginConfig.hotFile, viteDevServerUrl);
144
- setTimeout(() => {
145
- server.config.logger.info(
146
- `
147
- ${import_picocolors.default.red(
148
- `${import_picocolors.default.bold(
149
- "LITESTAR"
150
- )} ${litestarVersion()}`
151
- )} ${import_picocolors.default.dim("plugin")} ${import_picocolors.default.bold(
152
- `v${pluginVersion()}`
153
- )}`
154
- );
155
- server.config.logger.info("");
156
- server.config.logger.info(
157
- ` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(
158
- "APP_URL"
159
- )}: ${import_picocolors.default.cyan(
160
- appUrl.replace(
161
- /:(\d+)/,
162
- (_, port) => `:${import_picocolors.default.bold(port)}`
163
- )
164
- )}`
165
- );
166
- }, 100);
167
- }
168
- });
169
- if (!exitHandlersBound) {
170
- const clean = () => {
171
- if (import_fs.default.existsSync(pluginConfig.hotFile)) {
172
- import_fs.default.rmSync(pluginConfig.hotFile);
173
- }
174
- };
175
- process.on("exit", clean);
176
- process.on("SIGINT", process.exit);
177
- process.on("SIGTERM", process.exit);
178
- process.on("SIGHUP", process.exit);
179
- exitHandlersBound = true;
180
- }
181
- return () => server.middlewares.use((req, res, next) => {
182
- if (req.url === "/index.html") {
183
- res.statusCode = 404;
184
- res.end(
185
- import_fs.default.readFileSync(
186
- import_path.default.join(
187
- dirname(),
188
- "dev-server-index.html"
189
- )
190
- ).toString().replace(/{{ APP_URL }}/g, appUrl)
191
- );
192
- }
193
- next();
194
- });
195
- }
196
- };
197
- }
198
- function ensureCommandShouldRunInEnvironment(command, env) {
199
- const validEnvironmentNames = ["dev", "development", "local", "docker"];
200
- if (command === "build" || env.LITESTAR_BYPASS_ENV_CHECK === "1") {
201
- return;
202
- }
203
- if (typeof env.LITESTAR_MODE !== "undefined" && validEnvironmentNames.some((e) => e === env.LITESTAR_MODE)) {
204
- throw Error(
205
- "You should only run Vite dev server when Litestar is development mode. You should build your assets for production instead. To disable this ENV check you may set LITESTAR_BYPASS_ENV_CHECK=1"
206
- );
207
- }
208
- if (typeof env.CI !== "undefined") {
209
- throw Error(
210
- "You should not run the Vite HMR server in CI environments. You should build your assets for production instead. To disable this ENV check you may set LITESTAR_BYPASS_ENV_CHECK=1"
211
- );
212
- }
213
- }
214
- function litestarVersion() {
215
- return "X.Y.Z";
216
- }
217
- function pluginVersion() {
218
- try {
219
- return JSON.parse(
220
- import_fs.default.readFileSync(import_path.default.join(dirname(), "../package.json")).toString()
221
- )?.version;
222
- } catch {
223
- return "";
224
- }
225
- }
226
- function resolvePluginConfig(config) {
227
- if (typeof config === "undefined") {
228
- throw new Error("litestar-vite-plugin: missing configuration.");
229
- }
230
- if (typeof config === "string" || Array.isArray(config)) {
231
- config = { input: config, ssr: config };
232
- }
233
- if (typeof config.input === "undefined") {
234
- throw new Error(
235
- 'litestar-vite-plugin: missing configuration for "input".'
236
- );
237
- }
238
- if (typeof config.resourceDirectory === "string") {
239
- config.resourceDirectory = config.resourceDirectory.trim().replace(/^\/+/, "");
240
- if (config.resourceDirectory === "") {
241
- throw new Error(
242
- "litestar-vite-plugin: resourceDirectory must be a subdirectory. E.g. 'resources'."
243
- );
244
- }
245
- }
246
- if (typeof config.bundleDirectory === "string") {
247
- config.bundleDirectory = config.bundleDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, "");
248
- if (config.bundleDirectory === "") {
249
- throw new Error(
250
- "litestar-vite-plugin: bundleDirectory must be a subdirectory. E.g. 'public'."
251
- );
252
- }
253
- }
254
- if (typeof config.ssrOutputDirectory === "string") {
255
- config.ssrOutputDirectory = config.ssrOutputDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, "");
256
- }
257
- if (config.refresh === true) {
258
- config.refresh = [{ paths: refreshPaths }];
259
- }
260
- return {
261
- input: config.input,
262
- assetUrl: config.assetUrl ?? "static",
263
- resourceDirectory: config.resourceDirectory ?? "/resources/",
264
- bundleDirectory: config.bundleDirectory || (config.bundleDirectory ?? "public"),
265
- ssr: config.ssr ?? config.input,
266
- ssrOutputDirectory: config.ssrOutputDirectory ?? import_path.default.join(config.resourceDirectory ?? "resources", "bootstrap/ssr"),
267
- refresh: config.refresh ?? false,
268
- hotFile: config.hotFile ?? import_path.default.join(config.bundleDirectory ?? "public", "hot"),
269
- detectTls: config.detectTls ?? false,
270
- transformOnServe: config.transformOnServe ?? ((code) => code)
271
- };
272
- }
273
- function resolveBase(config, assetUrl) {
274
- return assetUrl + (config.assetUrl.endsWith("/") ? "" : "/");
275
- }
276
- function resolveInput(config, ssr) {
277
- if (ssr) {
278
- return config.ssr;
279
- }
280
- return config.input;
281
- }
282
- function resolveOutDir(config, ssr) {
283
- if (ssr) {
284
- return config.ssrOutputDirectory;
285
- }
286
- return import_path.default.join(config.bundleDirectory);
287
- }
288
- function resolveFullReloadConfig({
289
- refresh: config
290
- }) {
291
- if (typeof config === "boolean") {
292
- return [];
293
- }
294
- if (typeof config === "string") {
295
- config = [{ paths: [config] }];
296
- }
297
- if (!Array.isArray(config)) {
298
- config = [config];
299
- }
300
- if (config.some((c) => typeof c === "string")) {
301
- config = [{ paths: config }];
302
- }
303
- return config.flatMap((c) => {
304
- const plugin = (0, import_vite_plugin_full_reload.default)(c.paths, c.config);
305
- plugin.__litestar_plugin_config = c;
306
- return plugin;
307
- });
308
- }
309
- function resolveDevServerUrl(address, config, userConfig) {
310
- const configHmrProtocol = typeof config.server.hmr === "object" ? config.server.hmr.protocol : null;
311
- const clientProtocol = configHmrProtocol ? configHmrProtocol === "wss" ? "https" : "http" : null;
312
- const serverProtocol = config.server.https ? "https" : "http";
313
- const protocol = clientProtocol ?? serverProtocol;
314
- const configHmrHost = typeof config.server.hmr === "object" ? config.server.hmr.host : null;
315
- const configHost = typeof config.server.host === "string" ? config.server.host : null;
316
- const remoteHost = process.env.VITE_ALLOW_REMOTE && !userConfig.server?.host ? "localhost" : null;
317
- const serverAddress = isIpv6(address) ? `[${address.address}]` : address.address;
318
- const host = configHmrHost ?? remoteHost ?? configHost ?? serverAddress;
319
- const configHmrClientPort = typeof config.server.hmr === "object" ? config.server.hmr.clientPort : null;
320
- const port = configHmrClientPort ?? address.port;
321
- return `${protocol}://${host}:${port}`;
322
- }
323
- function isIpv6(address) {
324
- return address.family === "IPv6" || address.family === 6;
325
- }
326
- function noExternalInertiaHelpers(config) {
327
- const userNoExternal = config.ssr?.noExternal;
328
- const pluginNoExternal = ["litestar-vite-plugin"];
329
- if (userNoExternal === true) {
330
- return true;
331
- }
332
- if (typeof userNoExternal === "undefined") {
333
- return pluginNoExternal;
334
- }
335
- return [
336
- ...Array.isArray(userNoExternal) ? userNoExternal : [userNoExternal],
337
- ...pluginNoExternal
338
- ];
339
- }
340
- function resolveEnvironmentServerConfig(env) {
341
- if (!env.VITE_SERVER_KEY && !env.VITE_SERVER_CERT) {
342
- return;
343
- }
344
- if (!import_fs.default.existsSync(env.VITE_SERVER_KEY) || !import_fs.default.existsSync(env.VITE_SERVER_CERT)) {
345
- throw Error(
346
- `Unable to find the certificate files specified in your environment. Ensure you have correctly configured VITE_SERVER_KEY: [${env.VITE_SERVER_KEY}] and VITE_SERVER_CERT: [${env.VITE_SERVER_CERT}].`
347
- );
348
- }
349
- const host = resolveHostFromEnv(env);
350
- if (!host) {
351
- throw Error(
352
- `Unable to determine the host from the environment's APP_URL: [${env.APP_URL}].`
353
- );
354
- }
355
- return {
356
- hmr: { host },
357
- host,
358
- https: {
359
- key: import_fs.default.readFileSync(env.VITE_SERVER_KEY),
360
- cert: import_fs.default.readFileSync(env.VITE_SERVER_CERT)
361
- }
362
- };
363
- }
364
- function resolveHostFromEnv(env) {
365
- try {
366
- return new URL(env.APP_URL).host;
367
- } catch {
368
- return;
369
- }
370
- }
371
- function resolveDevelopmentEnvironmentServerConfig(host) {
372
- if (host === false) {
373
- return;
374
- }
375
- const configPath = determineDevelopmentEnvironmentConfigPath();
376
- host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host;
377
- const keyPath = import_path.default.resolve(configPath, "certs", `${host}.key`);
378
- const certPath = import_path.default.resolve(configPath, "certs", `${host}.crt`);
379
- if (!import_fs.default.existsSync(keyPath) || !import_fs.default.existsSync(certPath)) {
380
- throw Error(
381
- `Unable to find certificate files for your host [${host}] in the [${configPath}/certs] directory.`
382
- );
383
- }
384
- return {
385
- hmr: { host },
386
- host,
387
- https: {
388
- key: import_fs.default.readFileSync(keyPath),
389
- cert: import_fs.default.readFileSync(certPath)
390
- }
391
- };
392
- }
393
- function determineDevelopmentEnvironmentConfigPath() {
394
- const envConfigPath = import_path.default.resolve(process.cwd(), ".config");
395
- if (import_fs.default.existsSync(envConfigPath)) {
396
- return envConfigPath;
397
- }
398
- return import_path.default.resolve(process.cwd(), ".config");
399
- }
400
- function resolveDevelopmentEnvironmentHost(configPath) {
401
- const configFile = import_path.default.resolve(configPath, "config.json");
402
- if (!import_fs.default.existsSync(configFile)) {
403
- throw Error(
404
- `Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`detectTls\` configuration option.`
405
- );
406
- }
407
- const config = JSON.parse(
408
- import_fs.default.readFileSync(configFile, "utf-8")
409
- );
410
- return import_path.default.basename(process.cwd()) + "." + config.tld;
411
- }
412
- function dirname() {
413
- return (0, import_url.fileURLToPath)(new URL(".", import_meta_url));
414
- }
415
- // Annotate the CommonJS export names for ESM import in node:
416
- 0 && (module.exports = {
417
- refreshPaths
418
- });
package/dist/index.mjs DELETED
@@ -1,389 +0,0 @@
1
- import fs from "fs";
2
- import { fileURLToPath } from "url";
3
- import path from "path";
4
- import colors from "picocolors";
5
- import {
6
- loadEnv
7
- } from "vite";
8
- import fullReload from "vite-plugin-full-reload";
9
- let exitHandlersBound = false;
10
- const refreshPaths = [
11
- "**/*.py",
12
- "**/*.j2",
13
- "**/*.html.j2",
14
- "**/*.html",
15
- "**/assets/**/*"
16
- ];
17
- function litestar(config) {
18
- const pluginConfig = resolvePluginConfig(config);
19
- return [
20
- resolveLitestarPlugin(pluginConfig),
21
- ...resolveFullReloadConfig(pluginConfig)
22
- ];
23
- }
24
- function resolveLitestarPlugin(pluginConfig) {
25
- let viteDevServerUrl;
26
- let resolvedConfig;
27
- let userConfig;
28
- const defaultAliases = {
29
- "@": pluginConfig.resourceDirectory || "/resources/"
30
- };
31
- return {
32
- name: "litestar",
33
- enforce: "post",
34
- config: (config, { command, mode }) => {
35
- userConfig = config;
36
- const ssr = !!userConfig.build?.ssr;
37
- const env = loadEnv(mode, userConfig.envDir || process.cwd(), "");
38
- const assetUrl = env.ASSET_URL || pluginConfig.assetUrl;
39
- const serverConfig = command === "serve" ? resolveDevelopmentEnvironmentServerConfig(
40
- pluginConfig.detectTls
41
- ) ?? resolveEnvironmentServerConfig(env) : void 0;
42
- ensureCommandShouldRunInEnvironment(command, env);
43
- return {
44
- base: userConfig.base ?? (command === "build" ? resolveBase(pluginConfig, assetUrl) : pluginConfig.assetUrl),
45
- publicDir: userConfig.publicDir ?? false,
46
- build: {
47
- manifest: userConfig.build?.manifest ?? !ssr,
48
- outDir: userConfig.build?.outDir ?? resolveOutDir(pluginConfig, ssr),
49
- rollupOptions: {
50
- input: userConfig.build?.rollupOptions?.input ?? resolveInput(pluginConfig, ssr)
51
- },
52
- assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0
53
- },
54
- server: {
55
- origin: userConfig.server?.origin ?? "__litestar_vite_placeholder__",
56
- ...process.env.VITE_ALLOW_REMOTE ? {
57
- host: userConfig.server?.host ?? "0.0.0.0",
58
- port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),
59
- strictPort: userConfig.server?.strictPort ?? true
60
- } : void 0,
61
- ...serverConfig ? {
62
- host: userConfig.server?.host ?? serverConfig.host,
63
- hmr: userConfig.server?.hmr === false ? false : {
64
- ...serverConfig.hmr,
65
- ...userConfig.server?.hmr === true ? {} : userConfig.server?.hmr
66
- },
67
- https: userConfig.server?.https === false ? false : {
68
- ...serverConfig.https,
69
- ...userConfig.server?.https === true ? {} : userConfig.server?.https
70
- }
71
- } : void 0
72
- },
73
- resolve: {
74
- alias: Array.isArray(userConfig.resolve?.alias) ? [
75
- ...userConfig.resolve?.alias ?? [],
76
- ...Object.keys(defaultAliases).map((alias) => ({
77
- find: alias,
78
- replacement: defaultAliases[alias]
79
- }))
80
- ] : {
81
- ...defaultAliases,
82
- ...userConfig.resolve?.alias
83
- }
84
- },
85
- ssr: {
86
- noExternal: noExternalInertiaHelpers(userConfig)
87
- }
88
- };
89
- },
90
- configResolved(config) {
91
- resolvedConfig = config;
92
- },
93
- transform(code) {
94
- if (resolvedConfig.command === "serve") {
95
- code = code.replace(
96
- /__litestar_vite_placeholder__/g,
97
- viteDevServerUrl
98
- );
99
- return pluginConfig.transformOnServe(code, viteDevServerUrl);
100
- }
101
- },
102
- configureServer(server) {
103
- const envDir = resolvedConfig.envDir || process.cwd();
104
- const appUrl = loadEnv(resolvedConfig.mode, envDir, "APP_URL").APP_URL ?? "undefined";
105
- server.httpServer?.once("listening", () => {
106
- const address = server.httpServer?.address();
107
- const isAddressInfo = (x) => typeof x === "object";
108
- if (isAddressInfo(address)) {
109
- viteDevServerUrl = resolveDevServerUrl(
110
- address,
111
- server.config,
112
- userConfig
113
- );
114
- fs.writeFileSync(pluginConfig.hotFile, viteDevServerUrl);
115
- setTimeout(() => {
116
- server.config.logger.info(
117
- `
118
- ${colors.red(
119
- `${colors.bold(
120
- "LITESTAR"
121
- )} ${litestarVersion()}`
122
- )} ${colors.dim("plugin")} ${colors.bold(
123
- `v${pluginVersion()}`
124
- )}`
125
- );
126
- server.config.logger.info("");
127
- server.config.logger.info(
128
- ` ${colors.green("\u279C")} ${colors.bold(
129
- "APP_URL"
130
- )}: ${colors.cyan(
131
- appUrl.replace(
132
- /:(\d+)/,
133
- (_, port) => `:${colors.bold(port)}`
134
- )
135
- )}`
136
- );
137
- }, 100);
138
- }
139
- });
140
- if (!exitHandlersBound) {
141
- const clean = () => {
142
- if (fs.existsSync(pluginConfig.hotFile)) {
143
- fs.rmSync(pluginConfig.hotFile);
144
- }
145
- };
146
- process.on("exit", clean);
147
- process.on("SIGINT", process.exit);
148
- process.on("SIGTERM", process.exit);
149
- process.on("SIGHUP", process.exit);
150
- exitHandlersBound = true;
151
- }
152
- return () => server.middlewares.use((req, res, next) => {
153
- if (req.url === "/index.html") {
154
- res.statusCode = 404;
155
- res.end(
156
- fs.readFileSync(
157
- path.join(
158
- dirname(),
159
- "dev-server-index.html"
160
- )
161
- ).toString().replace(/{{ APP_URL }}/g, appUrl)
162
- );
163
- }
164
- next();
165
- });
166
- }
167
- };
168
- }
169
- function ensureCommandShouldRunInEnvironment(command, env) {
170
- const validEnvironmentNames = ["dev", "development", "local", "docker"];
171
- if (command === "build" || env.LITESTAR_BYPASS_ENV_CHECK === "1") {
172
- return;
173
- }
174
- if (typeof env.LITESTAR_MODE !== "undefined" && validEnvironmentNames.some((e) => e === env.LITESTAR_MODE)) {
175
- throw Error(
176
- "You should only run Vite dev server when Litestar is development mode. You should build your assets for production instead. To disable this ENV check you may set LITESTAR_BYPASS_ENV_CHECK=1"
177
- );
178
- }
179
- if (typeof env.CI !== "undefined") {
180
- throw Error(
181
- "You should not run the Vite HMR server in CI environments. You should build your assets for production instead. To disable this ENV check you may set LITESTAR_BYPASS_ENV_CHECK=1"
182
- );
183
- }
184
- }
185
- function litestarVersion() {
186
- return "X.Y.Z";
187
- }
188
- function pluginVersion() {
189
- try {
190
- return JSON.parse(
191
- fs.readFileSync(path.join(dirname(), "../package.json")).toString()
192
- )?.version;
193
- } catch {
194
- return "";
195
- }
196
- }
197
- function resolvePluginConfig(config) {
198
- if (typeof config === "undefined") {
199
- throw new Error("litestar-vite-plugin: missing configuration.");
200
- }
201
- if (typeof config === "string" || Array.isArray(config)) {
202
- config = { input: config, ssr: config };
203
- }
204
- if (typeof config.input === "undefined") {
205
- throw new Error(
206
- 'litestar-vite-plugin: missing configuration for "input".'
207
- );
208
- }
209
- if (typeof config.resourceDirectory === "string") {
210
- config.resourceDirectory = config.resourceDirectory.trim().replace(/^\/+/, "");
211
- if (config.resourceDirectory === "") {
212
- throw new Error(
213
- "litestar-vite-plugin: resourceDirectory must be a subdirectory. E.g. 'resources'."
214
- );
215
- }
216
- }
217
- if (typeof config.bundleDirectory === "string") {
218
- config.bundleDirectory = config.bundleDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, "");
219
- if (config.bundleDirectory === "") {
220
- throw new Error(
221
- "litestar-vite-plugin: bundleDirectory must be a subdirectory. E.g. 'public'."
222
- );
223
- }
224
- }
225
- if (typeof config.ssrOutputDirectory === "string") {
226
- config.ssrOutputDirectory = config.ssrOutputDirectory.trim().replace(/^\/+/, "").replace(/\/+$/, "");
227
- }
228
- if (config.refresh === true) {
229
- config.refresh = [{ paths: refreshPaths }];
230
- }
231
- return {
232
- input: config.input,
233
- assetUrl: config.assetUrl ?? "static",
234
- resourceDirectory: config.resourceDirectory ?? "/resources/",
235
- bundleDirectory: config.bundleDirectory || (config.bundleDirectory ?? "public"),
236
- ssr: config.ssr ?? config.input,
237
- ssrOutputDirectory: config.ssrOutputDirectory ?? path.join(config.resourceDirectory ?? "resources", "bootstrap/ssr"),
238
- refresh: config.refresh ?? false,
239
- hotFile: config.hotFile ?? path.join(config.bundleDirectory ?? "public", "hot"),
240
- detectTls: config.detectTls ?? false,
241
- transformOnServe: config.transformOnServe ?? ((code) => code)
242
- };
243
- }
244
- function resolveBase(config, assetUrl) {
245
- return assetUrl + (config.assetUrl.endsWith("/") ? "" : "/");
246
- }
247
- function resolveInput(config, ssr) {
248
- if (ssr) {
249
- return config.ssr;
250
- }
251
- return config.input;
252
- }
253
- function resolveOutDir(config, ssr) {
254
- if (ssr) {
255
- return config.ssrOutputDirectory;
256
- }
257
- return path.join(config.bundleDirectory);
258
- }
259
- function resolveFullReloadConfig({
260
- refresh: config
261
- }) {
262
- if (typeof config === "boolean") {
263
- return [];
264
- }
265
- if (typeof config === "string") {
266
- config = [{ paths: [config] }];
267
- }
268
- if (!Array.isArray(config)) {
269
- config = [config];
270
- }
271
- if (config.some((c) => typeof c === "string")) {
272
- config = [{ paths: config }];
273
- }
274
- return config.flatMap((c) => {
275
- const plugin = fullReload(c.paths, c.config);
276
- plugin.__litestar_plugin_config = c;
277
- return plugin;
278
- });
279
- }
280
- function resolveDevServerUrl(address, config, userConfig) {
281
- const configHmrProtocol = typeof config.server.hmr === "object" ? config.server.hmr.protocol : null;
282
- const clientProtocol = configHmrProtocol ? configHmrProtocol === "wss" ? "https" : "http" : null;
283
- const serverProtocol = config.server.https ? "https" : "http";
284
- const protocol = clientProtocol ?? serverProtocol;
285
- const configHmrHost = typeof config.server.hmr === "object" ? config.server.hmr.host : null;
286
- const configHost = typeof config.server.host === "string" ? config.server.host : null;
287
- const remoteHost = process.env.VITE_ALLOW_REMOTE && !userConfig.server?.host ? "localhost" : null;
288
- const serverAddress = isIpv6(address) ? `[${address.address}]` : address.address;
289
- const host = configHmrHost ?? remoteHost ?? configHost ?? serverAddress;
290
- const configHmrClientPort = typeof config.server.hmr === "object" ? config.server.hmr.clientPort : null;
291
- const port = configHmrClientPort ?? address.port;
292
- return `${protocol}://${host}:${port}`;
293
- }
294
- function isIpv6(address) {
295
- return address.family === "IPv6" || address.family === 6;
296
- }
297
- function noExternalInertiaHelpers(config) {
298
- const userNoExternal = config.ssr?.noExternal;
299
- const pluginNoExternal = ["litestar-vite-plugin"];
300
- if (userNoExternal === true) {
301
- return true;
302
- }
303
- if (typeof userNoExternal === "undefined") {
304
- return pluginNoExternal;
305
- }
306
- return [
307
- ...Array.isArray(userNoExternal) ? userNoExternal : [userNoExternal],
308
- ...pluginNoExternal
309
- ];
310
- }
311
- function resolveEnvironmentServerConfig(env) {
312
- if (!env.VITE_SERVER_KEY && !env.VITE_SERVER_CERT) {
313
- return;
314
- }
315
- if (!fs.existsSync(env.VITE_SERVER_KEY) || !fs.existsSync(env.VITE_SERVER_CERT)) {
316
- throw Error(
317
- `Unable to find the certificate files specified in your environment. Ensure you have correctly configured VITE_SERVER_KEY: [${env.VITE_SERVER_KEY}] and VITE_SERVER_CERT: [${env.VITE_SERVER_CERT}].`
318
- );
319
- }
320
- const host = resolveHostFromEnv(env);
321
- if (!host) {
322
- throw Error(
323
- `Unable to determine the host from the environment's APP_URL: [${env.APP_URL}].`
324
- );
325
- }
326
- return {
327
- hmr: { host },
328
- host,
329
- https: {
330
- key: fs.readFileSync(env.VITE_SERVER_KEY),
331
- cert: fs.readFileSync(env.VITE_SERVER_CERT)
332
- }
333
- };
334
- }
335
- function resolveHostFromEnv(env) {
336
- try {
337
- return new URL(env.APP_URL).host;
338
- } catch {
339
- return;
340
- }
341
- }
342
- function resolveDevelopmentEnvironmentServerConfig(host) {
343
- if (host === false) {
344
- return;
345
- }
346
- const configPath = determineDevelopmentEnvironmentConfigPath();
347
- host = host === true ? resolveDevelopmentEnvironmentHost(configPath) : host;
348
- const keyPath = path.resolve(configPath, "certs", `${host}.key`);
349
- const certPath = path.resolve(configPath, "certs", `${host}.crt`);
350
- if (!fs.existsSync(keyPath) || !fs.existsSync(certPath)) {
351
- throw Error(
352
- `Unable to find certificate files for your host [${host}] in the [${configPath}/certs] directory.`
353
- );
354
- }
355
- return {
356
- hmr: { host },
357
- host,
358
- https: {
359
- key: fs.readFileSync(keyPath),
360
- cert: fs.readFileSync(certPath)
361
- }
362
- };
363
- }
364
- function determineDevelopmentEnvironmentConfigPath() {
365
- const envConfigPath = path.resolve(process.cwd(), ".config");
366
- if (fs.existsSync(envConfigPath)) {
367
- return envConfigPath;
368
- }
369
- return path.resolve(process.cwd(), ".config");
370
- }
371
- function resolveDevelopmentEnvironmentHost(configPath) {
372
- const configFile = path.resolve(configPath, "config.json");
373
- if (!fs.existsSync(configFile)) {
374
- throw Error(
375
- `Unable to find the configuration file [${configFile}]. You will need to manually specify the host in the \`detectTls\` configuration option.`
376
- );
377
- }
378
- const config = JSON.parse(
379
- fs.readFileSync(configFile, "utf-8")
380
- );
381
- return path.basename(process.cwd()) + "." + config.tld;
382
- }
383
- function dirname() {
384
- return fileURLToPath(new URL(".", import.meta.url));
385
- }
386
- export {
387
- litestar as default,
388
- refreshPaths
389
- };