Skip to content

Commit f7e7e3e

Browse files
committed
qwik-labs: fix devtools json
1 parent 5282a39 commit f7e7e3e

File tree

6 files changed

+74
-52
lines changed

6 files changed

+74
-52
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Plugin } from 'vite';
2+
3+
const isCompiledStringId = (id: string) => /[?&]compiled-string/.test(id);
4+
5+
export function compiledStringPlugin(): Plugin {
6+
return {
7+
name: 'compiled-string-plugin',
8+
9+
resolveId(id) {
10+
// Keep the full id if it has our query param
11+
if (isCompiledStringId(id)) {
12+
return id;
13+
}
14+
return null;
15+
},
16+
17+
async load(id) {
18+
if (isCompiledStringId(id)) {
19+
// Extract the actual file path without the query parameter
20+
const filePath = id.split('?')[0];
21+
22+
try {
23+
// Let Rollup load the file content with side effects explicitly preserved
24+
const result = await this.load({
25+
id: filePath,
26+
moduleSideEffects: true, // Explicitly mark as having side effects
27+
});
28+
29+
if (!result) {
30+
throw new Error(`Failed to load file: ${filePath}`);
31+
}
32+
33+
// The code already contains the "// @preserve-side-effects" comment
34+
// which should help preserve side effects, but we'll ensure the resulting
35+
// string maintains that marker
36+
37+
return {
38+
code: `export default ${JSON.stringify(result.code)};`,
39+
map: null,
40+
};
41+
} catch (error) {
42+
console.error(`Error processing ${filePath}:`, error);
43+
return null;
44+
}
45+
}
46+
return null;
47+
},
48+
};
49+
}

packages/qwik-labs/src-vite/insights/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from 'node:path';
55
import { resolve } from 'path';
66
import { type PluginOption } from 'vite';
77

8-
const logWarn = (message?: any, ...rest) => {
8+
const logWarn = (message?: any, ...rest: any[]) => {
99
// eslint-disable-next-line no-console
1010
console.warn('\x1b[33m%s\x1b[0m', `qwikInsight()[WARN]: ${message}`, ...rest);
1111
};
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
import { qwikJsonDebug, runQwikJsonDebug } from './json';
2-
3-
export const devtoolsJsonSRC = `${runQwikJsonDebug}\n${qwikJsonDebug}\nrunQwikJsonDebug(window, document, qwikJsonDebug);`;
1+
// @ts-expect-error compiled-string-plugin
2+
export { default as devtoolsJsonSRC } from './json?compiled-string';

packages/qwik-labs/src/devtools/json.ts

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// IMPORTANT: This file should have no external imports!!!
2+
// It runs in the browser and is compiled to a string
23

3-
export function runQwikJsonDebug(window: Window, document: Document, debug: typeof qwikJsonDebug) {
4+
// required for side effects
5+
export {};
6+
7+
runQwikJsonDebug(window, document, qwikJsonDebug);
8+
9+
function runQwikJsonDebug(window: Window, document: Document, debug: typeof qwikJsonDebug) {
410
const parseQwikJSON = () => {
511
const rawData = JSON.parse(document.querySelector('script[type="qwik/json"]')!.textContent!);
612
const derivedFns =
@@ -20,11 +26,7 @@ export function runQwikJsonDebug(window: Window, document: Document, debug: type
2026
}
2127
}
2228

23-
export function qwikJsonDebug(
24-
document: Document,
25-
qwikJson: QwikJson,
26-
derivedFns: Function[]
27-
): DebugState {
29+
function qwikJsonDebug(document: Document, qwikJson: QwikJson, derivedFns: Function[]): DebugState {
2830
class Base {
2931
constructor(
3032
public __id: number,
@@ -434,7 +436,7 @@ export function qwikJsonDebug(
434436
}
435437
}
436438

437-
export interface QwikJson {
439+
interface QwikJson {
438440
refs: Record<string, string>;
439441
ctx: Record<
440442
string,
@@ -448,34 +450,34 @@ export interface QwikJson {
448450
objs: Array<QwikJsonObjsPrimitives | QwikJsonObjsObj>;
449451
subs: Array<Array<string>>;
450452
}
451-
export type QwikJsonObjsPrimitives = string | boolean | number | null;
452-
export type QwikJsonObjsObj = Record<string, QwikJsonObjsPrimitives>;
453+
type QwikJsonObjsPrimitives = string | boolean | number | null;
454+
type QwikJsonObjsObj = Record<string, QwikJsonObjsPrimitives>;
453455

454-
export interface Base {
456+
interface Base {
455457
__id: number;
456458
__backRefs: any[];
457459
}
458460

459-
export interface QRL extends Base {
461+
interface QRL extends Base {
460462
chunk: string;
461463
symbol: string;
462464
capture: any[];
463465
}
464466

465-
export interface QRefs {
467+
interface QRefs {
466468
element: Element;
467469
refMap: any[];
468470
listeners: Listener[];
469471
}
470472

471-
export interface Listener {
473+
interface Listener {
472474
event: string;
473475
qrl: QRL;
474476
}
475477

476-
export interface SubscriberEffect {}
478+
interface SubscriberEffect {}
477479

478-
export interface QContext {
480+
interface QContext {
479481
element: Node | null;
480482
props: Record<string, any> | null;
481483
componentQrl: QRL | null;
@@ -486,39 +488,9 @@ export interface QContext {
486488
scopeIds: string[] | null;
487489
}
488490

489-
export interface DebugState {
491+
interface DebugState {
490492
refs: Record<string, QRefs>;
491493
ctx: Record<string, QContext>;
492494
objs: any[];
493495
subs: unknown;
494496
}
495-
496-
export type QwikType =
497-
| 'string'
498-
| 'number'
499-
| 'bigint'
500-
| 'boolean'
501-
| 'function'
502-
| 'undefined'
503-
| 'object'
504-
| 'symbol'
505-
// Qwik custom types
506-
| 'QRL'
507-
| 'Signal'
508-
| 'SignalWrapper'
509-
| 'Task'
510-
| 'Resource'
511-
| 'URL'
512-
| 'Date'
513-
| 'Regex'
514-
| 'Error'
515-
| 'DerivedSignal'
516-
| 'FormData'
517-
| 'URLSearchParams'
518-
| 'Component'
519-
| 'NoFiniteNumber'
520-
| 'JSXNode'
521-
| 'BigInt'
522-
| 'Set'
523-
| 'Map'
524-
| 'Document';

packages/qwik-labs/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"@qwik-client-manifest": ["../../qwik/src/server/server-modules.d.ts"]
2020
}
2121
},
22-
"include": ["src"]
22+
"include": ["."],
23+
"exclude": ["node_modules", "lib"]
2324
}

packages/qwik-labs/vite.config.mts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vite';
22
import { qwikVite } from '@builder.io/qwik/optimizer';
33
import dtsPlugin from 'vite-plugin-dts';
4+
import { compiledStringPlugin } from './compiled-string-plugin';
45

56
export default defineConfig(() => {
67
return {
@@ -15,6 +16,6 @@ export default defineConfig(() => {
1516
external: ['zod'],
1617
},
1718
},
18-
plugins: [qwikVite(), dtsPlugin()],
19+
plugins: [qwikVite(), dtsPlugin(), compiledStringPlugin()],
1920
};
2021
});

0 commit comments

Comments
 (0)