vite-plugin-backend-html 0.2.1 → 0.2.2
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.mts +46 -33
- package/dist/index.mjs +20 -18
- package/package.json +16 -16
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Plugin } from "vite";
|
|
2
2
|
import { IncomingMessage } from "node:http";
|
|
3
|
-
|
|
4
|
-
//#region src/backend-proxy.d.ts
|
|
5
3
|
/**
|
|
6
4
|
* Logging levels supported by the proxy
|
|
7
5
|
* @public
|
|
@@ -12,20 +10,33 @@ type LoggingLevel = 'error' | 'info' | 'debug';
|
|
|
12
10
|
* @public
|
|
13
11
|
*/
|
|
14
12
|
type BackendResult = {
|
|
15
|
-
/** HTML response */
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/** HTML response */
|
|
14
|
+
type: 'html';
|
|
15
|
+
/** HTML content */
|
|
16
|
+
content: string;
|
|
17
|
+
/** Optional module name for asset injection */
|
|
18
|
+
module: string | null;
|
|
19
|
+
/** Optional headers to forward (e.g. set-cookie) */
|
|
18
20
|
headers?: Record<string, string | string[]>;
|
|
19
21
|
} | {
|
|
20
|
-
/** Binary response */
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
/** Binary response */
|
|
23
|
+
type: 'binary';
|
|
24
|
+
/** Binary content */
|
|
25
|
+
content: Buffer;
|
|
26
|
+
/** Content type */
|
|
27
|
+
contentType: string;
|
|
28
|
+
/** HTTP status code */
|
|
29
|
+
status: number;
|
|
30
|
+
/** Optional headers to forward (e.g. set-cookie) */
|
|
24
31
|
headers?: Record<string, string | string[]>;
|
|
25
32
|
} | {
|
|
26
|
-
/** Redirect response */
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
/** Redirect response */
|
|
34
|
+
type: 'redirect';
|
|
35
|
+
/** HTTP status code */
|
|
36
|
+
status: number;
|
|
37
|
+
/** Redirect location */
|
|
38
|
+
location: string;
|
|
39
|
+
/** Optional headers to forward (e.g. set-cookie) */
|
|
29
40
|
headers?: Record<string, string | string[]>;
|
|
30
41
|
};
|
|
31
42
|
/**
|
|
@@ -33,12 +44,19 @@ type BackendResult = {
|
|
|
33
44
|
* @public
|
|
34
45
|
*/
|
|
35
46
|
type BackendHandler = (context: {
|
|
36
|
-
/** Request URL */
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
/** Request URL */
|
|
48
|
+
url: string;
|
|
49
|
+
/** Node request object */
|
|
50
|
+
req: IncomingMessage;
|
|
51
|
+
/** HTTP method */
|
|
52
|
+
method: string;
|
|
53
|
+
/** Request headers */
|
|
54
|
+
headers: Record<string, string | string[] | undefined>;
|
|
55
|
+
/** Request body for POST requests */
|
|
56
|
+
body: Buffer | undefined;
|
|
57
|
+
/** Backend base URL */
|
|
58
|
+
backendBaseUrl: string;
|
|
59
|
+
/** Logging function */
|
|
42
60
|
logger: (level: LoggingLevel, message: string, data?: unknown) => void;
|
|
43
61
|
}) => Promise<BackendResult>;
|
|
44
62
|
type EntryPoints = {
|
|
@@ -103,7 +121,7 @@ type ProxyConfig = {
|
|
|
103
121
|
* @returns {BackendHandler} Backend handler
|
|
104
122
|
* @public
|
|
105
123
|
*/
|
|
106
|
-
declare const createFetchBackendHandler: (options: {
|
|
124
|
+
export declare const createFetchBackendHandler: (options: {
|
|
107
125
|
extractModule?: (url: string, html: string, headers: Headers) => string | null | Promise<string | null>;
|
|
108
126
|
transformBackendUrl?: (url: string, backendBaseUrl: string) => string;
|
|
109
127
|
}) => BackendHandler;
|
|
@@ -114,7 +132,7 @@ declare const createFetchBackendHandler: (options: {
|
|
|
114
132
|
* @returns {string | null} Extracted module or null
|
|
115
133
|
* @public
|
|
116
134
|
*/
|
|
117
|
-
declare const extractModuleFromMeta: (html: string, metaName?: string) => string | null;
|
|
135
|
+
export declare const extractModuleFromMeta: (html: string, metaName?: string) => string | null;
|
|
118
136
|
/**
|
|
119
137
|
* Helper: Extract module from URL based on pattern
|
|
120
138
|
* @param {string} url - Request URL
|
|
@@ -122,9 +140,7 @@ declare const extractModuleFromMeta: (html: string, metaName?: string) => string
|
|
|
122
140
|
* @returns {string | null} Extracted module or null
|
|
123
141
|
* @public
|
|
124
142
|
*/
|
|
125
|
-
declare const extractModuleFromUrl: (url: string, pattern: RegExp) => string | null;
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/path-utils.d.ts
|
|
143
|
+
export declare const extractModuleFromUrl: (url: string, pattern: RegExp) => string | null;
|
|
128
144
|
/**
|
|
129
145
|
* Path normalization utilities for URL construction
|
|
130
146
|
*/
|
|
@@ -134,21 +150,21 @@ declare const extractModuleFromUrl: (url: string, pattern: RegExp) => string | n
|
|
|
134
150
|
* @returns {string} Path without trailing slashes
|
|
135
151
|
* @public
|
|
136
152
|
*/
|
|
137
|
-
declare const stripTrailingSlash: (path: string) => string;
|
|
153
|
+
export declare const stripTrailingSlash: (path: string) => string;
|
|
138
154
|
/**
|
|
139
155
|
* Strips leading slashes from path
|
|
140
156
|
* @param {string} path - Path to normalize
|
|
141
157
|
* @returns {string} Path without leading slashes
|
|
142
158
|
* @public
|
|
143
159
|
*/
|
|
144
|
-
declare const stripLeadingSlash: (path: string) => string;
|
|
160
|
+
export declare const stripLeadingSlash: (path: string) => string;
|
|
145
161
|
/**
|
|
146
162
|
* Ensures path has exactly one leading slash
|
|
147
163
|
* @param {string} path - Path to normalize
|
|
148
164
|
* @returns {string} Path with single leading slash
|
|
149
165
|
* @public
|
|
150
166
|
*/
|
|
151
|
-
declare const ensureLeadingSlash: (path: string) => string;
|
|
167
|
+
export declare const ensureLeadingSlash: (path: string) => string;
|
|
152
168
|
/**
|
|
153
169
|
* Joins URL parts, ensuring no double slashes
|
|
154
170
|
* @param {string} base - Base URL (trailing slash removed)
|
|
@@ -156,7 +172,7 @@ declare const ensureLeadingSlash: (path: string) => string;
|
|
|
156
172
|
* @returns {string} Joined URL
|
|
157
173
|
* @public
|
|
158
174
|
*/
|
|
159
|
-
declare const joinUrlParts: (base: string, path: string) => string;
|
|
175
|
+
export declare const joinUrlParts: (base: string, path: string) => string;
|
|
160
176
|
/**
|
|
161
177
|
* Normalizes redirect location relative to base path
|
|
162
178
|
* Handles:
|
|
@@ -169,14 +185,11 @@ declare const joinUrlParts: (base: string, path: string) => string;
|
|
|
169
185
|
* @returns {string} Normalized location with single leading slash
|
|
170
186
|
* @public
|
|
171
187
|
*/
|
|
172
|
-
declare const normalizeRedirectLocation: (location: string, basePath: string) => string;
|
|
173
|
-
//#endregion
|
|
174
|
-
//#region src/index.d.ts
|
|
188
|
+
export declare const normalizeRedirectLocation: (location: string, basePath: string) => string;
|
|
175
189
|
/**
|
|
176
190
|
* Backend Proxy Plugin
|
|
177
191
|
* @param {ProxyConfig} config - Plugin configuration
|
|
178
192
|
* @returns {Plugin} Vite plugin
|
|
179
193
|
*/
|
|
180
|
-
declare const backendProxyPlugin: (config: ProxyConfig) => Plugin;
|
|
181
|
-
|
|
182
|
-
export { type AssetConfig, type BackendHandler, type BackendResult, type LoggingLevel, type ProxyConfig, backendProxyPlugin, createFetchBackendHandler, ensureLeadingSlash, extractModuleFromMeta, extractModuleFromUrl, joinUrlParts, normalizeRedirectLocation, stripLeadingSlash, stripTrailingSlash };
|
|
194
|
+
export declare const backendProxyPlugin: (config: ProxyConfig) => Plugin;
|
|
195
|
+
export type { AssetConfig, BackendHandler, BackendResult, LoggingLevel, ProxyConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/path-utils.ts
|
|
2
2
|
/**
|
|
3
3
|
* Path normalization utilities for URL construction
|
|
4
4
|
*/
|
|
@@ -52,8 +52,7 @@ const normalizeRedirectLocation = (location, basePath) => {
|
|
|
52
52
|
if (normalized.startsWith(basePath)) normalized = normalized.slice(basePath.length);
|
|
53
53
|
return ensureLeadingSlash(normalized);
|
|
54
54
|
};
|
|
55
|
-
|
|
56
|
-
//#region src/backend-proxy.ts
|
|
55
|
+
// src/backend-proxy.ts
|
|
57
56
|
/**
|
|
58
57
|
* Returns current date-time in format: HH24:MI:SS.MS
|
|
59
58
|
* @returns {string} Formatted timestamp.
|
|
@@ -93,11 +92,13 @@ var BackendProxy = class {
|
|
|
93
92
|
if (configIndex === -1) throw new Error(`Invalid configLevel ${configLevel}`);
|
|
94
93
|
const messageIndex = levels.indexOf(level);
|
|
95
94
|
if (messageIndex === -1) throw new Error(`Invalid messageIndex ${messageIndex}`);
|
|
96
|
-
if (messageIndex <= configIndex)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
if (messageIndex <= configIndex) {
|
|
96
|
+
if (message.length > 0) {
|
|
97
|
+
const prefix = `${nowUtcCompact()} [backend-proxy] ${level.toUpperCase().padEnd(5)} ${message}`;
|
|
98
|
+
if (level === "error") console.error(prefix, data ?? "");
|
|
99
|
+
else console.log(prefix, data ?? "");
|
|
100
|
+
} else console.log("");
|
|
101
|
+
}
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
104
|
* Handles a request from Vite and proxies it to the backend if appropriate
|
|
@@ -209,8 +210,8 @@ var BackendProxy = class {
|
|
|
209
210
|
inject({ js: "/@vite/client" });
|
|
210
211
|
inject(globalEntryPoints);
|
|
211
212
|
if (typeof module === "string") inject(getModuleEntryPoints(module));
|
|
212
|
-
let processedHtml = html.replaceAll(/<link [^>]*href="(?:\/assets\/|\/q\/p\/)[^"]+"[^>]*>/
|
|
213
|
-
processedHtml = processedHtml.replaceAll(/<script [^>]*src="(?:\/assets\/|\/q\/p\/)[^"]+"[^>]*><\/script>/
|
|
213
|
+
let processedHtml = html.replaceAll(/<link [^>]*href="(?:\/assets\/|\/q\/p\/)[^"]+"[^>]*>/gu, "");
|
|
214
|
+
processedHtml = processedHtml.replaceAll(/<script [^>]*src="(?:\/assets\/|\/q\/p\/)[^"]+"[^>]*><\/script>/gu, "");
|
|
214
215
|
if (processedHtml.includes("</head>")) return processedHtml.replace("</head>", `${injection.join("\n")}\n</head>`);
|
|
215
216
|
if (processedHtml.includes("<body>")) return processedHtml.replace("<body>", `<body>\n${injection.join("\n")}`);
|
|
216
217
|
return injection.join("\n") + processedHtml;
|
|
@@ -223,7 +224,7 @@ var BackendProxy = class {
|
|
|
223
224
|
shouldSkipForVite(url) {
|
|
224
225
|
if (this.config.bypass(url)) return true;
|
|
225
226
|
if (url.startsWith("/@") || url.startsWith("/node_modules/")) return true;
|
|
226
|
-
if (/\.(?:ts|tsx|js|jsx|mjs)
|
|
227
|
+
if (/\.(?:ts|tsx|js|jsx|mjs)$/u.test(url) || /\.css(?:\?|$)/u.test(url)) return true;
|
|
227
228
|
return false;
|
|
228
229
|
}
|
|
229
230
|
};
|
|
@@ -239,8 +240,10 @@ const createFetchBackendHandler = (options) => async ({ url, method, headers, bo
|
|
|
239
240
|
const backendUrl = options.transformBackendUrl ? options.transformBackendUrl(url, backendBaseUrl) : `${backendBaseUrl}${url}`;
|
|
240
241
|
logger("debug", `backendBaseUrl="${backendBaseUrl}" args.url="${url}" url="${backendUrl}"`);
|
|
241
242
|
const fetchHeaders = new Headers();
|
|
242
|
-
for (const [key, value] of Object.entries(headers)) if (value !== void 0)
|
|
243
|
-
|
|
243
|
+
for (const [key, value] of Object.entries(headers)) if (value !== void 0) {
|
|
244
|
+
if (Array.isArray(value)) for (const v of value) fetchHeaders.append(key, v);
|
|
245
|
+
else fetchHeaders.set(key, value);
|
|
246
|
+
}
|
|
244
247
|
const fetchOptions = {
|
|
245
248
|
method,
|
|
246
249
|
headers: fetchHeaders,
|
|
@@ -264,7 +267,8 @@ const createFetchBackendHandler = (options) => async ({ url, method, headers, bo
|
|
|
264
267
|
if (status >= 300 && status < 400) {
|
|
265
268
|
const rawLocation = response.headers.get("location");
|
|
266
269
|
if (rawLocation === null || rawLocation.length === 0) throw new Error(`Redirect ${status} missing Location header`);
|
|
267
|
-
const
|
|
270
|
+
const baseUrlObj = new URL(backendBaseUrl);
|
|
271
|
+
const location = normalizeRedirectLocation(rawLocation, baseUrlObj.pathname);
|
|
268
272
|
logger("debug", `redirect="${location}"`);
|
|
269
273
|
return {
|
|
270
274
|
type: "redirect",
|
|
@@ -305,7 +309,7 @@ const createFetchBackendHandler = (options) => async ({ url, method, headers, bo
|
|
|
305
309
|
* @public
|
|
306
310
|
*/
|
|
307
311
|
const extractModuleFromMeta = (html, metaName = "vite-module") => {
|
|
308
|
-
const match = new RegExp(`<meta name="${metaName}" content="([^"]+)"
|
|
312
|
+
const match = new RegExp(`<meta name="${metaName}" content="([^"]+)">`, "u").exec(html);
|
|
309
313
|
return Array.isArray(match) && typeof match[1] === "string" ? match[1] : null;
|
|
310
314
|
};
|
|
311
315
|
/**
|
|
@@ -319,8 +323,7 @@ const extractModuleFromUrl = (url, pattern) => {
|
|
|
319
323
|
const match = pattern.exec(url);
|
|
320
324
|
return Array.isArray(match) && typeof match[1] === "string" ? match[1] : null;
|
|
321
325
|
};
|
|
322
|
-
|
|
323
|
-
//#region src/index.ts
|
|
326
|
+
// src/index.ts
|
|
324
327
|
/**
|
|
325
328
|
* Backend Proxy Plugin
|
|
326
329
|
* @param {ProxyConfig} config - Plugin configuration
|
|
@@ -350,5 +353,4 @@ const backendProxyPlugin = (config) => {
|
|
|
350
353
|
}
|
|
351
354
|
};
|
|
352
355
|
};
|
|
353
|
-
//#endregion
|
|
354
356
|
export { backendProxyPlugin, createFetchBackendHandler, ensureLeadingSlash, extractModuleFromMeta, extractModuleFromUrl, joinUrlParts, normalizeRedirectLocation, stripLeadingSlash, stripTrailingSlash };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://raw.githubusercontent.com/vitejs/vite-plugin-registry/refs/heads/main/data/schema/extended-package-json.schema.json",
|
|
3
3
|
"name": "vite-plugin-backend-html",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"backend",
|
|
7
7
|
"frontend",
|
|
@@ -31,23 +31,23 @@
|
|
|
31
31
|
"debug": "^4.4.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@commitlint/cli": "^
|
|
35
|
-
"@commitlint/config-conventional": "^
|
|
36
|
-
"@types/debug": "^4.1.
|
|
37
|
-
"@types/node": "^
|
|
38
|
-
"@vitest/coverage-v8": "^
|
|
39
|
-
"conventional-changelog": "^
|
|
40
|
-
"conventional-changelog-angular": "^
|
|
41
|
-
"eslint-plugin-regexp": "3.
|
|
34
|
+
"@commitlint/cli": "^21.2.4",
|
|
35
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
36
|
+
"@types/debug": "^4.1.13",
|
|
37
|
+
"@types/node": "^26.6.6",
|
|
38
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
39
|
+
"conventional-changelog": "^8.1.3",
|
|
40
|
+
"conventional-changelog-angular": "^9.4.0",
|
|
41
|
+
"eslint-plugin-regexp": "3.3.3",
|
|
42
42
|
"husky": "^9.1.7",
|
|
43
|
-
"oxfmt": "^0.
|
|
44
|
-
"oxlint": "^1.
|
|
45
|
-
"oxlint-tsgolint": "^0.
|
|
46
|
-
"release-it": "^
|
|
47
|
-
"tsdown": "^0.
|
|
48
|
-
"typescript": "^
|
|
43
|
+
"oxfmt": "^0.79.0",
|
|
44
|
+
"oxlint": "^1.94.0",
|
|
45
|
+
"oxlint-tsgolint": "^7.0.2007",
|
|
46
|
+
"release-it": "^21.1.2",
|
|
47
|
+
"tsdown": "^0.23.7",
|
|
48
|
+
"typescript": "^7.0.2",
|
|
49
49
|
"vite": "^7.0.0 || ^8.0.0",
|
|
50
|
-
"vitest": "^
|
|
50
|
+
"vitest": "^5.1.1"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"vite": "^7.0.0 || ^8.0.0"
|