@tanstack/devtools-vite 0.5.5 → 0.6.1

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.
@@ -1,456 +1,342 @@
1
- import { devtoolsEventClient } from "@tanstack/devtools-client";
2
- import { ServerEventBus } from "@tanstack/devtools-event-bus/server";
3
- import { normalizePath } from "vite";
4
- import chalk from "chalk";
5
1
  import { handleDevToolsViteRequest, readPackageJson, stripEnhancedLogPrefix } from "./utils.js";
6
2
  import { DEFAULT_EDITOR_CONFIG, handleOpenSource } from "./editor.js";
7
3
  import { removeDevtools } from "./remove-devtools.js";
8
4
  import { addSourceToJsx } from "./inject-source.js";
9
5
  import { enhanceConsoleLog } from "./enhance-logs.js";
10
6
  import { detectDevtoolsFile, injectPluginIntoFile } from "./inject-plugin.js";
11
- import { emitOutdatedDeps, installPackage, addPluginToDevtools } from "./package-manager.js";
7
+ import { addPluginToDevtools, emitOutdatedDeps, installPackage } from "./package-manager.js";
12
8
  import { generateConsolePipeCode } from "./virtual-console.js";
13
- const defineDevtoolsConfig = (config) => config;
14
- const devtools = (args) => {
15
- let port = 5173;
16
- const logging = args?.logging ?? true;
17
- const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true };
18
- const injectSourceConfig = args?.injectSource ?? { enabled: true };
19
- const removeDevtoolsOnBuild = args?.removeDevtoolsOnBuild ?? true;
20
- const serverBusEnabled = args?.eventBusConfig?.enabled ?? true;
21
- const consolePipingConfig = args?.consolePiping ?? { enabled: true };
22
- const consolePipingLevels = consolePipingConfig.levels ?? ["log", "warn", "error", "info", "debug"];
23
- let devtoolsFileId = null;
24
- let devtoolsPort = null;
25
- let devtoolsHost = null;
26
- let devtoolsProtocol = null;
27
- return [
28
- {
29
- enforce: "pre",
30
- name: "@tanstack/devtools:inject-source",
31
- apply(config) {
32
- return config.mode === "development" && injectSourceConfig.enabled;
33
- },
34
- transform: {
35
- filter: {
36
- id: {
37
- exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//]
38
- }
39
- },
40
- handler(code, id) {
41
- return addSourceToJsx(code, id, args?.injectSource?.ignore);
42
- }
43
- }
44
- },
45
- {
46
- name: "@tanstack/devtools:config",
47
- enforce: "pre",
48
- config(_, { command }) {
49
- if (command !== "serve") {
50
- return;
51
- }
52
- }
53
- },
54
- {
55
- enforce: "pre",
56
- name: "@tanstack/devtools:custom-server",
57
- apply(config) {
58
- return config.mode === "development";
59
- },
60
- async configureServer(server) {
61
- if (serverBusEnabled) {
62
- const preferredPort = args?.eventBusConfig?.port ?? 4206;
63
- const isHttps = !!server.config.server.https;
64
- const serverHost = typeof server.config.server.host === "string" ? server.config.server.host : "localhost";
65
- devtoolsProtocol = isHttps ? "https" : "http";
66
- devtoolsHost = serverHost;
67
- const bus = new ServerEventBus({
68
- ...args?.eventBusConfig,
69
- port: preferredPort,
70
- host: serverHost,
71
- // When HTTPS is enabled, piggyback on Vite's server
72
- // so WebSocket/SSE connections share the same TLS certificate
73
- ...isHttps && server.httpServer ? { httpServer: server.httpServer } : {}
74
- });
75
- devtoolsPort = await bus.start();
76
- }
77
- server.middlewares.use((req, _res, next) => {
78
- if (req.socket.localPort && req.socket.localPort !== port) {
79
- port = req.socket.localPort;
80
- }
81
- next();
82
- });
83
- if (server.config.server.port) {
84
- port = server.config.server.port;
85
- }
86
- server.httpServer?.on("listening", () => {
87
- port = server.config.server.port;
88
- });
89
- const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG;
90
- const openInEditor = async (path, lineNum, columnNum) => {
91
- if (!path) {
92
- return;
93
- }
94
- await editor.open(path, lineNum, columnNum);
95
- };
96
- const sseClients = [];
97
- let sseClientId = 0;
98
- const consolePipingEnabled = consolePipingConfig.enabled ?? true;
99
- server.middlewares.use(
100
- (req, res, next) => handleDevToolsViteRequest(req, res, next, {
101
- onOpenSource: (parsedData) => {
102
- const { data, routine } = parsedData;
103
- if (routine === "open-source") {
104
- return handleOpenSource({
105
- data: { type: data.type, data },
106
- openInEditor
107
- });
108
- }
109
- return;
110
- },
111
- ...consolePipingEnabled ? {
112
- onConsolePipe: (entries) => {
113
- for (const entry of entries) {
114
- const prefix = chalk.cyan("[Client]");
115
- const logMethod = console[entry.level];
116
- const cleanedArgs = stripEnhancedLogPrefix(
117
- entry.args,
118
- (loc) => chalk.gray(loc)
119
- );
120
- logMethod(prefix, ...cleanedArgs);
121
- }
122
- },
123
- onConsolePipeSSE: (res2, req2) => {
124
- res2.setHeader("Content-Type", "text/event-stream");
125
- res2.setHeader("Cache-Control", "no-cache");
126
- res2.setHeader("Connection", "keep-alive");
127
- res2.setHeader("Access-Control-Allow-Origin", "*");
128
- res2.flushHeaders();
129
- const clientId = ++sseClientId;
130
- sseClients.push({ res: res2, id: clientId });
131
- req2.on("close", () => {
132
- const index = sseClients.findIndex(
133
- (c) => c.id === clientId
134
- );
135
- if (index !== -1) {
136
- sseClients.splice(index, 1);
137
- }
138
- });
139
- },
140
- onServerConsolePipe: (entries) => {
141
- try {
142
- const data = JSON.stringify({
143
- entries: entries.map((e) => ({
144
- level: e.level,
145
- args: e.args,
146
- source: "server",
147
- timestamp: e.timestamp || Date.now()
148
- }))
149
- });
150
- for (const client of sseClients) {
151
- client.res.write(`data: ${data}
152
-
153
- `);
154
- }
155
- } catch {
156
- }
157
- }
158
- } : {}
159
- })
160
- );
161
- }
162
- },
163
- {
164
- name: "@tanstack/devtools:remove-devtools-on-build",
165
- apply(config, { command }) {
166
- return (command !== "serve" || config.mode === "production") && removeDevtoolsOnBuild;
167
- },
168
- enforce: "pre",
169
- transform(code, id) {
170
- const devtoolPackages = [
171
- "@tanstack/react-devtools",
172
- "@tanstack/preact-devtools",
173
- "@tanstack/solid-devtools",
174
- "@tanstack/vue-devtools",
175
- "@tanstack/devtools"
176
- ];
177
- if (id.includes("node_modules") || id.includes("?raw") || !devtoolPackages.some((pkg) => code.includes(pkg)))
178
- return;
179
- const transform = removeDevtools(code, id);
180
- if (!transform) return;
181
- if (logging) {
182
- console.log(
183
- `
184
- ${chalk.greenBright(`[@tanstack/devtools-vite]`)} Removed devtools code from: ${id.replace(normalizePath(process.cwd()), "")}
185
- `
186
- );
187
- }
188
- return transform;
189
- }
190
- },
191
- {
192
- name: "@tanstack/devtools:event-client-setup",
193
- apply(config, { command }) {
194
- if (process.env.CI || process.env.NODE_ENV !== "development" || command !== "serve")
195
- return false;
196
- return config.mode === "development";
197
- },
198
- async configureServer() {
199
- const packageJson = await readPackageJson();
200
- const outdatedDeps = emitOutdatedDeps().then((deps) => deps);
201
- devtoolsEventClient.on("install-devtools", async (event) => {
202
- const result = await installPackage(event.payload.packageName);
203
- devtoolsEventClient.emit("devtools-installed", {
204
- packageName: event.payload.packageName,
205
- success: result.success,
206
- error: result.error
207
- });
208
- if (result.success) {
209
- const { packageName, pluginName, pluginImport } = event.payload;
210
- console.log(
211
- chalk.blueBright(
212
- `[@tanstack/devtools-vite] Auto-adding ${packageName} to devtools...`
213
- )
214
- );
215
- const injectResult = addPluginToDevtools(
216
- devtoolsFileId,
217
- packageName,
218
- pluginName,
219
- pluginImport
220
- );
221
- if (injectResult.success) {
222
- devtoolsEventClient.emit("plugin-added", {
223
- packageName,
224
- success: true
225
- });
226
- const updatedPackageJson = await readPackageJson();
227
- devtoolsEventClient.emit("package-json-read", {
228
- packageJson: updatedPackageJson
229
- });
230
- }
231
- }
232
- });
233
- devtoolsEventClient.on("add-plugin-to-devtools", (event) => {
234
- const { packageName, pluginName, pluginImport } = event.payload;
235
- console.log(
236
- chalk.blueBright(
237
- `[@tanstack/devtools-vite] Adding ${packageName} to devtools...`
238
- )
239
- );
240
- const result = addPluginToDevtools(
241
- devtoolsFileId,
242
- packageName,
243
- pluginName,
244
- pluginImport
245
- );
246
- devtoolsEventClient.emit("plugin-added", {
247
- packageName,
248
- success: result.success,
249
- error: result.error
250
- });
251
- });
252
- devtoolsEventClient.on("bump-package-version", async (event) => {
253
- const {
254
- packageName,
255
- devtoolsPackage,
256
- pluginName,
257
- minVersion,
258
- pluginImport
259
- } = event.payload;
260
- console.log(
261
- chalk.blueBright(
262
- `[@tanstack/devtools-vite] Bumping ${packageName} to version ${minVersion}...`
263
- )
264
- );
265
- const packageWithVersion = minVersion ? `${packageName}@^${minVersion}` : packageName;
266
- const result = await installPackage(packageWithVersion);
267
- if (!result.success) {
268
- console.log(
269
- chalk.redBright(
270
- `[@tanstack/devtools-vite] Failed to bump ${packageName}: ${result.error}`
271
- )
272
- );
273
- devtoolsEventClient.emit("devtools-installed", {
274
- packageName: devtoolsPackage,
275
- success: false,
276
- error: result.error
277
- });
278
- return;
279
- }
280
- console.log(
281
- chalk.greenBright(
282
- `[@tanstack/devtools-vite] Successfully bumped ${packageName} to ${minVersion}!`
283
- )
284
- );
285
- if (!devtoolsFileId) {
286
- console.log(
287
- chalk.yellowBright(
288
- `[@tanstack/devtools-vite] Devtools file not found. Skipping auto-injection.`
289
- )
290
- );
291
- devtoolsEventClient.emit("devtools-installed", {
292
- packageName: devtoolsPackage,
293
- success: true
294
- });
295
- return;
296
- }
297
- console.log(
298
- chalk.blueBright(
299
- `[@tanstack/devtools-vite] Adding ${devtoolsPackage} to devtools...`
300
- )
301
- );
302
- const injectResult = injectPluginIntoFile(devtoolsFileId, {
303
- packageName: devtoolsPackage,
304
- pluginName,
305
- pluginImport
306
- });
307
- if (injectResult.success) {
308
- console.log(
309
- chalk.greenBright(
310
- `[@tanstack/devtools-vite] Successfully added ${devtoolsPackage} to devtools!`
311
- )
312
- );
313
- devtoolsEventClient.emit("plugin-added", {
314
- packageName: devtoolsPackage,
315
- success: true
316
- });
317
- const updatedPackageJson = await readPackageJson();
318
- devtoolsEventClient.emit("package-json-read", {
319
- packageJson: updatedPackageJson
320
- });
321
- } else {
322
- console.log(
323
- chalk.redBright(
324
- `[@tanstack/devtools-vite] Failed to add ${devtoolsPackage} to devtools: ${injectResult.error}`
325
- )
326
- );
327
- devtoolsEventClient.emit("plugin-added", {
328
- packageName: devtoolsPackage,
329
- success: false,
330
- error: injectResult.error
331
- });
332
- }
333
- });
334
- devtoolsEventClient.on("mounted", async () => {
335
- devtoolsEventClient.emit("outdated-deps-read", {
336
- outdatedDeps: await outdatedDeps
337
- });
338
- devtoolsEventClient.emit("package-json-read", {
339
- packageJson
340
- });
341
- });
342
- },
343
- async handleHotUpdate({ file }) {
344
- if (file.endsWith("package.json")) {
345
- const newPackageJson = await readPackageJson();
346
- devtoolsEventClient.emit("package-json-read", {
347
- packageJson: newPackageJson
348
- });
349
- emitOutdatedDeps();
350
- }
351
- }
352
- },
353
- // Inject console piping code into entry files (both client and server)
354
- {
355
- name: "@tanstack/devtools:console-pipe-transform",
356
- enforce: "pre",
357
- apply(config, { command }) {
358
- return config.mode === "development" && command === "serve" && (consolePipingConfig.enabled ?? true);
359
- },
360
- transform: {
361
- filter: {
362
- id: {
363
- include: /\.(tsx?|jsx?)$/,
364
- exclude: [/node_modules/, /\/dist\//, /\?/]
365
- },
366
- code: {
367
- exclude: /__tsdConsolePipe/
368
- // avoid transforming files that already contain the console pipe code
369
- }
370
- },
371
- handler(code) {
372
- const isRootEntry = /<html[\s>]/i.test(code) || code.includes("StartClient") || code.includes("hydrateRoot") || code.includes("createRoot") || code.includes("solid-js/web") && code.includes("render(");
373
- if (isRootEntry) {
374
- const viteServerUrl = `http://localhost:${port}`;
375
- const inlineCode = generateConsolePipeCode(
376
- consolePipingLevels,
377
- viteServerUrl
378
- );
379
- return `${inlineCode}
380
- ${code}`;
381
- }
382
- return void 0;
383
- }
384
- }
385
- },
386
- {
387
- name: "@tanstack/devtools:better-console-logs",
388
- enforce: "pre",
389
- apply(config) {
390
- return config.mode === "development" && enhancedLogsConfig.enabled;
391
- },
392
- transform: {
393
- filter: {
394
- id: {
395
- exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//]
396
- },
397
- code: {
398
- include: "console."
399
- }
400
- },
401
- handler(code, id) {
402
- return enhanceConsoleLog(code, id, port);
403
- }
404
- }
405
- },
406
- {
407
- name: "@tanstack/devtools:inject-plugin",
408
- apply(config, { command }) {
409
- return config.mode === "development" && command === "serve";
410
- },
411
- transform(code, id) {
412
- if (!devtoolsFileId && detectDevtoolsFile(code)) {
413
- const [filePath] = id.split("?");
414
- if (filePath) {
415
- devtoolsFileId = filePath;
416
- }
417
- }
418
- return void 0;
419
- }
420
- },
421
- {
422
- name: "@tanstack/devtools:connection-injection",
423
- apply(config, { command }) {
424
- return config.mode === "development" && command === "serve";
425
- },
426
- transform(code, id) {
427
- const hasPlaceholder = code.includes("__TANSTACK_DEVTOOLS_PORT__") || code.includes("__TANSTACK_DEVTOOLS_HOST__") || code.includes("__TANSTACK_DEVTOOLS_PROTOCOL__");
428
- if (!hasPlaceholder) return;
429
- if (!id.includes("@tanstack/devtools") && !id.includes("@tanstack/event-bus"))
430
- return;
431
- const portValue = devtoolsPort ?? 4206;
432
- const hostValue = devtoolsHost ?? "localhost";
433
- const protocolValue = devtoolsProtocol ?? "http";
434
- let result = code;
435
- result = result.replace(
436
- /__TANSTACK_DEVTOOLS_PORT__/g,
437
- String(portValue)
438
- );
439
- result = result.replace(
440
- /__TANSTACK_DEVTOOLS_HOST__/g,
441
- JSON.stringify(hostValue)
442
- );
443
- result = result.replace(
444
- /__TANSTACK_DEVTOOLS_PROTOCOL__/g,
445
- JSON.stringify(protocolValue)
446
- );
447
- return result;
448
- }
449
- }
450
- ];
451
- };
452
- export {
453
- defineDevtoolsConfig,
454
- devtools
9
+ import { devtoolsEventClient } from "@tanstack/devtools-client";
10
+ import { ServerEventBus } from "@tanstack/devtools-event-bus/server";
11
+ import { normalizePath } from "vite";
12
+ import chalk from "chalk";
13
+ //#region src/plugin.ts
14
+ var defineDevtoolsConfig = (config) => config;
15
+ var devtools = (args) => {
16
+ let port = 5173;
17
+ const logging = args?.logging ?? true;
18
+ const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true };
19
+ const injectSourceConfig = args?.injectSource ?? { enabled: true };
20
+ const removeDevtoolsOnBuild = args?.removeDevtoolsOnBuild ?? true;
21
+ const serverBusEnabled = args?.eventBusConfig?.enabled ?? true;
22
+ const consolePipingConfig = args?.consolePiping ?? { enabled: true };
23
+ const consolePipingLevels = consolePipingConfig.levels ?? [
24
+ "log",
25
+ "warn",
26
+ "error",
27
+ "info",
28
+ "debug"
29
+ ];
30
+ let devtoolsFileId = null;
31
+ let devtoolsPort = null;
32
+ let devtoolsHost = null;
33
+ let devtoolsProtocol = null;
34
+ return [
35
+ {
36
+ enforce: "pre",
37
+ name: "@tanstack/devtools:inject-source",
38
+ apply(config) {
39
+ return config.mode === "development" && injectSourceConfig.enabled;
40
+ },
41
+ transform: {
42
+ filter: { id: { exclude: [
43
+ /node_modules/,
44
+ /\?raw/,
45
+ /\/dist\//,
46
+ /\/build\//
47
+ ] } },
48
+ handler(code, id) {
49
+ return addSourceToJsx(code, id, args?.injectSource?.ignore);
50
+ }
51
+ }
52
+ },
53
+ {
54
+ name: "@tanstack/devtools:config",
55
+ enforce: "pre",
56
+ config(_, { command }) {
57
+ if (command !== "serve") return;
58
+ }
59
+ },
60
+ {
61
+ enforce: "pre",
62
+ name: "@tanstack/devtools:custom-server",
63
+ apply(config) {
64
+ return config.mode === "development";
65
+ },
66
+ async configureServer(server) {
67
+ if (serverBusEnabled) {
68
+ const preferredPort = args?.eventBusConfig?.port ?? 4206;
69
+ const isHttps = !!server.config.server.https;
70
+ const serverHost = typeof server.config.server.host === "string" ? server.config.server.host : "localhost";
71
+ devtoolsProtocol = isHttps ? "https" : "http";
72
+ devtoolsHost = serverHost;
73
+ devtoolsPort = await new ServerEventBus({
74
+ ...args?.eventBusConfig,
75
+ port: preferredPort,
76
+ host: serverHost,
77
+ ...isHttps && server.httpServer ? { httpServer: server.httpServer } : {}
78
+ }).start();
79
+ }
80
+ server.middlewares.use((req, _res, next) => {
81
+ if (req.socket.localPort && req.socket.localPort !== port) port = req.socket.localPort;
82
+ next();
83
+ });
84
+ if (server.config.server.port) port = server.config.server.port;
85
+ server.httpServer?.on("listening", () => {
86
+ port = server.config.server.port;
87
+ });
88
+ const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG;
89
+ const openInEditor = async (path, lineNum, columnNum) => {
90
+ if (!path) return;
91
+ await editor.open(path, lineNum, columnNum);
92
+ };
93
+ const originalConsole = Object.fromEntries(consolePipingLevels.map((l) => [l, console[l].bind(console)]));
94
+ const sseClients = [];
95
+ let sseClientId = 0;
96
+ const consolePipingEnabled = consolePipingConfig.enabled ?? true;
97
+ server.middlewares.use((req, res, next) => handleDevToolsViteRequest(req, res, next, {
98
+ onOpenSource: (parsedData) => {
99
+ const { data, routine } = parsedData;
100
+ if (routine === "open-source") return handleOpenSource({
101
+ data: {
102
+ type: data.type,
103
+ data
104
+ },
105
+ openInEditor
106
+ });
107
+ },
108
+ ...consolePipingEnabled ? {
109
+ onConsolePipe: (entries) => {
110
+ for (const entry of entries) {
111
+ const prefix = chalk.cyan("[Client]");
112
+ (originalConsole[entry.level] ?? originalConsole.log)(prefix, ...stripEnhancedLogPrefix(entry.args, (loc) => chalk.gray(loc)));
113
+ }
114
+ },
115
+ onConsolePipeSSE: (res, req) => {
116
+ res.setHeader("Content-Type", "text/event-stream");
117
+ res.setHeader("Cache-Control", "no-cache");
118
+ res.setHeader("Connection", "keep-alive");
119
+ res.setHeader("Access-Control-Allow-Origin", "*");
120
+ res.flushHeaders();
121
+ const clientId = ++sseClientId;
122
+ sseClients.push({
123
+ res,
124
+ id: clientId
125
+ });
126
+ req.on("close", () => {
127
+ const index = sseClients.findIndex((c) => c.id === clientId);
128
+ if (index !== -1) sseClients.splice(index, 1);
129
+ });
130
+ },
131
+ onServerConsolePipe: (entries) => {
132
+ try {
133
+ const data = JSON.stringify({ entries: entries.map((e) => ({
134
+ level: e.level,
135
+ args: e.args,
136
+ source: "server",
137
+ timestamp: e.timestamp || Date.now()
138
+ })) });
139
+ for (const client of sseClients) client.res.write(`data: ${data}\n\n`);
140
+ } catch {}
141
+ }
142
+ } : {}
143
+ }));
144
+ }
145
+ },
146
+ {
147
+ name: "@tanstack/devtools:remove-devtools-on-build",
148
+ apply(config, { command }) {
149
+ return (command !== "serve" || config.mode === "production") && removeDevtoolsOnBuild;
150
+ },
151
+ enforce: "pre",
152
+ transform(code, id) {
153
+ if (id.includes("node_modules") || id.includes("?raw") || ![
154
+ "@tanstack/react-devtools",
155
+ "@tanstack/preact-devtools",
156
+ "@tanstack/solid-devtools",
157
+ "@tanstack/vue-devtools",
158
+ "@tanstack/devtools"
159
+ ].some((pkg) => code.includes(pkg))) return;
160
+ const transform = removeDevtools(code, id);
161
+ if (!transform) return;
162
+ if (logging) console.log(`\n${chalk.greenBright(`[@tanstack/devtools-vite]`)} Removed devtools code from: ${id.replace(normalizePath(process.cwd()), "")}\n`);
163
+ return transform;
164
+ }
165
+ },
166
+ {
167
+ name: "@tanstack/devtools:event-client-setup",
168
+ apply(config, { command }) {
169
+ if (process.env.CI || process.env.NODE_ENV !== "development" || command !== "serve") return false;
170
+ return config.mode === "development";
171
+ },
172
+ async configureServer() {
173
+ const packageJson = await readPackageJson();
174
+ const outdatedDeps = emitOutdatedDeps().then((deps) => deps);
175
+ devtoolsEventClient.on("install-devtools", async (event) => {
176
+ const result = await installPackage(event.payload.packageName);
177
+ devtoolsEventClient.emit("devtools-installed", {
178
+ packageName: event.payload.packageName,
179
+ success: result.success,
180
+ error: result.error
181
+ });
182
+ if (result.success) {
183
+ const { packageName, pluginName, pluginImport } = event.payload;
184
+ console.log(chalk.blueBright(`[@tanstack/devtools-vite] Auto-adding ${packageName} to devtools...`));
185
+ if (addPluginToDevtools(devtoolsFileId, packageName, pluginName, pluginImport).success) {
186
+ devtoolsEventClient.emit("plugin-added", {
187
+ packageName,
188
+ success: true
189
+ });
190
+ const updatedPackageJson = await readPackageJson();
191
+ devtoolsEventClient.emit("package-json-read", { packageJson: updatedPackageJson });
192
+ }
193
+ }
194
+ });
195
+ devtoolsEventClient.on("add-plugin-to-devtools", (event) => {
196
+ const { packageName, pluginName, pluginImport } = event.payload;
197
+ console.log(chalk.blueBright(`[@tanstack/devtools-vite] Adding ${packageName} to devtools...`));
198
+ const result = addPluginToDevtools(devtoolsFileId, packageName, pluginName, pluginImport);
199
+ devtoolsEventClient.emit("plugin-added", {
200
+ packageName,
201
+ success: result.success,
202
+ error: result.error
203
+ });
204
+ });
205
+ devtoolsEventClient.on("bump-package-version", async (event) => {
206
+ const { packageName, devtoolsPackage, pluginName, minVersion, pluginImport } = event.payload;
207
+ console.log(chalk.blueBright(`[@tanstack/devtools-vite] Bumping ${packageName} to version ${minVersion}...`));
208
+ const result = await installPackage(minVersion ? `${packageName}@^${minVersion}` : packageName);
209
+ if (!result.success) {
210
+ console.log(chalk.redBright(`[@tanstack/devtools-vite] Failed to bump ${packageName}: ${result.error}`));
211
+ devtoolsEventClient.emit("devtools-installed", {
212
+ packageName: devtoolsPackage,
213
+ success: false,
214
+ error: result.error
215
+ });
216
+ return;
217
+ }
218
+ console.log(chalk.greenBright(`[@tanstack/devtools-vite] Successfully bumped ${packageName} to ${minVersion}!`));
219
+ if (!devtoolsFileId) {
220
+ console.log(chalk.yellowBright(`[@tanstack/devtools-vite] Devtools file not found. Skipping auto-injection.`));
221
+ devtoolsEventClient.emit("devtools-installed", {
222
+ packageName: devtoolsPackage,
223
+ success: true
224
+ });
225
+ return;
226
+ }
227
+ console.log(chalk.blueBright(`[@tanstack/devtools-vite] Adding ${devtoolsPackage} to devtools...`));
228
+ const injectResult = injectPluginIntoFile(devtoolsFileId, {
229
+ packageName: devtoolsPackage,
230
+ pluginName,
231
+ pluginImport
232
+ });
233
+ if (injectResult.success) {
234
+ console.log(chalk.greenBright(`[@tanstack/devtools-vite] Successfully added ${devtoolsPackage} to devtools!`));
235
+ devtoolsEventClient.emit("plugin-added", {
236
+ packageName: devtoolsPackage,
237
+ success: true
238
+ });
239
+ const updatedPackageJson = await readPackageJson();
240
+ devtoolsEventClient.emit("package-json-read", { packageJson: updatedPackageJson });
241
+ } else {
242
+ console.log(chalk.redBright(`[@tanstack/devtools-vite] Failed to add ${devtoolsPackage} to devtools: ${injectResult.error}`));
243
+ devtoolsEventClient.emit("plugin-added", {
244
+ packageName: devtoolsPackage,
245
+ success: false,
246
+ error: injectResult.error
247
+ });
248
+ }
249
+ });
250
+ devtoolsEventClient.on("mounted", async () => {
251
+ devtoolsEventClient.emit("outdated-deps-read", { outdatedDeps: await outdatedDeps });
252
+ devtoolsEventClient.emit("package-json-read", { packageJson });
253
+ });
254
+ },
255
+ async handleHotUpdate({ file }) {
256
+ if (file.endsWith("package.json")) {
257
+ const newPackageJson = await readPackageJson();
258
+ devtoolsEventClient.emit("package-json-read", { packageJson: newPackageJson });
259
+ emitOutdatedDeps();
260
+ }
261
+ }
262
+ },
263
+ {
264
+ name: "@tanstack/devtools:console-pipe-transform",
265
+ enforce: "pre",
266
+ apply(config, { command }) {
267
+ return config.mode === "development" && command === "serve" && (consolePipingConfig.enabled ?? true);
268
+ },
269
+ transform: {
270
+ filter: {
271
+ id: {
272
+ include: /\.(tsx?|jsx?)$/,
273
+ exclude: [
274
+ /node_modules/,
275
+ /\/dist\//,
276
+ /\?/
277
+ ]
278
+ },
279
+ code: { exclude: /__tsdConsolePipe/ }
280
+ },
281
+ handler(code) {
282
+ if (/<html[\s>]/i.test(code) || code.includes("StartClient") || code.includes("hydrateRoot") || code.includes("createRoot") || code.includes("solid-js/web") && code.includes("render(")) return `${generateConsolePipeCode(consolePipingLevels, `http://localhost:${port}`)}\n${code}`;
283
+ }
284
+ }
285
+ },
286
+ {
287
+ name: "@tanstack/devtools:better-console-logs",
288
+ enforce: "pre",
289
+ apply(config) {
290
+ return config.mode === "development" && enhancedLogsConfig.enabled;
291
+ },
292
+ transform: {
293
+ filter: {
294
+ id: { exclude: [
295
+ /node_modules/,
296
+ /\?raw/,
297
+ /\/dist\//,
298
+ /\/build\//
299
+ ] },
300
+ code: { include: "console." }
301
+ },
302
+ handler(code, id) {
303
+ return enhanceConsoleLog(code, id, port);
304
+ }
305
+ }
306
+ },
307
+ {
308
+ name: "@tanstack/devtools:inject-plugin",
309
+ apply(config, { command }) {
310
+ return config.mode === "development" && command === "serve";
311
+ },
312
+ transform(code, id) {
313
+ if (!devtoolsFileId && detectDevtoolsFile(code)) {
314
+ const [filePath] = id.split("?");
315
+ if (filePath) devtoolsFileId = filePath;
316
+ }
317
+ }
318
+ },
319
+ {
320
+ name: "@tanstack/devtools:connection-injection",
321
+ apply(config, { command }) {
322
+ return config.mode === "development" && command === "serve";
323
+ },
324
+ transform(code, id) {
325
+ if (!(code.includes("__TANSTACK_DEVTOOLS_PORT__") || code.includes("__TANSTACK_DEVTOOLS_HOST__") || code.includes("__TANSTACK_DEVTOOLS_PROTOCOL__"))) return;
326
+ if (!id.includes("@tanstack/devtools") && !id.includes("@tanstack/event-bus")) return;
327
+ const portValue = devtoolsPort ?? 4206;
328
+ const hostValue = devtoolsHost ?? "localhost";
329
+ const protocolValue = devtoolsProtocol ?? "http";
330
+ let result = code;
331
+ result = result.replace(/__TANSTACK_DEVTOOLS_PORT__/g, String(portValue));
332
+ result = result.replace(/__TANSTACK_DEVTOOLS_HOST__/g, JSON.stringify(hostValue));
333
+ result = result.replace(/__TANSTACK_DEVTOOLS_PROTOCOL__/g, JSON.stringify(protocolValue));
334
+ return result;
335
+ }
336
+ }
337
+ ];
455
338
  };
456
- //# sourceMappingURL=plugin.js.map
339
+ //#endregion
340
+ export { defineDevtoolsConfig, devtools };
341
+
342
+ //# sourceMappingURL=plugin.js.map