|
1 | 1 | import definition from 'virtual:user-background-entrypoint'; |
2 | 2 | import { initPlugins } from 'virtual:wxt-plugins'; |
3 | | -import { getDevServerWebSocket } from '../sandbox/dev-server-websocket'; |
4 | 3 | import { logger } from '../sandbox/utils/logger'; |
5 | 4 | import { browser } from 'wxt/browser'; |
6 | 5 | import { keepServiceWorkerAlive } from './utils/keep-service-worker-alive'; |
7 | 6 | import { reloadContentScript } from './utils/reload-content-scripts'; |
8 | 7 |
|
9 | | -if (import.meta.env.COMMAND === 'serve') { |
10 | | - try { |
11 | | - const ws = getDevServerWebSocket(); |
12 | | - ws.addWxtEventListener('wxt:reload-extension', () => { |
13 | | - browser.runtime.reload(); |
14 | | - }); |
15 | | - ws.addWxtEventListener('wxt:reload-content-script', (event) => { |
16 | | - reloadContentScript(event.detail); |
17 | | - }); |
| 8 | +if (import.meta.hot) { |
| 9 | + import.meta.hot.on('wxt:reload-extension', () => browser.runtime.reload()); |
| 10 | + import.meta.hot.on('wxt:reload-content-script', (event) => |
| 11 | + reloadContentScript(event.detail), |
| 12 | + ); |
18 | 13 |
|
19 | | - if (import.meta.env.MANIFEST_VERSION === 3) { |
20 | | - // Tell the server the background script is loaded and ready to go |
21 | | - ws.addEventListener('open', () => |
22 | | - ws.sendCustom('wxt:background-initialized'), |
23 | | - ); |
| 14 | + if (import.meta.env.MANIFEST_VERSION === 3) { |
| 15 | + let backgroundInitialized = false; |
| 16 | + // Tell the server the background script is loaded and ready to go |
| 17 | + import.meta.hot.on('vite:ws:connect', () => { |
| 18 | + if (backgroundInitialized) return; |
24 | 19 |
|
25 | | - // Web Socket will disconnect if the service worker is killed |
26 | | - keepServiceWorkerAlive(); |
27 | | - } |
28 | | - } catch (err) { |
29 | | - logger.error('Failed to setup web socket connection with dev server', err); |
| 20 | + import.meta.hot?.send('wxt:background-initialized'); |
| 21 | + backgroundInitialized = true; |
| 22 | + }); |
| 23 | + |
| 24 | + // Web Socket will disconnect if the service worker is killed |
| 25 | + keepServiceWorkerAlive(); |
30 | 26 | } |
31 | 27 |
|
32 | 28 | browser.commands.onCommand.addListener((command) => { |
|
0 commit comments