@lovable.dev/vite-tanstack-config 2.25.1 → 2.25.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.
@@ -0,0 +1,185 @@
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, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
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 };
@@ -0,0 +1,185 @@
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, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
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;
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.25.1";
108
+ var version = "2.25.3";
109
109
  //#endregion
110
110
  //#region src/logging.ts
111
111
  const PACKAGE_NAME = "@lovable.dev/vite-tanstack-config";
@@ -1851,6 +1851,8 @@ function defineConfig(configOrOptions = {}) {
1851
1851
  internalPlugins.push(linkedDependenciesPlugin());
1852
1852
  const { devServerBridgePlugin } = await Promise.resolve().then(() => require("./dev-server-bridge.cjs"));
1853
1853
  internalPlugins.push(devServerBridgePlugin());
1854
+ const { devModulePreloadPlugin } = await Promise.resolve().then(() => require("./devModulePreload-D7H42V_N.cjs"));
1855
+ internalPlugins.push(devModulePreloadPlugin());
1854
1856
  }
1855
1857
  if (command === "serve") internalPlugins.push(lovableAssetsProxyPlugin());
1856
1858
  let envDefine = {};
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.25.1";
84
+ var version = "2.25.3";
85
85
  //#endregion
86
86
  //#region src/logging.ts
87
87
  const PACKAGE_NAME = "@lovable.dev/vite-tanstack-config";
@@ -1827,6 +1827,8 @@ function defineConfig(configOrOptions = {}) {
1827
1827
  internalPlugins.push(linkedDependenciesPlugin());
1828
1828
  const { devServerBridgePlugin } = await import("./dev-server-bridge.js");
1829
1829
  internalPlugins.push(devServerBridgePlugin());
1830
+ const { devModulePreloadPlugin } = await import("./devModulePreload-BOSHEX8Y.js");
1831
+ internalPlugins.push(devModulePreloadPlugin());
1830
1832
  }
1831
1833
  if (command === "serve") internalPlugins.push(lovableAssetsProxyPlugin());
1832
1834
  let envDefine = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovable.dev/vite-tanstack-config",
3
- "version": "2.25.1",
3
+ "version": "2.25.3",
4
4
  "description": "Vite config wrapper for Lovable TanStack Start projects",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -106,7 +106,7 @@
106
106
  "scripts": {
107
107
  "build": "tsdown",
108
108
  "check:package": "tmp=$(mktemp -d) && pnpm pack --out \"$tmp/pkg.tgz\" && tar -xzf \"$tmp/pkg.tgz\" -C \"$tmp\" && publint \"$tmp/package\" --strict && attw \"$tmp/pkg.tgz\"",
109
- "typecheck": "tsgo --noEmit",
109
+ "typecheck": "tsc --noEmit --rootDir ../..",
110
110
  "dev": "tsdown --watch",
111
111
  "test": "vitest run",
112
112
  "test:unit": "vitest run --project unit",