Skip to content

Commit a6f9d5c

Browse files
authored
Merge pull request #7400 from QwikDev/fix-json-devtools
qwik-labs: fix devtools json
2 parents 0f9b36b + 0d175f7 commit a6f9d5c

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
@@ -4,7 +4,7 @@ import { readFile, writeFile } from 'node:fs/promises';
44
import { join, resolve } from 'node:path';
55
import { type PluginOption } from 'vite';
66

7-
const logWarn = (message?: any, ...rest) => {
7+
const logWarn = (message?: any, ...rest: any[]) => {
88
// eslint-disable-next-line no-console
99
console.warn('\x1b[33m%s\x1b[0m', `qwikInsight()[WARN]: ${message}`, ...rest);
1010
};
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,
@@ -438,7 +440,7 @@ export function qwikJsonDebug(
438440
}
439441
}
440442

441-
export interface QwikJson {
443+
interface QwikJson {
442444
refs: Record<string, string>;
443445
ctx: Record<
444446
string,
@@ -452,34 +454,34 @@ export interface QwikJson {
452454
objs: Array<QwikJsonObjsPrimitives | QwikJsonObjsObj>;
453455
subs: Array<Array<string>>;
454456
}
455-
export type QwikJsonObjsPrimitives = string | boolean | number | null;
456-
export type QwikJsonObjsObj = Record<string, QwikJsonObjsPrimitives>;
457+
type QwikJsonObjsPrimitives = string | boolean | number | null;
458+
type QwikJsonObjsObj = Record<string, QwikJsonObjsPrimitives>;
457459

458-
export interface Base {
460+
interface Base {
459461
__id: number;
460462
__backRefs: any[];
461463
}
462464

463-
export interface QRL extends Base {
465+
interface QRL extends Base {
464466
chunk: string;
465467
symbol: string;
466468
capture: any[];
467469
}
468470

469-
export interface QRefs {
471+
interface QRefs {
470472
element: Element;
471473
refMap: any[];
472474
listeners: Listener[];
473475
}
474476

475-
export interface Listener {
477+
interface Listener {
476478
event: string;
477479
qrl: QRL;
478480
}
479481

480-
export interface SubscriberEffect {}
482+
interface SubscriberEffect {}
481483

482-
export interface QContext {
484+
interface QContext {
483485
element: Node | null;
484486
props: Record<string, any> | null;
485487
componentQrl: QRL | null;
@@ -490,39 +492,9 @@ export interface QContext {
490492
scopeIds: string[] | null;
491493
}
492494

493-
export interface DebugState {
495+
interface DebugState {
494496
refs: Record<string, QRefs>;
495497
ctx: Record<string, QContext>;
496498
objs: any[];
497499
subs: unknown;
498500
}
499-
500-
export type QwikType =
501-
| 'string'
502-
| 'number'
503-
| 'bigint'
504-
| 'boolean'
505-
| 'function'
506-
| 'undefined'
507-
| 'object'
508-
| 'symbol'
509-
// Qwik custom types
510-
| 'QRL'
511-
| 'Signal'
512-
| 'SignalWrapper'
513-
| 'Task'
514-
| 'Resource'
515-
| 'URL'
516-
| 'Date'
517-
| 'Regex'
518-
| 'Error'
519-
| 'DerivedSignal'
520-
| 'FormData'
521-
| 'URLSearchParams'
522-
| 'Component'
523-
| 'NoFiniteNumber'
524-
| 'JSXNode'
525-
| 'BigInt'
526-
| 'Set'
527-
| 'Map'
528-
| '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": ["src"],
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)