| 6 | const RESOLVED_ID = '\0' + VIRTUAL_ID; |
| 7 | |
| 8 | export function nsHmrClientVitePlugin(opts: { platform: string; verbose?: boolean }): Plugin { |
| 9 | let config: ResolvedConfig | undefined; |
| 10 | |
| 11 | return { |
| 12 | name: 'ns-hmr-client', |
| 13 | configResolved(c) { |
| 14 | config = c; |
| 15 | }, |
| 16 | resolveId(id) { |
| 17 | if (id === VIRTUAL_ID) return RESOLVED_ID; |
| 18 | return null; |
| 19 | }, |
| 20 | load(id) { |
| 21 | if (id !== RESOLVED_ID) return null; |
| 22 | |
| 23 | const clientPath = require.resolve('@nativescript/vite/hmr/client/index.js'); |
| 24 | |
| 25 | // Build ws url from Vite server info |
| 26 | let host = process.env.NS_HMR_HOST || (config?.server?.host as any); |
| 27 | // Android emu special-case |
| 28 | if (opts.platform === 'android' && (host === '0.0.0.0' || !host || host === true)) { |
| 29 | host = '10.0.2.2'; |
| 30 | } else if (!host) { |
| 31 | host = 'localhost'; |
| 32 | } |
| 33 | const port = Number(config?.server?.port || 5173); |
| 34 | const secure = !!config?.server?.https; |
| 35 | const protocol = secure ? 'wss' : 'ws'; |
| 36 | const wsUrl = `${protocol}://${host}:${port}/ns-hmr`; |
| 37 | |
| 38 | // Import client and start it with explicit ws URL |
| 39 | const banner = opts.verbose ? `console.log('[ns-hmr-client] starting client -> ${wsUrl} (HTTP loader enabled via __NS_HTTP_ORIGIN__)');` : ''; |
| 40 | return ` |
| 41 | import startViteHMR from "${clientPath}"; |
| 42 | ${banner} |
| 43 | startViteHMR({ wsUrl: ${JSON.stringify(wsUrl)} }); |
| 44 | `; |
| 45 | }, |
| 46 | }; |
| 47 | } |