@gtkx/cli 0.18.1 → 0.18.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.
Files changed (67) hide show
  1. package/dist/builder.d.ts +1 -0
  2. package/dist/builder.d.ts.map +1 -0
  3. package/dist/builder.js +1 -0
  4. package/dist/builder.js.map +1 -0
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.d.ts.map +1 -0
  7. package/dist/cli.js +1 -0
  8. package/dist/cli.js.map +1 -0
  9. package/dist/create.d.ts +1 -0
  10. package/dist/create.d.ts.map +1 -0
  11. package/dist/create.js +1 -0
  12. package/dist/create.js.map +1 -0
  13. package/dist/dev-server.d.ts +1 -0
  14. package/dist/dev-server.d.ts.map +1 -0
  15. package/dist/dev-server.js +1 -0
  16. package/dist/dev-server.js.map +1 -0
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/mcp-client.d.ts +1 -0
  22. package/dist/mcp-client.d.ts.map +1 -0
  23. package/dist/mcp-client.js +1 -0
  24. package/dist/mcp-client.js.map +1 -0
  25. package/dist/refresh-runtime.d.ts +1 -0
  26. package/dist/refresh-runtime.d.ts.map +1 -0
  27. package/dist/refresh-runtime.js +1 -0
  28. package/dist/refresh-runtime.js.map +1 -0
  29. package/dist/templates.d.ts +1 -0
  30. package/dist/templates.d.ts.map +1 -0
  31. package/dist/templates.js +1 -0
  32. package/dist/templates.js.map +1 -0
  33. package/dist/vite-plugin-gtkx-assets.d.ts +1 -0
  34. package/dist/vite-plugin-gtkx-assets.d.ts.map +1 -0
  35. package/dist/vite-plugin-gtkx-assets.js +1 -0
  36. package/dist/vite-plugin-gtkx-assets.js.map +1 -0
  37. package/dist/vite-plugin-gtkx-built-url.d.ts +1 -0
  38. package/dist/vite-plugin-gtkx-built-url.d.ts.map +1 -0
  39. package/dist/vite-plugin-gtkx-built-url.js +1 -0
  40. package/dist/vite-plugin-gtkx-built-url.js.map +1 -0
  41. package/dist/vite-plugin-gtkx-native.d.ts +1 -0
  42. package/dist/vite-plugin-gtkx-native.d.ts.map +1 -0
  43. package/dist/vite-plugin-gtkx-native.js +1 -0
  44. package/dist/vite-plugin-gtkx-native.js.map +1 -0
  45. package/dist/vite-plugin-gtkx-refresh.d.ts +1 -0
  46. package/dist/vite-plugin-gtkx-refresh.d.ts.map +1 -0
  47. package/dist/vite-plugin-gtkx-refresh.js +1 -0
  48. package/dist/vite-plugin-gtkx-refresh.js.map +1 -0
  49. package/dist/vite-plugin-swc-ssr-refresh.d.ts +1 -0
  50. package/dist/vite-plugin-swc-ssr-refresh.d.ts.map +1 -0
  51. package/dist/vite-plugin-swc-ssr-refresh.js +1 -0
  52. package/dist/vite-plugin-swc-ssr-refresh.js.map +1 -0
  53. package/package.json +8 -6
  54. package/src/builder.ts +94 -0
  55. package/src/cli.tsx +154 -0
  56. package/src/create.ts +310 -0
  57. package/src/dev-server.tsx +162 -0
  58. package/src/global.d.ts +6 -0
  59. package/src/index.ts +3 -0
  60. package/src/mcp-client.ts +518 -0
  61. package/src/refresh-runtime.ts +89 -0
  62. package/src/templates.ts +26 -0
  63. package/src/vite-plugin-gtkx-assets.ts +32 -0
  64. package/src/vite-plugin-gtkx-built-url.ts +48 -0
  65. package/src/vite-plugin-gtkx-native.ts +64 -0
  66. package/src/vite-plugin-gtkx-refresh.ts +54 -0
  67. package/src/vite-plugin-swc-ssr-refresh.ts +61 -0
@@ -0,0 +1,518 @@
1
+ import * as net from "node:net";
2
+ import { getNativeInterface } from "@gtkx/ffi";
3
+ import * as Gio from "@gtkx/ffi/gio";
4
+ import { Value } from "@gtkx/ffi/gobject";
5
+ import * as Gtk from "@gtkx/ffi/gtk";
6
+ import {
7
+ DEFAULT_SOCKET_PATH,
8
+ type IpcMethod,
9
+ type IpcRequest,
10
+ IpcRequestSchema,
11
+ type IpcResponse,
12
+ IpcResponseSchema,
13
+ McpError,
14
+ McpErrorCode,
15
+ methodNotFoundError,
16
+ type SerializedWidget,
17
+ widgetNotFoundError,
18
+ } from "@gtkx/mcp";
19
+
20
+ const widgetIdMap = new WeakMap<Gtk.Widget, string>();
21
+ let nextWidgetId = 0;
22
+
23
+ const getWidgetId = (widget: Gtk.Widget): string => {
24
+ let id = widgetIdMap.get(widget);
25
+ if (!id) {
26
+ id = String(nextWidgetId++);
27
+ widgetIdMap.set(widget, id);
28
+ }
29
+ return id;
30
+ };
31
+
32
+ type TestingModule = typeof import("@gtkx/testing");
33
+
34
+ let testingModule: TestingModule | null = null;
35
+ let testingLoadError: Error | null = null;
36
+
37
+ const loadTestingModule = async (): Promise<TestingModule> => {
38
+ if (testingModule) return testingModule;
39
+ if (testingLoadError) throw testingLoadError;
40
+
41
+ try {
42
+ testingModule = await import("@gtkx/testing");
43
+ return testingModule;
44
+ } catch (cause) {
45
+ testingLoadError = new Error(
46
+ "@gtkx/testing is not installed. Install it to enable MCP widget interactions: pnpm add -D @gtkx/testing",
47
+ { cause },
48
+ );
49
+ throw testingLoadError;
50
+ }
51
+ };
52
+
53
+ type McpClientOptions = {
54
+ socketPath?: string;
55
+ appId: string;
56
+ };
57
+
58
+ type PendingRequest = {
59
+ resolve: (result: unknown) => void;
60
+ reject: (error: Error) => void;
61
+ timeout: NodeJS.Timeout;
62
+ };
63
+
64
+ const RECONNECT_DELAY_MS = 2000;
65
+ const REQUEST_TIMEOUT_MS = 30000;
66
+
67
+ const formatRole = (role: Gtk.AccessibleRole | undefined): string => {
68
+ if (role === undefined) return "UNKNOWN";
69
+ return Gtk.AccessibleRole[role] ?? String(role);
70
+ };
71
+
72
+ const getWidgetText = (widget: Gtk.Widget): string | null => {
73
+ if ("getLabel" in widget && typeof widget.getLabel === "function") {
74
+ return widget.getLabel() ?? null;
75
+ }
76
+
77
+ if ("getText" in widget && typeof widget.getText === "function") {
78
+ return widget.getText() ?? null;
79
+ }
80
+
81
+ if ("getTitle" in widget && typeof widget.getTitle === "function") {
82
+ return widget.getTitle() ?? null;
83
+ }
84
+
85
+ return getNativeInterface(widget, Gtk.Editable)?.getText() ?? null;
86
+ };
87
+
88
+ const serializeWidget = (widget: Gtk.Widget): SerializedWidget => {
89
+ const children: SerializedWidget[] = [];
90
+ let child = widget.getFirstChild();
91
+ while (child) {
92
+ children.push(serializeWidget(child));
93
+ child = child.getNextSibling();
94
+ }
95
+
96
+ const text = getWidgetText(widget);
97
+
98
+ return {
99
+ id: getWidgetId(widget),
100
+ type: widget.constructor.name,
101
+ role: formatRole(widget.getAccessibleRole()),
102
+ name: widget.getName() || null,
103
+ label: text,
104
+ text,
105
+ sensitive: widget.getSensitive(),
106
+ visible: widget.getVisible(),
107
+ cssClasses: widget.getCssClasses() ?? [],
108
+ children,
109
+ };
110
+ };
111
+
112
+ const widgetRegistry = new Map<string, Gtk.Widget>();
113
+
114
+ const registerWidgets = (widget: Gtk.Widget): void => {
115
+ const idStr = getWidgetId(widget);
116
+ widgetRegistry.set(idStr, widget);
117
+ let child = widget.getFirstChild();
118
+ while (child) {
119
+ registerWidgets(child);
120
+ child = child.getNextSibling();
121
+ }
122
+ };
123
+
124
+ const getWidgetById = (id: string): Gtk.Widget | undefined => {
125
+ return widgetRegistry.get(id);
126
+ };
127
+
128
+ const refreshWidgetRegistry = (): void => {
129
+ widgetRegistry.clear();
130
+ const windows = Gtk.Window.listToplevels();
131
+ for (const window of windows) {
132
+ registerWidgets(window);
133
+ }
134
+ };
135
+
136
+ class McpClient {
137
+ private socket: net.Socket | null = null;
138
+ private buffer = "";
139
+ private socketPath: string;
140
+ private appId: string;
141
+ private reconnectTimer: NodeJS.Timeout | null = null;
142
+ private hasConnected = false;
143
+ private isStopping = false;
144
+ private pendingRequests = new Map<string, PendingRequest>();
145
+
146
+ constructor(options: McpClientOptions) {
147
+ this.socketPath = options.socketPath ?? DEFAULT_SOCKET_PATH;
148
+ this.appId = options.appId;
149
+ }
150
+
151
+ async connect(): Promise<void> {
152
+ return new Promise((resolve, reject) => {
153
+ this.attemptConnect(resolve, reject);
154
+ });
155
+ }
156
+
157
+ private attemptConnect(onSuccess?: () => void, onError?: (error: Error) => void): void {
158
+ let settled = false;
159
+
160
+ const settle = <T extends unknown[]>(callback: ((...args: T) => void) | undefined, ...args: T) => {
161
+ if (settled) return;
162
+ settled = true;
163
+ callback?.(...args);
164
+ };
165
+
166
+ this.socket = net.createConnection(this.socketPath, () => {
167
+ console.log(`[gtkx] Connected to MCP server at ${this.socketPath}`);
168
+ this.hasConnected = true;
169
+ this.register()
170
+ .then(() => {
171
+ console.log("[gtkx] Registered with MCP server");
172
+ settle(onSuccess);
173
+ })
174
+ .catch((error) => {
175
+ console.error("[gtkx] Failed to register with MCP server:", error.message);
176
+ settle(onError, error instanceof Error ? error : new Error(String(error)));
177
+ });
178
+ });
179
+
180
+ this.socket.on("data", (data: Buffer) => this.handleData(data));
181
+
182
+ this.socket.on("close", () => {
183
+ if (this.hasConnected) {
184
+ console.log("[gtkx] Disconnected from MCP server");
185
+ this.hasConnected = false;
186
+ }
187
+ this.socket = null;
188
+ this.rejectPendingRequests(new Error("Connection closed"));
189
+ this.scheduleReconnect();
190
+ });
191
+
192
+ this.socket.on("error", (error) => {
193
+ const code = (error as NodeJS.ErrnoException).code;
194
+ const isDisconnectError =
195
+ code === "ENOENT" || code === "ECONNREFUSED" || code === "EPIPE" || code === "ECONNRESET";
196
+ if (isDisconnectError) {
197
+ this.scheduleReconnect();
198
+ } else {
199
+ console.error("[gtkx] Socket error:", error.message);
200
+ }
201
+ settle(onError, error);
202
+ });
203
+ }
204
+
205
+ private scheduleReconnect(): void {
206
+ if (this.reconnectTimer || this.isStopping) return;
207
+ this.reconnectTimer = setTimeout(() => {
208
+ this.reconnectTimer = null;
209
+ this.attemptConnect();
210
+ }, RECONNECT_DELAY_MS);
211
+ }
212
+
213
+ disconnect(): void {
214
+ this.isStopping = true;
215
+ if (this.reconnectTimer) {
216
+ clearTimeout(this.reconnectTimer);
217
+ this.reconnectTimer = null;
218
+ }
219
+ this.rejectPendingRequests(new Error("Client disconnected"));
220
+ if (this.socket) {
221
+ this.send({ id: crypto.randomUUID(), method: "app.unregister" });
222
+ this.socket.destroy();
223
+ this.socket = null;
224
+ }
225
+ this.hasConnected = false;
226
+ }
227
+
228
+ private rejectPendingRequests(error: Error): void {
229
+ for (const pending of this.pendingRequests.values()) {
230
+ clearTimeout(pending.timeout);
231
+ pending.reject(error);
232
+ }
233
+ this.pendingRequests.clear();
234
+ }
235
+
236
+ private async register(): Promise<void> {
237
+ await this.sendRequest("app.register", {
238
+ appId: this.appId,
239
+ pid: process.pid,
240
+ });
241
+ }
242
+
243
+ private send(message: IpcRequest | IpcResponse): void {
244
+ if (!this.socket || !this.socket.writable) return;
245
+ this.socket.write(`${JSON.stringify(message)}\n`);
246
+ }
247
+
248
+ private sendRequest(method: IpcMethod, params?: unknown): Promise<unknown> {
249
+ return new Promise((resolve, reject) => {
250
+ if (!this.socket || !this.socket.writable) {
251
+ reject(new Error("Socket not connected"));
252
+ return;
253
+ }
254
+
255
+ const id = crypto.randomUUID();
256
+ const timeout = setTimeout(() => {
257
+ this.pendingRequests.delete(id);
258
+ reject(new Error(`Request timed out: ${method}`));
259
+ }, REQUEST_TIMEOUT_MS);
260
+
261
+ this.pendingRequests.set(id, { resolve, reject, timeout });
262
+ this.send({ id, method, params });
263
+ });
264
+ }
265
+
266
+ private handleData(data: Buffer): void {
267
+ this.buffer += data.toString();
268
+
269
+ let newlineIndex = this.buffer.indexOf("\n");
270
+ while (newlineIndex !== -1) {
271
+ const line = this.buffer.slice(0, newlineIndex);
272
+ this.buffer = this.buffer.slice(newlineIndex + 1);
273
+
274
+ if (line.trim()) {
275
+ this.processMessage(line);
276
+ }
277
+ newlineIndex = this.buffer.indexOf("\n");
278
+ }
279
+ }
280
+
281
+ private processMessage(line: string): void {
282
+ let parsed: unknown;
283
+ try {
284
+ parsed = JSON.parse(line);
285
+ } catch {
286
+ console.warn("[gtkx] Received invalid JSON from MCP server");
287
+ return;
288
+ }
289
+
290
+ const responseResult = IpcResponseSchema.safeParse(parsed);
291
+ if (responseResult.success) {
292
+ const response = responseResult.data;
293
+ const pending = this.pendingRequests.get(response.id);
294
+ if (pending) {
295
+ clearTimeout(pending.timeout);
296
+ this.pendingRequests.delete(response.id);
297
+ if (response.error) {
298
+ pending.reject(new Error(response.error.message));
299
+ } else {
300
+ pending.resolve(response.result);
301
+ }
302
+ return;
303
+ }
304
+ }
305
+
306
+ const requestResult = IpcRequestSchema.safeParse(parsed);
307
+ if (!requestResult.success) {
308
+ return;
309
+ }
310
+
311
+ this.handleRequest(requestResult.data).catch((error) => {
312
+ console.error("[gtkx] Error handling request:", error);
313
+ });
314
+ }
315
+
316
+ private async handleRequest(request: IpcRequest): Promise<void> {
317
+ const { id, method, params } = request;
318
+
319
+ try {
320
+ const result = await this.executeMethod(method as IpcMethod, params);
321
+ this.send({ id, result });
322
+ } catch (error) {
323
+ if (error instanceof McpError) {
324
+ this.send({ id, error: error.toIpcError() });
325
+ } else {
326
+ const message = error instanceof Error ? error.message : String(error);
327
+ this.send({
328
+ id,
329
+ error: {
330
+ code: McpErrorCode.INTERNAL_ERROR,
331
+ message,
332
+ },
333
+ });
334
+ }
335
+ }
336
+ }
337
+
338
+ private async executeMethod(method: IpcMethod, params: unknown): Promise<unknown> {
339
+ const app = Gio.Application.getDefault() as Gtk.Application | null;
340
+
341
+ if (!app) {
342
+ throw new Error("Application not initialized");
343
+ }
344
+
345
+ refreshWidgetRegistry();
346
+
347
+ switch (method) {
348
+ case "app.getWindows": {
349
+ const windows = Gtk.Window.listToplevels();
350
+ return {
351
+ windows: windows.map((w) => ({
352
+ id: getWidgetId(w),
353
+ title: (w as Gtk.Window).getTitle?.() ?? null,
354
+ })),
355
+ };
356
+ }
357
+
358
+ case "widget.getTree": {
359
+ const testing = await loadTestingModule();
360
+ return { tree: testing.prettyWidget(app, { includeIds: true, highlight: false }) };
361
+ }
362
+
363
+ case "widget.query": {
364
+ const testing = await loadTestingModule();
365
+ const p = params as { queryType: string; value: string | number; options?: Record<string, unknown> };
366
+ let widgets: Gtk.Widget[] = [];
367
+
368
+ switch (p.queryType) {
369
+ case "role": {
370
+ const roleValue =
371
+ typeof p.value === "string"
372
+ ? (Gtk.AccessibleRole[p.value as keyof typeof Gtk.AccessibleRole] as Gtk.AccessibleRole)
373
+ : (p.value as Gtk.AccessibleRole);
374
+ widgets = await testing.findAllByRole(app, roleValue, p.options);
375
+ break;
376
+ }
377
+ case "text":
378
+ widgets = await testing.findAllByText(app, String(p.value), p.options);
379
+ break;
380
+ case "testId":
381
+ widgets = await testing.findAllByTestId(app, String(p.value), p.options);
382
+ break;
383
+ case "labelText":
384
+ widgets = await testing.findAllByLabelText(app, String(p.value), p.options);
385
+ break;
386
+ default:
387
+ throw new Error(`Unknown query type: ${p.queryType}`);
388
+ }
389
+
390
+ return { widgets: widgets.map((w) => serializeWidget(w)) };
391
+ }
392
+
393
+ case "widget.getProps": {
394
+ const p = params as { widgetId: string };
395
+ const widget = getWidgetById(p.widgetId);
396
+ if (!widget) {
397
+ throw widgetNotFoundError(p.widgetId);
398
+ }
399
+ return serializeWidget(widget);
400
+ }
401
+
402
+ case "widget.click": {
403
+ const testing = await loadTestingModule();
404
+ const p = params as { widgetId: string };
405
+ const widget = getWidgetById(p.widgetId);
406
+ if (!widget) {
407
+ throw widgetNotFoundError(p.widgetId);
408
+ }
409
+ await testing.userEvent.click(widget);
410
+ return { success: true };
411
+ }
412
+
413
+ case "widget.type": {
414
+ const testing = await loadTestingModule();
415
+ const p = params as { widgetId: string; text: string; clear?: boolean };
416
+ const widget = getWidgetById(p.widgetId);
417
+ if (!widget) {
418
+ throw widgetNotFoundError(p.widgetId);
419
+ }
420
+ if (p.clear) {
421
+ await testing.userEvent.clear(widget);
422
+ }
423
+ await testing.userEvent.type(widget, p.text);
424
+ return { success: true };
425
+ }
426
+
427
+ case "widget.fireEvent": {
428
+ const testing = await loadTestingModule();
429
+ const p = params as {
430
+ widgetId: string;
431
+ signal: string;
432
+ args?: (unknown | { type: string; value: unknown })[];
433
+ };
434
+ const widget = getWidgetById(p.widgetId);
435
+ if (!widget) {
436
+ throw widgetNotFoundError(p.widgetId);
437
+ }
438
+ const signalArgs = (p.args ?? []).map((arg) => {
439
+ const isTypedArg = typeof arg === "object" && arg !== null && "type" in arg && "value" in arg;
440
+ const argType = isTypedArg ? (arg as { type: string }).type : typeof arg;
441
+ const argValue = isTypedArg ? (arg as { value: unknown }).value : arg;
442
+
443
+ switch (argType) {
444
+ case "boolean":
445
+ return Value.newFromBoolean(argValue as boolean);
446
+ case "int":
447
+ return Value.newFromInt(argValue as number);
448
+ case "uint":
449
+ return Value.newFromUint(argValue as number);
450
+ case "int64":
451
+ return Value.newFromInt64(argValue as number);
452
+ case "uint64":
453
+ return Value.newFromUint64(argValue as number);
454
+ case "float":
455
+ return Value.newFromFloat(argValue as number);
456
+ case "double":
457
+ case "number":
458
+ return Value.newFromDouble(argValue as number);
459
+ case "string":
460
+ return Value.newFromString(argValue as string | null);
461
+ default:
462
+ throw new McpError(McpErrorCode.INVALID_REQUEST, `Unknown argument type: ${argType}`);
463
+ }
464
+ });
465
+ await testing.fireEvent(widget, p.signal, ...signalArgs);
466
+ return { success: true };
467
+ }
468
+
469
+ case "widget.screenshot": {
470
+ const testing = await loadTestingModule();
471
+ const p = params as { windowId?: string };
472
+
473
+ let targetWindow: Gtk.Window;
474
+
475
+ if (p.windowId) {
476
+ const widget = getWidgetById(p.windowId);
477
+ if (!widget) {
478
+ throw widgetNotFoundError(p.windowId);
479
+ }
480
+ targetWindow = widget as Gtk.Window;
481
+ } else {
482
+ const windows = app.getWindows();
483
+ if (windows.length === 0) {
484
+ throw new Error("No windows available for screenshot");
485
+ }
486
+ targetWindow = windows[0] as Gtk.Window;
487
+ }
488
+
489
+ const result = await testing.screenshot(targetWindow);
490
+ return { data: result.data, mimeType: result.mimeType };
491
+ }
492
+
493
+ default:
494
+ throw methodNotFoundError(method);
495
+ }
496
+ }
497
+ }
498
+
499
+ let globalClient: McpClient | null = null;
500
+
501
+ export const startMcpClient = async (appId: string): Promise<McpClient> => {
502
+ if (globalClient) {
503
+ return globalClient;
504
+ }
505
+
506
+ globalClient = new McpClient({ appId });
507
+
508
+ await globalClient.connect().catch(() => {});
509
+
510
+ return globalClient;
511
+ };
512
+
513
+ export const stopMcpClient = (): void => {
514
+ if (globalClient) {
515
+ globalClient.disconnect();
516
+ globalClient = null;
517
+ }
518
+ };
@@ -0,0 +1,89 @@
1
+ import RefreshRuntime from "react-refresh/runtime";
2
+
3
+ type ComponentType = (...args: unknown[]) => unknown;
4
+
5
+ RefreshRuntime.injectIntoGlobalHook(globalThis);
6
+ globalThis.$RefreshReg$ = () => {};
7
+ globalThis.$RefreshSig$ = () => (type: unknown) => type;
8
+
9
+ /**
10
+ * Creates registration functions for a module's React components.
11
+ *
12
+ * Used internally by the Vite plugin to register components
13
+ * for React Fast Refresh.
14
+ *
15
+ * @param moduleId - Unique identifier for the module
16
+ * @returns Registration functions for the module
17
+ * @internal
18
+ */
19
+ export function createModuleRegistration(moduleId: string): {
20
+ $RefreshReg$: (type: ComponentType, id: string) => void;
21
+ $RefreshSig$: typeof RefreshRuntime.createSignatureFunctionForTransform;
22
+ } {
23
+ return {
24
+ $RefreshReg$: (type: ComponentType, id: string) => {
25
+ RefreshRuntime.register(type, `${moduleId} ${id}`);
26
+ },
27
+ $RefreshSig$: RefreshRuntime.createSignatureFunctionForTransform,
28
+ };
29
+ }
30
+
31
+ function isLikelyComponentType(value: unknown): boolean {
32
+ if (typeof value !== "function") {
33
+ return false;
34
+ }
35
+
36
+ const func = value as { $$typeof?: symbol };
37
+
38
+ if (func.$$typeof === Symbol.for("react.memo") || func.$$typeof === Symbol.for("react.forward_ref")) {
39
+ return true;
40
+ }
41
+
42
+ const name = (value as { name?: string }).name;
43
+ if (typeof name === "string" && /^[A-Z]/.test(name)) {
44
+ return true;
45
+ }
46
+
47
+ return false;
48
+ }
49
+
50
+ /**
51
+ * Checks if a module's exports form a React Refresh boundary.
52
+ *
53
+ * A module is a refresh boundary if all its exports are React components,
54
+ * allowing for fast refresh without full page reload.
55
+ *
56
+ * @param moduleExports - The module's exports object
57
+ * @returns `true` if the module can be fast-refreshed
58
+ * @internal
59
+ */
60
+ export function isReactRefreshBoundary(moduleExports: Record<string, unknown>): boolean {
61
+ if (RefreshRuntime.isLikelyComponentType(moduleExports)) {
62
+ return true;
63
+ }
64
+
65
+ for (const key in moduleExports) {
66
+ if (key === "__esModule") {
67
+ continue;
68
+ }
69
+
70
+ const value = moduleExports[key];
71
+
72
+ if (!isLikelyComponentType(value)) {
73
+ return false;
74
+ }
75
+ }
76
+
77
+ return Object.keys(moduleExports).filter((k) => k !== "__esModule").length > 0;
78
+ }
79
+
80
+ /**
81
+ * Triggers React Fast Refresh to re-render components.
82
+ *
83
+ * Called after module updates when all exports are React components.
84
+ *
85
+ * @internal
86
+ */
87
+ export function performRefresh(): void {
88
+ RefreshRuntime.performReactRefresh();
89
+ }
@@ -0,0 +1,26 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import ejs from "ejs";
4
+ import type { TestingOption } from "./create.js";
5
+
6
+ export type TemplateContext = {
7
+ name: string;
8
+ appId: string;
9
+ title: string;
10
+ testing: TestingOption;
11
+ };
12
+
13
+ const getTemplatesDir = (): string => {
14
+ return join(import.meta.dirname, "..", "templates");
15
+ };
16
+
17
+ const renderTemplate = (templatePath: string, context: TemplateContext): string => {
18
+ const templateContent = readFileSync(templatePath, "utf-8");
19
+ return ejs.render(templateContent, context);
20
+ };
21
+
22
+ export const renderFile = (templateName: string, context: TemplateContext): string => {
23
+ const templatesDir = getTemplatesDir();
24
+ const templatePath = join(templatesDir, templateName);
25
+ return renderTemplate(templatePath, context);
26
+ };
@@ -0,0 +1,32 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ const ASSET_RE = /\.(png|jpe?g|gif|svg|webp|webm|mp4|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf|ico|avif)$/i;
4
+
5
+ /**
6
+ * Vite plugin that resolves static asset imports to filesystem paths.
7
+ *
8
+ * In dev mode, asset imports resolve to the absolute source file path.
9
+ * In build mode, Vite's built-in asset pipeline handles emission and
10
+ * hashing; the `renderBuiltUrl` config in the builder converts the
11
+ * URL to a filesystem path via `import.meta.url`.
12
+ */
13
+ export function gtkxAssets(): Plugin {
14
+ let isBuild = false;
15
+
16
+ return {
17
+ name: "gtkx:assets",
18
+ enforce: "pre",
19
+
20
+ configResolved(config) {
21
+ isBuild = config.command === "build";
22
+ },
23
+
24
+ load(id) {
25
+ if (isBuild || !ASSET_RE.test(id)) {
26
+ return;
27
+ }
28
+
29
+ return `export default ${JSON.stringify(id)};`;
30
+ },
31
+ };
32
+ }