Skip to content

Commit ab37408

Browse files
authored
fix(electron): compatible with vue custom renderer (#164)
1 parent b569ec4 commit ab37408

File tree

16 files changed

+752
-71
lines changed

16 files changed

+752
-71
lines changed

packages/core/src/bridge/user-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ export function registerBridgeRpc(bridge: BridgeInstanceType) {
9191

9292
// get vue inspector
9393
bridgeRpcCore.on(bridgeRpcEvents.isVueInspectorDetected, async () => {
94-
return !!await devtools.api.getVueInspector()
94+
return !!await devtools?.api?.getVueInspector?.()
9595
})
9696

9797
// enable vue inspector
9898
bridgeRpcCore.on(bridgeRpcEvents.enableVueInspector, async () => {
99-
const inspector = await devtools.api.getVueInspector()
99+
const inspector = await devtools?.api?.getVueInspector?.()
100100
if (inspector)
101101
await inspector.enable()
102102
})

packages/devtools-kit/src/core/plugins/components.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export function registerComponentsDevTools(app: VueAppInstance) {
2828
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
2929
const instance = getComponentInstance(devtoolsContext.appRecord!, payload.instanceId)
3030
if (instance) {
31+
if (typeof DOMRect === 'undefined')
32+
return
33+
3134
payload.rect = getComponentBoundingRect(instance)
3235
if (payload.rect instanceof DOMRect) {
3336
payload.rect = {

packages/devtools/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
".": {
1414
"import": "./dist/index.mjs",
1515
"require": "./dist/index.cjs"
16+
},
17+
"./hook": {
18+
"types": "./dist/hook.d.ts",
19+
"import": "./dist/hook.mjs",
20+
"require": "./dist/hook.cjs"
1621
}
1722
},
1823
"main": "./dist/index.cjs",

packages/devtools/src/hook.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { devtools } from '@vue/devtools-kit'
2+
3+
devtools.init()

packages/devtools/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Options } from 'tsup'
33
export default <Options>{
44
entryPoints: [
55
'src/index.ts',
6+
'src/hook.ts',
67
],
78
esbuildOptions(options) {
89
if (options.format === 'esm')

packages/electron/scripts/build.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ async function buildBundle() {
4646
build({
4747
entry: [resolve('./src/user-app.ts')],
4848
...baseOptions,
49-
format: ['cjs', 'esm', 'iife'],
49+
format: ['cjs', 'esm'],
50+
clean: false,
51+
})
52+
53+
build({
54+
entry: [resolve('./src/user-app.iife.ts')],
55+
...baseOptions,
56+
format: ['iife'],
5057
clean: false,
5158
})
5259

packages/electron/src/devtools.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ function init() {
4343
})
4444
})
4545

46-
socket.on('vue-devtools:disconnect', () => {
46+
function shutdown() {
4747
app.unmount()
4848
reload = null
4949
socket.close()
5050
init()
51-
})
51+
}
52+
53+
socket.on('vue-devtools:disconnect', shutdown)
54+
socket.on('vue-devtools-disconnect-devtools', shutdown)
5255
}
5356

5457
init()

packages/electron/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function init() {
1010
app.use(
1111
'/',
1212
eventHandler(() => {
13-
const userAppContent = fs.readFileSync(path.join(__dirname, './user-app.js'), 'utf-8')
13+
const userAppContent = fs.readFileSync(path.join(__dirname, './user-app.iife.js'), 'utf-8')
1414
const processSyntaxPolyfill = `if(!window.process){window.process={env:{}}};`
1515
return processSyntaxPolyfill + userAppContent
1616
}),
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { isBrowser, target } from '@vue/devtools-shared'
2+
import { Bridge } from '../../core/src/bridge'
3+
import { prepareInjection } from '../../core/src/injection'
4+
import { devtools } from '../../devtools-kit/src/index'
5+
6+
export function init(io) {
7+
const createSocket = io
8+
const host = target.__VUE_DEVTOOLS_HOST__ || 'http://localhost'
9+
const port = target.__VUE_DEVTOOLS_PORT__ !== undefined ? target.__VUE_DEVTOOLS_PORT__ : 8098
10+
const fullHost = port ? `${host}:${port}` : host
11+
const socket = createSocket(fullHost)
12+
13+
// @TODO: de-duplicate
14+
devtools.init()
15+
16+
const bridge = new Bridge({
17+
tracker(fn) {
18+
socket.on('vue-devtools:message', (data) => {
19+
fn(data)
20+
})
21+
},
22+
trigger(data) {
23+
socket.emit('vue-devtools:message', data)
24+
},
25+
})
26+
27+
socket.on('connect', () => {
28+
prepareInjection(bridge)
29+
socket.emit('vue-devtools:init')
30+
})
31+
32+
// Global disconnect handler. Fires in two cases:
33+
// - after calling above socket.disconnect()
34+
// - once devtools is closed (that's why we need socket.disconnect() here too, to prevent further polling)
35+
socket.on('disconnect', () => {
36+
socket.disconnect()
37+
})
38+
39+
// Disconnect socket once other client is connected
40+
socket.on('vue-devtools:disconnect-user-app', () => {
41+
socket.disconnect()
42+
})
43+
44+
if (isBrowser) {
45+
window.addEventListener('beforeunload', () => {
46+
socket.emit('vue-devtools:disconnect')
47+
})
48+
}
49+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import io from 'socket.io-client/dist/socket.io.js'
2+
import { init } from './user-app.core'
3+
4+
init(io)

0 commit comments

Comments
 (0)