@lovable.dev/vite-tanstack-config 2.26.0 → 2.26.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/README.md +0 -4
- package/dist/index.cjs +1 -4
- package/dist/index.d.cts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -4
- package/package.json +1 -1
- package/dist/devModulePreload-BOSHEX8Y.js +0 -185
- package/dist/devModulePreload-D7H42V_N.cjs +0 -185
package/README.md
CHANGED
|
@@ -13,10 +13,6 @@ Vite config wrapper for Lovable TanStack Start projects.
|
|
|
13
13
|
|
|
14
14
|
Apps can pass through Vite options and adjust the React, TanStack Start, Nitro, HMR gate, and error-logging behavior when they need to.
|
|
15
15
|
|
|
16
|
-
## Server bundle
|
|
17
|
-
|
|
18
|
-
Inside a Lovable build the Nitro server bundle is minified, for both the Cloudflare and the portable fetch-bundle targets (Nitro's Cloudflare preset leaves it unminified). The `LOVABLE_SERVER_MINIFY=0` environment variable restores the preset default. Outside a Lovable build the preset decides.
|
|
19
|
-
|
|
20
16
|
## Build exit watchdog
|
|
21
17
|
|
|
22
18
|
Inside a Lovable build with prerender enabled, the build process is forced to exit if it is still alive 60 seconds after the last build hook has returned, and a `[lovable-build-exit-report]` block naming the open handle types is written to stderr. Adjust it with `defineConfig({ buildExitWatchdog: { graceMs, enabled } })`; the grace is clamped to 5 s–10 min. The `LOVABLE_PRERENDER_EXIT_GRACE_MS` and `LOVABLE_PRERENDER_EXIT_WATCHDOG=off` environment variables override the config.
|
package/dist/index.cjs
CHANGED
|
@@ -105,7 +105,7 @@ function lovableAssetsProxyPlugin() {
|
|
|
105
105
|
//#endregion
|
|
106
106
|
//#region package.json
|
|
107
107
|
var name = "@lovable.dev/vite-tanstack-config";
|
|
108
|
-
var version = "2.26.
|
|
108
|
+
var version = "2.26.2";
|
|
109
109
|
//#endregion
|
|
110
110
|
//#region src/logging.ts
|
|
111
111
|
const PACKAGE_NAME = "@lovable.dev/vite-tanstack-config";
|
|
@@ -1823,7 +1823,6 @@ function defineConfig(configOrOptions = {}) {
|
|
|
1823
1823
|
...userNitroOpts.cloudflare
|
|
1824
1824
|
};
|
|
1825
1825
|
}
|
|
1826
|
-
nitroOpts.minify = process.env.LOVABLE_SERVER_MINIFY !== "0";
|
|
1827
1826
|
} else if (!nitroOpts.preset && !await nitroSupportsDefaultPreset()) logWarning("The installed `nitro` predates the `defaultPreset` option (3.0.260603-beta), so the Cloudflare fallback is ignored and a zero-config build may target Node instead. Upgrade nitro to >=3.0.260603-beta, or set an explicit `nitro: { preset: \"cloudflare-module\" }`.");
|
|
1828
1827
|
const nitroDirs = prerenderEnabled || isSandbox ? prerenderShimDirs(options.vite, nitroOpts) : void 0;
|
|
1829
1828
|
internalPlugins.push(nitro(nitroOpts));
|
|
@@ -1852,8 +1851,6 @@ function defineConfig(configOrOptions = {}) {
|
|
|
1852
1851
|
internalPlugins.push(linkedDependenciesPlugin());
|
|
1853
1852
|
const { devServerBridgePlugin } = await Promise.resolve().then(() => require("./dev-server-bridge.cjs"));
|
|
1854
1853
|
internalPlugins.push(devServerBridgePlugin());
|
|
1855
|
-
const { devModulePreloadPlugin } = await Promise.resolve().then(() => require("./devModulePreload-D7H42V_N.cjs"));
|
|
1856
|
-
internalPlugins.push(devModulePreloadPlugin());
|
|
1857
1854
|
}
|
|
1858
1855
|
if (command === "serve") internalPlugins.push(lovableAssetsProxyPlugin());
|
|
1859
1856
|
let envDefine = {};
|
package/dist/index.d.cts
CHANGED
|
@@ -57,8 +57,7 @@ interface LovableViteTanstackOptions {
|
|
|
57
57
|
* - **`false`**: skip nitro entirely.
|
|
58
58
|
*
|
|
59
59
|
* Inside a Lovable build `LOVABLE_NITRO_PRESET` (Cloudflare by default) pins
|
|
60
|
-
* the preset and output layout
|
|
61
|
-
* `LOVABLE_SERVER_MINIFY=0`; overrides here apply only outside it.
|
|
60
|
+
* the preset and output layout; overrides here apply only outside it.
|
|
62
61
|
*
|
|
63
62
|
* The option surface is narrow on purpose — Nitro v3 is still pre-RC, so
|
|
64
63
|
* we only expose stable build-time knobs to reduce churn if
|
package/dist/index.d.ts
CHANGED
|
@@ -57,8 +57,7 @@ interface LovableViteTanstackOptions {
|
|
|
57
57
|
* - **`false`**: skip nitro entirely.
|
|
58
58
|
*
|
|
59
59
|
* Inside a Lovable build `LOVABLE_NITRO_PRESET` (Cloudflare by default) pins
|
|
60
|
-
* the preset and output layout
|
|
61
|
-
* `LOVABLE_SERVER_MINIFY=0`; overrides here apply only outside it.
|
|
60
|
+
* the preset and output layout; overrides here apply only outside it.
|
|
62
61
|
*
|
|
63
62
|
* The option surface is narrow on purpose — Nitro v3 is still pre-RC, so
|
|
64
63
|
* we only expose stable build-time knobs to reduce churn if
|
package/dist/index.js
CHANGED
|
@@ -81,7 +81,7 @@ function lovableAssetsProxyPlugin() {
|
|
|
81
81
|
//#endregion
|
|
82
82
|
//#region package.json
|
|
83
83
|
var name = "@lovable.dev/vite-tanstack-config";
|
|
84
|
-
var version = "2.26.
|
|
84
|
+
var version = "2.26.2";
|
|
85
85
|
//#endregion
|
|
86
86
|
//#region src/logging.ts
|
|
87
87
|
const PACKAGE_NAME = "@lovable.dev/vite-tanstack-config";
|
|
@@ -1799,7 +1799,6 @@ function defineConfig(configOrOptions = {}) {
|
|
|
1799
1799
|
...userNitroOpts.cloudflare
|
|
1800
1800
|
};
|
|
1801
1801
|
}
|
|
1802
|
-
nitroOpts.minify = process.env.LOVABLE_SERVER_MINIFY !== "0";
|
|
1803
1802
|
} else if (!nitroOpts.preset && !await nitroSupportsDefaultPreset()) logWarning("The installed `nitro` predates the `defaultPreset` option (3.0.260603-beta), so the Cloudflare fallback is ignored and a zero-config build may target Node instead. Upgrade nitro to >=3.0.260603-beta, or set an explicit `nitro: { preset: \"cloudflare-module\" }`.");
|
|
1804
1803
|
const nitroDirs = prerenderEnabled || isSandbox ? prerenderShimDirs(options.vite, nitroOpts) : void 0;
|
|
1805
1804
|
internalPlugins.push(nitro(nitroOpts));
|
|
@@ -1828,8 +1827,6 @@ function defineConfig(configOrOptions = {}) {
|
|
|
1828
1827
|
internalPlugins.push(linkedDependenciesPlugin());
|
|
1829
1828
|
const { devServerBridgePlugin } = await import("./dev-server-bridge.js");
|
|
1830
1829
|
internalPlugins.push(devServerBridgePlugin());
|
|
1831
|
-
const { devModulePreloadPlugin } = await import("./devModulePreload-BOSHEX8Y.js");
|
|
1832
|
-
internalPlugins.push(devModulePreloadPlugin());
|
|
1833
1830
|
}
|
|
1834
1831
|
if (command === "serve") internalPlugins.push(lovableAssetsProxyPlugin());
|
|
1835
1832
|
let envDefine = {};
|
package/package.json
CHANGED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { posix } from "node:path";
|
|
2
|
-
import { isCSSRequest } from "vite";
|
|
3
|
-
//#region src/devModulePreload.ts
|
|
4
|
-
const DEV_CLIENT_ENTRY = "virtual:tanstack-start-dev-client-entry";
|
|
5
|
-
const MAX_PRELOADED_MODULES = 500;
|
|
6
|
-
const HEAD_SCAN_LIMIT = 64 * 1024;
|
|
7
|
-
const HEAD_OPEN_RE = /<head(?:\s[^>]*)?>/i;
|
|
8
|
-
const KNOWN_JS_SRC_RE = /\.(?:[jt]sx?|m[jt]s|vue|marko|svelte|astro|imba|mdx)(?:$|\?)/;
|
|
9
|
-
/** Preloads the dev client's module graph so the browser fetches it in parallel, not one import level per round trip. */
|
|
10
|
-
function devModulePreloadPlugin(options = {}) {
|
|
11
|
-
const entry = options.entry ?? DEV_CLIENT_ENTRY;
|
|
12
|
-
return {
|
|
13
|
-
name: "lovable:dev-module-preload",
|
|
14
|
-
apply: "serve",
|
|
15
|
-
async configureServer(server) {
|
|
16
|
-
if (process.env["LOVABLE_DEV_MODULE_PRELOAD"] !== "1") return;
|
|
17
|
-
if (server.config.experimental?.bundledDev) return;
|
|
18
|
-
const client = server.environments?.client;
|
|
19
|
-
if (!client) return;
|
|
20
|
-
const staticImports = staticImportReader((await import("vite")).parseAst);
|
|
21
|
-
let crawl;
|
|
22
|
-
const recrawl = () => {
|
|
23
|
-
crawl ??= crawlModuleGraph(client, entry, server.config.base, staticImports).finally(() => {
|
|
24
|
-
crawl = void 0;
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
server.httpServer?.once("listening", recrawl);
|
|
28
|
-
server.middlewares.use((req, res, next) => {
|
|
29
|
-
if (req.method === "GET" && (req.headers.accept ?? "").includes("text/html")) {
|
|
30
|
-
recrawl();
|
|
31
|
-
injectAfterHead(res, () => modulePreloadTags(preloadUrls(client, entry, server.config.base, staticImports)));
|
|
32
|
-
}
|
|
33
|
-
next();
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
async function crawlModuleGraph(env, entry, base, staticImports) {
|
|
39
|
-
const queued = /* @__PURE__ */ new Set([entry]);
|
|
40
|
-
let level = [entry];
|
|
41
|
-
while (level.length > 0) {
|
|
42
|
-
const next = [];
|
|
43
|
-
await Promise.all(level.map(async (url) => {
|
|
44
|
-
try {
|
|
45
|
-
await env.transformRequest(url);
|
|
46
|
-
const mod = await env.moduleGraph.getModuleByUrl(url);
|
|
47
|
-
if (!mod) return;
|
|
48
|
-
for (const dep of mod.importedModules) {
|
|
49
|
-
if (dep.id === null || queued.has(dep.url) || queued.size >= MAX_PRELOADED_MODULES) continue;
|
|
50
|
-
if (isLazyDependencyChunk(env, mod, browserImportUrl(dep, base), staticImports)) continue;
|
|
51
|
-
queued.add(dep.url);
|
|
52
|
-
next.push(dep.url);
|
|
53
|
-
}
|
|
54
|
-
} catch {}
|
|
55
|
-
}));
|
|
56
|
-
level = next;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function preloadUrls(env, entry, base, staticImports) {
|
|
60
|
-
const root = env.moduleGraph.urlToModuleMap.get(entry);
|
|
61
|
-
const queue = root ? [root] : [];
|
|
62
|
-
const seen = new Set(queue);
|
|
63
|
-
const urls = [];
|
|
64
|
-
for (const mod of queue) {
|
|
65
|
-
const code = mod.transformResult?.code ?? "";
|
|
66
|
-
for (const dep of mod.importedModules) {
|
|
67
|
-
if (seen.has(dep) || !dep.transformResult) continue;
|
|
68
|
-
const url = browserImportUrl(dep, base);
|
|
69
|
-
if (!code.includes(`"${url}"`) && !code.includes(`'${url}'`)) continue;
|
|
70
|
-
if (isLazyDependencyChunk(env, mod, url, staticImports)) continue;
|
|
71
|
-
if (urls.length === MAX_PRELOADED_MODULES) return urls;
|
|
72
|
-
seen.add(dep);
|
|
73
|
-
queue.push(dep);
|
|
74
|
-
urls.push(url);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return urls;
|
|
78
|
-
}
|
|
79
|
-
/** A chunk a pre-bundled dependency imports dynamically, such as one of a highlighter's hundreds of grammars. */
|
|
80
|
-
function isLazyDependencyChunk(env, importer, url, staticImports) {
|
|
81
|
-
if (!env.depsOptimizer?.isOptimizedDepUrl(importer.url)) return false;
|
|
82
|
-
return !staticImports(importer).has(url);
|
|
83
|
-
}
|
|
84
|
-
/** Reads the specifiers a module's served code imports statically; unparseable code yields none. */
|
|
85
|
-
function staticImportReader(parse) {
|
|
86
|
-
const cache = /* @__PURE__ */ new WeakMap();
|
|
87
|
-
return (mod) => {
|
|
88
|
-
const result = mod.transformResult;
|
|
89
|
-
if (!result) return /* @__PURE__ */ new Set();
|
|
90
|
-
let imports = cache.get(result);
|
|
91
|
-
if (imports) return imports;
|
|
92
|
-
imports = /* @__PURE__ */ new Set();
|
|
93
|
-
try {
|
|
94
|
-
for (const node of parse(result.code).body) if ((node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") && typeof node.source?.value === "string") imports.add(node.source.value);
|
|
95
|
-
} catch {}
|
|
96
|
-
cache.set(result, imports);
|
|
97
|
-
return imports;
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function browserImportUrl(mod, base) {
|
|
101
|
-
let url = mod.url.startsWith("/") ? mod.url : `/@id/${mod.url.replace("\0", "__x00__")}`;
|
|
102
|
-
if (mod.lastHMRTimestamp > 0) url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
|
|
103
|
-
if (!isJSRequest(url) && !isCSSRequest(url)) url = injectQuery(url, "import");
|
|
104
|
-
return base === "/" ? url : `${base.replace(/\/$/, "")}${url}`;
|
|
105
|
-
}
|
|
106
|
-
function injectQuery(url, query) {
|
|
107
|
-
const at = url.search(/[?#]/);
|
|
108
|
-
if (at === -1) return `${url}?${query}`;
|
|
109
|
-
const postfix = url.slice(at);
|
|
110
|
-
return `${url.slice(0, at)}?${query}${postfix[0] === "?" ? `&${postfix.slice(1)}` : postfix}`;
|
|
111
|
-
}
|
|
112
|
-
function isJSRequest(url) {
|
|
113
|
-
const path = url.replace(/[?#].*$/, "");
|
|
114
|
-
return KNOWN_JS_SRC_RE.test(path) || !posix.extname(path) && !path.endsWith("/");
|
|
115
|
-
}
|
|
116
|
-
function modulePreloadTags(urls) {
|
|
117
|
-
const escape = (url) => url.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
118
|
-
return urls.map((url) => `<link rel="modulepreload" href="${escape(url)}">`).join("");
|
|
119
|
-
}
|
|
120
|
-
function headerPairs(headers) {
|
|
121
|
-
if (!Array.isArray(headers)) return Object.entries(headers);
|
|
122
|
-
if (Array.isArray(headers[0])) return headers;
|
|
123
|
-
return headers.flatMap((value, i) => i % 2 === 0 ? [[value, headers[i + 1]]] : []);
|
|
124
|
-
}
|
|
125
|
-
function withoutContentLength(headers) {
|
|
126
|
-
const isLength = (name) => String(name).toLowerCase() === "content-length";
|
|
127
|
-
if (!Array.isArray(headers)) return Object.fromEntries(Object.entries(headers).filter(([name]) => !isLength(name)));
|
|
128
|
-
if (Array.isArray(headers[0])) return headers.filter((pair) => !isLength(pair[0]));
|
|
129
|
-
return headers.filter((_, i) => !isLength(headers[i - i % 2]));
|
|
130
|
-
}
|
|
131
|
-
/** Splices tags after the opening `<head>` of an unencoded HTML response, keeping every original byte. */
|
|
132
|
-
function injectAfterHead(res, tags) {
|
|
133
|
-
const declared = /* @__PURE__ */ new Map();
|
|
134
|
-
const header = (name) => declared.get(name) ?? String(res.getHeader(name) ?? "");
|
|
135
|
-
const isPlainHtml = () => /text\/html/i.test(header("content-type")) && /^(identity)?$/i.test(header("content-encoding").trim());
|
|
136
|
-
let searching = true;
|
|
137
|
-
let scanned = 0;
|
|
138
|
-
const splice = (chunk, encoding) => {
|
|
139
|
-
if (!searching || chunk == null || typeof chunk === "function") return chunk;
|
|
140
|
-
const isBytes = chunk instanceof Uint8Array;
|
|
141
|
-
const isUtf8String = typeof chunk === "string" && (typeof encoding !== "string" || /^utf-?8$/i.test(encoding));
|
|
142
|
-
if (!isPlainHtml() || !(isBytes || isUtf8String)) {
|
|
143
|
-
searching = false;
|
|
144
|
-
return chunk;
|
|
145
|
-
}
|
|
146
|
-
const budget = HEAD_SCAN_LIMIT - scanned;
|
|
147
|
-
const text = isBytes ? Buffer.from(chunk.buffer, chunk.byteOffset, Math.min(chunk.byteLength, budget)).toString("latin1") : chunk.slice(0, budget);
|
|
148
|
-
const match = HEAD_OPEN_RE.exec(text);
|
|
149
|
-
if (!match) {
|
|
150
|
-
scanned += text.length;
|
|
151
|
-
searching = scanned < HEAD_SCAN_LIMIT;
|
|
152
|
-
return chunk;
|
|
153
|
-
}
|
|
154
|
-
searching = false;
|
|
155
|
-
const at = match.index + match[0].length;
|
|
156
|
-
let inserted = "";
|
|
157
|
-
try {
|
|
158
|
-
inserted = tags();
|
|
159
|
-
} catch {
|
|
160
|
-
return chunk;
|
|
161
|
-
}
|
|
162
|
-
if (!isBytes) return `${chunk.slice(0, at)}${inserted}${chunk.slice(at)}`;
|
|
163
|
-
return Buffer.concat([
|
|
164
|
-
chunk.subarray(0, at),
|
|
165
|
-
Buffer.from(inserted, "utf8"),
|
|
166
|
-
chunk.subarray(at)
|
|
167
|
-
]);
|
|
168
|
-
};
|
|
169
|
-
const writeHead = res.writeHead.bind(res);
|
|
170
|
-
const write = res.write.bind(res);
|
|
171
|
-
const end = res.end.bind(res);
|
|
172
|
-
res.writeHead = ((status, ...rest) => {
|
|
173
|
-
const at = rest.findIndex((arg) => arg !== null && typeof arg === "object");
|
|
174
|
-
if (at !== -1) for (const [name, value] of headerPairs(rest[at])) declared.set(String(name).toLowerCase(), String(value));
|
|
175
|
-
if (isPlainHtml()) {
|
|
176
|
-
res.removeHeader("content-length");
|
|
177
|
-
if (at !== -1) rest[at] = withoutContentLength(rest[at]);
|
|
178
|
-
}
|
|
179
|
-
return writeHead(status, ...rest);
|
|
180
|
-
});
|
|
181
|
-
res.write = ((chunk, ...rest) => write(splice(chunk, rest[0]), ...rest));
|
|
182
|
-
res.end = ((chunk, ...rest) => end(splice(chunk, rest[0]), ...rest));
|
|
183
|
-
}
|
|
184
|
-
//#endregion
|
|
185
|
-
export { devModulePreloadPlugin };
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
let node_path = require("node:path");
|
|
2
|
-
let vite = require("vite");
|
|
3
|
-
//#region src/devModulePreload.ts
|
|
4
|
-
const DEV_CLIENT_ENTRY = "virtual:tanstack-start-dev-client-entry";
|
|
5
|
-
const MAX_PRELOADED_MODULES = 500;
|
|
6
|
-
const HEAD_SCAN_LIMIT = 64 * 1024;
|
|
7
|
-
const HEAD_OPEN_RE = /<head(?:\s[^>]*)?>/i;
|
|
8
|
-
const KNOWN_JS_SRC_RE = /\.(?:[jt]sx?|m[jt]s|vue|marko|svelte|astro|imba|mdx)(?:$|\?)/;
|
|
9
|
-
/** Preloads the dev client's module graph so the browser fetches it in parallel, not one import level per round trip. */
|
|
10
|
-
function devModulePreloadPlugin(options = {}) {
|
|
11
|
-
const entry = options.entry ?? DEV_CLIENT_ENTRY;
|
|
12
|
-
return {
|
|
13
|
-
name: "lovable:dev-module-preload",
|
|
14
|
-
apply: "serve",
|
|
15
|
-
async configureServer(server) {
|
|
16
|
-
if (process.env["LOVABLE_DEV_MODULE_PRELOAD"] !== "1") return;
|
|
17
|
-
if (server.config.experimental?.bundledDev) return;
|
|
18
|
-
const client = server.environments?.client;
|
|
19
|
-
if (!client) return;
|
|
20
|
-
const staticImports = staticImportReader((await import("vite")).parseAst);
|
|
21
|
-
let crawl;
|
|
22
|
-
const recrawl = () => {
|
|
23
|
-
crawl ??= crawlModuleGraph(client, entry, server.config.base, staticImports).finally(() => {
|
|
24
|
-
crawl = void 0;
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
server.httpServer?.once("listening", recrawl);
|
|
28
|
-
server.middlewares.use((req, res, next) => {
|
|
29
|
-
if (req.method === "GET" && (req.headers.accept ?? "").includes("text/html")) {
|
|
30
|
-
recrawl();
|
|
31
|
-
injectAfterHead(res, () => modulePreloadTags(preloadUrls(client, entry, server.config.base, staticImports)));
|
|
32
|
-
}
|
|
33
|
-
next();
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
async function crawlModuleGraph(env, entry, base, staticImports) {
|
|
39
|
-
const queued = /* @__PURE__ */ new Set([entry]);
|
|
40
|
-
let level = [entry];
|
|
41
|
-
while (level.length > 0) {
|
|
42
|
-
const next = [];
|
|
43
|
-
await Promise.all(level.map(async (url) => {
|
|
44
|
-
try {
|
|
45
|
-
await env.transformRequest(url);
|
|
46
|
-
const mod = await env.moduleGraph.getModuleByUrl(url);
|
|
47
|
-
if (!mod) return;
|
|
48
|
-
for (const dep of mod.importedModules) {
|
|
49
|
-
if (dep.id === null || queued.has(dep.url) || queued.size >= MAX_PRELOADED_MODULES) continue;
|
|
50
|
-
if (isLazyDependencyChunk(env, mod, browserImportUrl(dep, base), staticImports)) continue;
|
|
51
|
-
queued.add(dep.url);
|
|
52
|
-
next.push(dep.url);
|
|
53
|
-
}
|
|
54
|
-
} catch {}
|
|
55
|
-
}));
|
|
56
|
-
level = next;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function preloadUrls(env, entry, base, staticImports) {
|
|
60
|
-
const root = env.moduleGraph.urlToModuleMap.get(entry);
|
|
61
|
-
const queue = root ? [root] : [];
|
|
62
|
-
const seen = new Set(queue);
|
|
63
|
-
const urls = [];
|
|
64
|
-
for (const mod of queue) {
|
|
65
|
-
const code = mod.transformResult?.code ?? "";
|
|
66
|
-
for (const dep of mod.importedModules) {
|
|
67
|
-
if (seen.has(dep) || !dep.transformResult) continue;
|
|
68
|
-
const url = browserImportUrl(dep, base);
|
|
69
|
-
if (!code.includes(`"${url}"`) && !code.includes(`'${url}'`)) continue;
|
|
70
|
-
if (isLazyDependencyChunk(env, mod, url, staticImports)) continue;
|
|
71
|
-
if (urls.length === MAX_PRELOADED_MODULES) return urls;
|
|
72
|
-
seen.add(dep);
|
|
73
|
-
queue.push(dep);
|
|
74
|
-
urls.push(url);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return urls;
|
|
78
|
-
}
|
|
79
|
-
/** A chunk a pre-bundled dependency imports dynamically, such as one of a highlighter's hundreds of grammars. */
|
|
80
|
-
function isLazyDependencyChunk(env, importer, url, staticImports) {
|
|
81
|
-
if (!env.depsOptimizer?.isOptimizedDepUrl(importer.url)) return false;
|
|
82
|
-
return !staticImports(importer).has(url);
|
|
83
|
-
}
|
|
84
|
-
/** Reads the specifiers a module's served code imports statically; unparseable code yields none. */
|
|
85
|
-
function staticImportReader(parse) {
|
|
86
|
-
const cache = /* @__PURE__ */ new WeakMap();
|
|
87
|
-
return (mod) => {
|
|
88
|
-
const result = mod.transformResult;
|
|
89
|
-
if (!result) return /* @__PURE__ */ new Set();
|
|
90
|
-
let imports = cache.get(result);
|
|
91
|
-
if (imports) return imports;
|
|
92
|
-
imports = /* @__PURE__ */ new Set();
|
|
93
|
-
try {
|
|
94
|
-
for (const node of parse(result.code).body) if ((node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") && typeof node.source?.value === "string") imports.add(node.source.value);
|
|
95
|
-
} catch {}
|
|
96
|
-
cache.set(result, imports);
|
|
97
|
-
return imports;
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function browserImportUrl(mod, base) {
|
|
101
|
-
let url = mod.url.startsWith("/") ? mod.url : `/@id/${mod.url.replace("\0", "__x00__")}`;
|
|
102
|
-
if (mod.lastHMRTimestamp > 0) url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
|
|
103
|
-
if (!isJSRequest(url) && !(0, vite.isCSSRequest)(url)) url = injectQuery(url, "import");
|
|
104
|
-
return base === "/" ? url : `${base.replace(/\/$/, "")}${url}`;
|
|
105
|
-
}
|
|
106
|
-
function injectQuery(url, query) {
|
|
107
|
-
const at = url.search(/[?#]/);
|
|
108
|
-
if (at === -1) return `${url}?${query}`;
|
|
109
|
-
const postfix = url.slice(at);
|
|
110
|
-
return `${url.slice(0, at)}?${query}${postfix[0] === "?" ? `&${postfix.slice(1)}` : postfix}`;
|
|
111
|
-
}
|
|
112
|
-
function isJSRequest(url) {
|
|
113
|
-
const path = url.replace(/[?#].*$/, "");
|
|
114
|
-
return KNOWN_JS_SRC_RE.test(path) || !node_path.posix.extname(path) && !path.endsWith("/");
|
|
115
|
-
}
|
|
116
|
-
function modulePreloadTags(urls) {
|
|
117
|
-
const escape = (url) => url.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
118
|
-
return urls.map((url) => `<link rel="modulepreload" href="${escape(url)}">`).join("");
|
|
119
|
-
}
|
|
120
|
-
function headerPairs(headers) {
|
|
121
|
-
if (!Array.isArray(headers)) return Object.entries(headers);
|
|
122
|
-
if (Array.isArray(headers[0])) return headers;
|
|
123
|
-
return headers.flatMap((value, i) => i % 2 === 0 ? [[value, headers[i + 1]]] : []);
|
|
124
|
-
}
|
|
125
|
-
function withoutContentLength(headers) {
|
|
126
|
-
const isLength = (name) => String(name).toLowerCase() === "content-length";
|
|
127
|
-
if (!Array.isArray(headers)) return Object.fromEntries(Object.entries(headers).filter(([name]) => !isLength(name)));
|
|
128
|
-
if (Array.isArray(headers[0])) return headers.filter((pair) => !isLength(pair[0]));
|
|
129
|
-
return headers.filter((_, i) => !isLength(headers[i - i % 2]));
|
|
130
|
-
}
|
|
131
|
-
/** Splices tags after the opening `<head>` of an unencoded HTML response, keeping every original byte. */
|
|
132
|
-
function injectAfterHead(res, tags) {
|
|
133
|
-
const declared = /* @__PURE__ */ new Map();
|
|
134
|
-
const header = (name) => declared.get(name) ?? String(res.getHeader(name) ?? "");
|
|
135
|
-
const isPlainHtml = () => /text\/html/i.test(header("content-type")) && /^(identity)?$/i.test(header("content-encoding").trim());
|
|
136
|
-
let searching = true;
|
|
137
|
-
let scanned = 0;
|
|
138
|
-
const splice = (chunk, encoding) => {
|
|
139
|
-
if (!searching || chunk == null || typeof chunk === "function") return chunk;
|
|
140
|
-
const isBytes = chunk instanceof Uint8Array;
|
|
141
|
-
const isUtf8String = typeof chunk === "string" && (typeof encoding !== "string" || /^utf-?8$/i.test(encoding));
|
|
142
|
-
if (!isPlainHtml() || !(isBytes || isUtf8String)) {
|
|
143
|
-
searching = false;
|
|
144
|
-
return chunk;
|
|
145
|
-
}
|
|
146
|
-
const budget = HEAD_SCAN_LIMIT - scanned;
|
|
147
|
-
const text = isBytes ? Buffer.from(chunk.buffer, chunk.byteOffset, Math.min(chunk.byteLength, budget)).toString("latin1") : chunk.slice(0, budget);
|
|
148
|
-
const match = HEAD_OPEN_RE.exec(text);
|
|
149
|
-
if (!match) {
|
|
150
|
-
scanned += text.length;
|
|
151
|
-
searching = scanned < HEAD_SCAN_LIMIT;
|
|
152
|
-
return chunk;
|
|
153
|
-
}
|
|
154
|
-
searching = false;
|
|
155
|
-
const at = match.index + match[0].length;
|
|
156
|
-
let inserted = "";
|
|
157
|
-
try {
|
|
158
|
-
inserted = tags();
|
|
159
|
-
} catch {
|
|
160
|
-
return chunk;
|
|
161
|
-
}
|
|
162
|
-
if (!isBytes) return `${chunk.slice(0, at)}${inserted}${chunk.slice(at)}`;
|
|
163
|
-
return Buffer.concat([
|
|
164
|
-
chunk.subarray(0, at),
|
|
165
|
-
Buffer.from(inserted, "utf8"),
|
|
166
|
-
chunk.subarray(at)
|
|
167
|
-
]);
|
|
168
|
-
};
|
|
169
|
-
const writeHead = res.writeHead.bind(res);
|
|
170
|
-
const write = res.write.bind(res);
|
|
171
|
-
const end = res.end.bind(res);
|
|
172
|
-
res.writeHead = ((status, ...rest) => {
|
|
173
|
-
const at = rest.findIndex((arg) => arg !== null && typeof arg === "object");
|
|
174
|
-
if (at !== -1) for (const [name, value] of headerPairs(rest[at])) declared.set(String(name).toLowerCase(), String(value));
|
|
175
|
-
if (isPlainHtml()) {
|
|
176
|
-
res.removeHeader("content-length");
|
|
177
|
-
if (at !== -1) rest[at] = withoutContentLength(rest[at]);
|
|
178
|
-
}
|
|
179
|
-
return writeHead(status, ...rest);
|
|
180
|
-
});
|
|
181
|
-
res.write = ((chunk, ...rest) => write(splice(chunk, rest[0]), ...rest));
|
|
182
|
-
res.end = ((chunk, ...rest) => end(splice(chunk, rest[0]), ...rest));
|
|
183
|
-
}
|
|
184
|
-
//#endregion
|
|
185
|
-
exports.devModulePreloadPlugin = devModulePreloadPlugin;
|