Skip to content

Commit 62ac640

Browse files
authored
improvement: vue-devtools debugging (#268)
* improvement: vue-devtools debugging * timeline grouped and event named
1 parent b77e288 commit 62ac640

File tree

10 files changed

+90
-62
lines changed

10 files changed

+90
-62
lines changed

.github/CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ The `build` script builds all public packages (packages without `private: true`
9191
Packages to build can be specified with fuzzy matching:
9292

9393
```bash
94-
# build compiler only
95-
yarn build compiler
94+
# build message-compiler only
95+
yarn build message-compiler
9696

9797
# build all packages
9898
yarn build --all
@@ -128,13 +128,13 @@ By default, each package will be built in multiple distribution formats as speci
128128
For example, to build `compiler` with the global build only:
129129

130130
```bash
131-
yarn build compiler -f global
131+
yarn build message-compiler -f global
132132
```
133133

134134
Multiple formats can be specified as a comma-separated list:
135135

136136
```bash
137-
yarn build compiler -f esm-browser,cjs
137+
yarn build message-compiler -f esm-browser,cjs
138138
```
139139

140140
#### Build with Source Maps

packages/core-base/src/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ export function handleMissing<Message = string>(
332332
emitter.emit(DevToolsTimelineEvents.MISSING, {
333333
locale,
334334
key,
335-
type
335+
type,
336+
groupId: `${type}:${key}`
336337
})
337338
}
338339
}

packages/core-base/src/datetime.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ export function datetime<DateTimeFormats, Message = string>(
190190
type,
191191
key,
192192
from,
193-
to
193+
to,
194+
groupId: `${type}:${key}`
194195
})
195196
}
196197
}

packages/core-base/src/debugger/constants.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@ import type { Locale, CoreMissingType } from '../context'
55
export const enum DevToolsIDs {
66
PLUGIN = 'vue-devtools-plugin-vue-i18n',
77
CUSTOM_INSPECTOR = 'vue-i18n-resource-inspector',
8-
TIMELINE_COMPILE_ERROR = 'vue-i18n-compile-error',
9-
TIMELINE_MISSING = 'vue-i18n-missing',
10-
TIMELINE_FALLBACK = 'vue-i18n-fallback',
11-
TIMELINE_PERFORMANCE = 'vue-i18n-performance'
8+
TIMELINE = 'vue-i18n-timeline'
129
}
1310

1411
export const DevToolsLabels: Record<string, string> = {
1512
[DevToolsIDs.PLUGIN]: 'Vue I18n devtools',
1613
[DevToolsIDs.CUSTOM_INSPECTOR]: 'I18n Resources',
17-
[DevToolsIDs.TIMELINE_COMPILE_ERROR]: 'Vue I18n: Compile Errors',
18-
[DevToolsIDs.TIMELINE_MISSING]: 'Vue I18n: Missing',
19-
[DevToolsIDs.TIMELINE_FALLBACK]: 'Vue I18n: Fallback',
20-
[DevToolsIDs.TIMELINE_PERFORMANCE]: 'Vue I18n: Performance'
14+
[DevToolsIDs.TIMELINE]: 'Vue I18n'
2115
}
2216

2317
export const DevToolsPlaceholders: Record<string, string> = {
2418
[DevToolsIDs.CUSTOM_INSPECTOR]: 'Search for scopes ...'
2519
}
2620

2721
export const DevToolsTimelineColors: Record<string, number> = {
28-
[DevToolsIDs.TIMELINE_COMPILE_ERROR]: 0xff0000,
29-
[DevToolsIDs.TIMELINE_MISSING]: 0xffcd19,
30-
[DevToolsIDs.TIMELINE_FALLBACK]: 0xffcd19,
31-
[DevToolsIDs.TIMELINE_PERFORMANCE]: 0xffcd19
22+
[DevToolsIDs.TIMELINE]: 0xffcd19
3223
}
3324

3425
export const enum DevToolsTimelineEvents {
@@ -46,46 +37,42 @@ export type DevToolsTimelineEventPayloads = {
4637
error: string
4738
start?: number
4839
end?: number
40+
groupId?: string
4941
}
5042
[DevToolsTimelineEvents.MISSING]: {
5143
locale: Locale
5244
key: Path
5345
type: CoreMissingType
46+
groupId?: string
5447
}
5548
[DevToolsTimelineEvents.FALBACK]: {
5649
key: Path
5750
type: CoreMissingType
5851
from?: Locale
5952
to: Locale | 'global'
53+
groupId?: string
6054
}
6155
[DevToolsTimelineEvents.MESSAGE_RESOLVE]: {
6256
type: DevToolsTimelineEvents.MESSAGE_RESOLVE
6357
key: Path
6458
message: PathValue
6559
time: number
60+
groupId?: string
6661
}
6762
[DevToolsTimelineEvents.MESSAGE_COMPILATION]: {
6863
type: DevToolsTimelineEvents.MESSAGE_COMPILATION
6964
message: PathValue
7065
time: number
66+
groupId?: string
7167
}
7268
[DevToolsTimelineEvents.MESSAGE_EVALUATION]: {
7369
type: DevToolsTimelineEvents.MESSAGE_EVALUATION
7470
value: unknown
7571
time: number
72+
groupId?: string
7673
}
7774
}
7875

79-
export const DevToolsTimelineLayerMaps: Record<string, string> = {
80-
[DevToolsTimelineEvents.COMPILE_ERROR]: DevToolsIDs.TIMELINE_COMPILE_ERROR,
81-
[DevToolsTimelineEvents.MISSING]: DevToolsIDs.TIMELINE_MISSING,
82-
[DevToolsTimelineEvents.FALBACK]: DevToolsIDs.TIMELINE_FALLBACK,
83-
[DevToolsTimelineEvents.MESSAGE_RESOLVE]: DevToolsIDs.TIMELINE_PERFORMANCE,
84-
[DevToolsTimelineEvents.MESSAGE_COMPILATION]:
85-
DevToolsIDs.TIMELINE_PERFORMANCE,
86-
[DevToolsTimelineEvents.MESSAGE_EVALUATION]: DevToolsIDs.TIMELINE_PERFORMANCE
87-
}
88-
8976
export type DevToolsEmitterEvents = {
9077
[DevToolsTimelineEvents.COMPILE_ERROR]: DevToolsTimelineEventPayloads[DevToolsTimelineEvents.COMPILE_ERROR]
9178
[DevToolsTimelineEvents.MISSING]: DevToolsTimelineEventPayloads[DevToolsTimelineEvents.MISSING]

packages/core-base/src/number.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ export function number<NumberFormats, Message = string>(
184184
type,
185185
key,
186186
from,
187-
to
187+
to,
188+
groupId: `${type}:${key}`
188189
})
189190
}
190191
}

packages/core-base/src/translate.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ function resolveMessageFormat<Messages, Message>(
396396
type,
397397
key,
398398
from,
399-
to
399+
to,
400+
groupId: `${type}:${key}`
400401
})
401402
}
402403
}
@@ -429,7 +430,8 @@ function resolveMessageFormat<Messages, Message>(
429430
type: DevToolsTimelineEvents.MESSAGE_RESOLVE,
430431
key,
431432
message: format,
432-
time: end - start
433+
time: end - start,
434+
groupId: `${type}:${key}`
433435
})
434436
}
435437
if (startTag && endTag && mark && measure) {
@@ -503,7 +505,8 @@ function compileMessasgeFormat<Messages, Message>(
503505
emitter.emit(DevToolsTimelineEvents.MESSAGE_COMPILATION, {
504506
type: DevToolsTimelineEvents.MESSAGE_COMPILATION,
505507
message: format,
506-
time: end - start
508+
time: end - start,
509+
groupId: `${'translate'}:${key}`
507510
})
508511
}
509512
if (startTag && endTag && mark && measure) {
@@ -545,7 +548,8 @@ function evaluateMessage<Messages, Message>(
545548
emitter.emit(DevToolsTimelineEvents.MESSAGE_EVALUATION, {
546549
type: DevToolsTimelineEvents.MESSAGE_EVALUATION,
547550
value: messaged,
548-
time: end - start
551+
time: end - start,
552+
groupId: `${'translate'}:${(msg as MessageFunctionInternal).key}`
549553
})
550554
}
551555
if (startTag && endTag && mark && measure) {
@@ -617,7 +621,8 @@ function getCompileOptions<Messages, Message>(
617621
message: source,
618622
error: err.message,
619623
start: err.location && err.location.start.offset,
620-
end: err.location && err.location.end.offset
624+
end: err.location && err.location.end.offset,
625+
groupId: `${'translate'}:${key}`
621626
})
622627
}
623628
console.error(codeFrame ? `${message}\n${codeFrame}` : message)

packages/vue-i18n/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"dependencies": {
3737
"@intlify/core-base": "9.0.0-beta.16",
3838
"@intlify/shared": "9.0.0-beta.16",
39-
"@vue/devtools-api": "^6.0.0-beta.2"
39+
"@vue/devtools-api": "^6.0.0-beta.3"
4040
},
4141
"peerDependencies": {
4242
"vue": "^3.0.0"

packages/vue-i18n/src/composer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,8 @@ export function createComposer<
12521252
emitter.emit(DevToolsTimelineEvents.FALBACK, {
12531253
type: warnType,
12541254
key,
1255-
to: 'global'
1255+
to: 'global',
1256+
groupId: `${warnType}:${key}`
12561257
})
12571258
}
12581259
}

packages/vue-i18n/src/devtools.ts

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { setupDevtoolsPlugin, Hooks } from '@vue/devtools-api'
1+
import {
2+
setupDevtoolsPlugin,
3+
Hooks,
4+
AppRecord,
5+
ComponentTreeNode
6+
} from '@vue/devtools-api'
27
import {
38
DevToolsIDs,
49
DevToolsTimelineColors,
510
DevToolsLabels,
611
DevToolsPlaceholders,
7-
DevToolsTimelineEvents,
8-
DevToolsTimelineLayerMaps
12+
DevToolsTimelineEvents
913
} from '@intlify/core-base'
1014
import { isFunction, isObject } from '@intlify/shared'
1115

@@ -89,6 +93,13 @@ export async function enableDevTools<
8993
api => {
9094
devtoolsApi = api
9195

96+
api.on.walkComponentTree((payload, ctx) => {
97+
updateComponentTreeDataTags(
98+
ctx.currentAppRecord,
99+
payload.componentTreeData
100+
)
101+
})
102+
92103
api.on.inspectComponent(payload => {
93104
const componentInstance = payload.componentInstance
94105
if (
@@ -129,27 +140,9 @@ export async function enableDevTools<
129140
})
130141

131142
api.addTimelineLayer({
132-
id: DevToolsIDs.TIMELINE_COMPILE_ERROR,
133-
label: DevToolsLabels[DevToolsIDs.TIMELINE_COMPILE_ERROR],
134-
color: DevToolsTimelineColors[DevToolsIDs.TIMELINE_COMPILE_ERROR]
135-
})
136-
137-
api.addTimelineLayer({
138-
id: DevToolsIDs.TIMELINE_PERFORMANCE,
139-
label: DevToolsLabels[DevToolsIDs.TIMELINE_PERFORMANCE],
140-
color: DevToolsTimelineColors[DevToolsIDs.TIMELINE_PERFORMANCE]
141-
})
142-
143-
api.addTimelineLayer({
144-
id: DevToolsIDs.TIMELINE_MISSING,
145-
label: DevToolsLabels[DevToolsIDs.TIMELINE_MISSING],
146-
color: DevToolsTimelineColors[DevToolsIDs.TIMELINE_MISSING]
147-
})
148-
149-
api.addTimelineLayer({
150-
id: DevToolsIDs.TIMELINE_FALLBACK,
151-
label: DevToolsLabels[DevToolsIDs.TIMELINE_FALLBACK],
152-
color: DevToolsTimelineColors[DevToolsIDs.TIMELINE_FALLBACK]
143+
id: DevToolsIDs.TIMELINE,
144+
label: DevToolsLabels[DevToolsIDs.TIMELINE],
145+
color: DevToolsTimelineColors[DevToolsIDs.TIMELINE]
153146
})
154147

155148
resolve(true)
@@ -162,6 +155,26 @@ export async function enableDevTools<
162155
})
163156
}
164157

158+
function updateComponentTreeDataTags(
159+
appRecord: AppRecord,
160+
treeData: ComponentTreeNode
161+
): void {
162+
const instance = appRecord.instanceMap.get(treeData.id)
163+
if (instance && instance.vnode.el.__INTLIFY__) {
164+
const label =
165+
instance.type.name || instance.type.displayName || instance.type.__file
166+
const tag = {
167+
label: `i18n (${label} Scope)`,
168+
textColor: 0x000000,
169+
backgroundColor: 0xffcd19
170+
}
171+
treeData.tags = [tag]
172+
}
173+
for (const node of treeData.children) {
174+
updateComponentTreeDataTags(appRecord, node)
175+
}
176+
}
177+
165178
function inspectComposer(
166179
instanceData: InspectedComponentData,
167180
composer: Composer
@@ -386,12 +399,26 @@ export function addTimelineEvent(
386399
payload?: DevToolsTimelineEventPayloads[DevToolsTimelineEvents]
387400
): void {
388401
if (devtoolsApi) {
402+
let groupId: string | undefined
403+
if (payload && 'groupId' in payload) {
404+
groupId = payload.groupId
405+
delete payload.groupId
406+
}
389407
devtoolsApi.addTimelineEvent({
390-
layerId: DevToolsTimelineLayerMaps[event],
408+
layerId: DevToolsIDs.TIMELINE,
391409
event: {
410+
title: event,
411+
groupId,
392412
time: Date.now(),
393413
meta: {},
394-
data: payload || {}
414+
data: payload || {},
415+
logType:
416+
event === DevToolsTimelineEvents.COMPILE_ERROR
417+
? 'error'
418+
: event === DevToolsTimelineEvents.FALBACK ||
419+
event === DevToolsTimelineEvents.MISSING
420+
? 'warning'
421+
: 'default'
395422
}
396423
})
397424
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,11 @@
15241524
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.0.0-beta.2.tgz#833ad3335f97ae9439e26247d97f9baf7b5a6116"
15251525
integrity sha512-5k0A8ffjNNukOiceImBdx1e3W5Jbpwqsu7xYHiZVu9mn4rYxFztIt+Q25mOHm7nwvDnMHrE7u5KtY2zmd+81GA==
15261526

1527+
"@vue/devtools-api@^6.0.0-beta.3":
1528+
version "6.0.0-beta.3"
1529+
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.0.0-beta.3.tgz#5a66cc8beed688fe18c272ee7a8bd8ed7e35a54c"
1530+
integrity sha512-iJQhVyWzWIJxYIMjbZpljZQfU4gL2IMD5YQm3HXO8tQRU7RqqnD3f1WHn+vrqvrSvM8Qw2BeNugwdBBmbK8Oxg==
1531+
15271532
15281533
version "3.0.4"
15291534
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.4.tgz#b6599dd8271a745960a03f05744ccf7991ba5d8d"

0 commit comments

Comments
 (0)