|
1 | | -import { ResolvedConfig, type Plugin } from 'vite'; |
2 | | -import { getServerFunctions } from './rpc'; |
3 | | -import { createServerRpc, setViteServerContext, VIRTUAL_QWIK_DEVTOOLS_KEY, INNER_USE_HOOK } from '@devtools/kit'; |
4 | | -import VueInspector from 'vite-plugin-inspect' |
5 | | -import useCollectHooksSource from './utils/useCollectHooks' |
6 | | -import { parseQwikCode } from './parse/parse'; |
7 | | -import { startPreloading } from './npm/index'; |
8 | | -import updateConf from './utils/updateConf'; |
9 | | -import {debug} from 'debug' |
10 | | - |
11 | | -const log = debug('qwik:devtools:plugin'); |
12 | | -export function qwikDevtools(): Plugin[] { |
13 | | - let _config: ResolvedConfig; |
14 | | - const qwikData = new Map<string, any>(); |
15 | | - let preloadStarted = false; |
16 | | - const qwikDevtoolsPlugin: Plugin = { |
17 | | - name: 'vite-plugin-qwik-devtools', |
18 | | - apply: 'serve', |
19 | | - resolveId(id) { |
20 | | - // Normalize to a stable, absolute-like id so Qwik can generate runtime chunks |
21 | | - const clean = id.split('?')[0].split('#')[0]; |
22 | | - if ( |
23 | | - clean === VIRTUAL_QWIK_DEVTOOLS_KEY || |
24 | | - clean === `/${VIRTUAL_QWIK_DEVTOOLS_KEY}` || |
25 | | - clean === `\u0000${VIRTUAL_QWIK_DEVTOOLS_KEY}` || |
26 | | - clean === `/@id/${VIRTUAL_QWIK_DEVTOOLS_KEY}` |
27 | | - ) { |
28 | | - return `/${VIRTUAL_QWIK_DEVTOOLS_KEY}`; |
29 | | - } |
30 | | - }, |
31 | | - load(id) { |
32 | | - if ( |
33 | | - id === `/${VIRTUAL_QWIK_DEVTOOLS_KEY}` || |
34 | | - id === VIRTUAL_QWIK_DEVTOOLS_KEY || |
35 | | - id === `\u0000${VIRTUAL_QWIK_DEVTOOLS_KEY}` || |
36 | | - id === `/@id/${VIRTUAL_QWIK_DEVTOOLS_KEY}` |
37 | | - ) { |
38 | | - return { |
39 | | - code: useCollectHooksSource, |
40 | | - map: { mappings: '' }, |
41 | | - }; |
42 | | - } |
43 | | - }, |
44 | | - configResolved(viteConfig) { |
45 | | - _config = viteConfig; |
46 | | - updateConf(_config); |
47 | | - // Start preloading as early as possible, right after config is resolved |
48 | | - if (!preloadStarted) { |
49 | | - preloadStarted = true; |
50 | | - startPreloading({ config: _config }).catch((err) => { |
51 | | - log('[Qwik DevTools] Failed to start preloading:', err); |
52 | | - }); |
53 | | - } |
54 | | - }, |
55 | | - transform: { |
56 | | - order: 'pre', |
57 | | - handler(code, id) { |
58 | | - const mode = process.env.MODE; |
59 | | - // Ensure virtual import is present at the very top once when a component$ is present |
60 | | - if (id.endsWith('.tsx') && code.includes('component$')) { |
61 | | - if (!code.includes(VIRTUAL_QWIK_DEVTOOLS_KEY)) { |
62 | | - |
63 | | - const importLine = `import { ${INNER_USE_HOOK} } from '${VIRTUAL_QWIK_DEVTOOLS_KEY}';\n` |
64 | | - code = importLine + code |
65 | | - }else { |
66 | | - log('importing virtual qwik devtools', VIRTUAL_QWIK_DEVTOOLS_KEY, code); |
67 | | - } |
68 | | - code = parseQwikCode(code, {path: id}) |
69 | | - } |
70 | | - // Only transform the root component file |
71 | | - if (id.endsWith('root.tsx')) { |
72 | | - const importPath = |
73 | | - mode === 'dev' ? '@devtools/ui' : '@qwik.dev/devtools/ui'; |
74 | | - // Check if QwikDevtools import already exists |
75 | | - if (!code.includes(importPath)) { |
76 | | - // Add import for QwikDevtools using the correct package name |
77 | | - code = `import { QwikDevtools } from '${importPath}';\n${code}`; |
78 | | - } |
79 | | - |
80 | | - // Find the closing body tag and append QwikDevtools at the end of body |
81 | | - const match = code.match(/<body[^>]*>([\s\S]*?)<\/body>/); |
82 | | - if (match) { |
83 | | - const bodyContent = match[1]; |
84 | | - const newBodyContent = `${bodyContent}\n <QwikDevtools />`; |
85 | | - code = code.replace(bodyContent, newBodyContent); |
86 | | - } |
87 | | - |
88 | | - return { |
89 | | - code, |
90 | | - map: null, |
91 | | - }; |
92 | | - } |
93 | | - |
94 | | - return { |
95 | | - code, |
96 | | - map: { mappings: '' }, |
97 | | - }; |
98 | | - }, |
99 | | - }, |
100 | | - configureServer(server) { |
101 | | - setViteServerContext(server as any); |
102 | | - |
103 | | - const rpcFunctions = getServerFunctions({ server, config: _config, qwikData }); |
104 | | - |
105 | | - createServerRpc(rpcFunctions); |
106 | | - |
107 | | - // Preloading should have already started in configResolved |
108 | | - }, |
109 | | - } |
110 | | - return [ |
111 | | - qwikDevtoolsPlugin, |
112 | | - VueInspector(), // Add the VueInspector plugin instance |
113 | | - ]; |
114 | | -} |
| 1 | +export { qwikDevtools } from './plugin'; |
0 commit comments