Skip to content

Commit 7c19896

Browse files
authored
fix: support vue2 app debugging in micro-app (#1657)
1 parent 10629e1 commit 7c19896

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Customize vue2 app scan selector
2+
> For example, if you are using micro-app as your micro-frontend framework, the devtools cannot find Vue2 apps in `<micro-app>` by default.
3+
4+
You can set a custom selector used to scan for Vue2 apps in your project with the following code in your frontend app:
5+
6+
```js
7+
if (process.env.NODE_ENV !== 'production') {
8+
window.VUE_DEVTOOLS_CONFIG = {
9+
customVue2ScanSelector: 'micro-app'
10+
}
11+
}
12+
```

packages/app-backend-core/src/app.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export async function registerApp (options: AppRecordOptions, ctx: BackendContex
2626

2727
async function registerAppJob (options: AppRecordOptions, ctx: BackendContext) {
2828
// Dedupe
29-
if (ctx.appRecords.find(a => a.options === options)) {
29+
if (ctx.appRecords.find(a => a.options.app === options.app)) {
3030
return
3131
}
3232

@@ -194,15 +194,25 @@ export async function sendApps (ctx: BackendContext) {
194194
})
195195
}
196196

197+
function removeAppRecord (appRecord: AppRecord, ctx: BackendContext) {
198+
try {
199+
appIds.delete(appRecord.id)
200+
const index = ctx.appRecords.indexOf(appRecord)
201+
if (index !== -1) ctx.appRecords.splice(index, 1)
202+
removeLayersForApp(appRecord.options.app, ctx)
203+
ctx.bridge.send(BridgeEvents.TO_FRONT_APP_REMOVE, { id: appRecord.id })
204+
} catch (e) {
205+
if (SharedData.debugInfo) {
206+
console.error(e)
207+
}
208+
}
209+
}
210+
197211
export async function removeApp (app: App, ctx: BackendContext) {
198212
try {
199213
const appRecord = await getAppRecord(app, ctx)
200214
if (appRecord) {
201-
appIds.delete(appRecord.id)
202-
const index = ctx.appRecords.indexOf(appRecord)
203-
if (index !== -1) ctx.appRecords.splice(index, 1)
204-
removeLayersForApp(app, ctx)
205-
ctx.bridge.send(BridgeEvents.TO_FRONT_APP_REMOVE, { id: appRecord.id })
215+
removeAppRecord(appRecord, ctx)
206216
}
207217
} catch (e) {
208218
if (SharedData.debugInfo) {
@@ -212,9 +222,17 @@ export async function removeApp (app: App, ctx: BackendContext) {
212222
}
213223

214224
// eslint-disable-next-line camelcase
215-
export async function _legacy_getAndRegisterApps (Vue: any, ctx: BackendContext) {
225+
export async function _legacy_getAndRegisterApps (ctx: BackendContext) {
226+
// Remove apps that are legacy
227+
ctx.appRecords.forEach(appRecord => {
228+
if (appRecord.meta.Vue) {
229+
removeAppRecord(appRecord, ctx)
230+
}
231+
})
232+
216233
const apps = scan()
217234
apps.forEach(app => {
235+
const Vue = app.constructor
218236
registerApp({
219237
app,
220238
types: {},

packages/app-backend-core/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ export async function initBackend (bridge: Bridge) {
6161

6262
if (hook.Vue) {
6363
connect()
64-
_legacy_getAndRegisterApps(hook.Vue, ctx)
65-
} else {
66-
hook.once(HookEvents.INIT, (Vue) => {
67-
_legacy_getAndRegisterApps(Vue, ctx)
68-
})
64+
_legacy_getAndRegisterApps(ctx)
6965
}
66+
hook.on(HookEvents.INIT, () => {
67+
_legacy_getAndRegisterApps(ctx)
68+
})
7069

7170
hook.on(HookEvents.APP_ADD, async app => {
71+
await _legacy_getAndRegisterApps(ctx)
7272
await registerApp(app, ctx)
7373

7474
// Will init connect

packages/app-backend-core/src/legacy/scan.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isBrowser, target } from '@vue-devtools/shared-utils'
2+
import { getPageConfig } from '../page-config'
23

34
const rootInstances = []
45

@@ -60,6 +61,17 @@ export function scan () {
6061
// Ignore
6162
}
6263
}
64+
65+
// Scan for Vue instances in the customTarget elements
66+
const { customVue2ScanSelector } = getPageConfig()
67+
const customTargets = customVue2ScanSelector ? document.querySelectorAll(customVue2ScanSelector) : []
68+
for (const customTarget of customTargets) {
69+
try {
70+
walkDocument(customTarget)
71+
} catch (e) {
72+
// Ignore
73+
}
74+
}
6375
} else {
6476
if (Array.isArray(target.__VUE_ROOT_INSTANCES__)) {
6577
target.__VUE_ROOT_INSTANCES__.map(processInstance)

packages/app-backend-core/src/page-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { target, SharedData } from '@vue-devtools/shared-utils'
33
export interface PageConfig {
44
openInEditorHost?: string
55
defaultSelectedAppId?: string
6+
customVue2ScanSelector?: string
67
}
78

89
let config: PageConfig = {}

0 commit comments

Comments
 (0)