Skip to content

Commit c2e72c0

Browse files
authored
improvement: vue-i18n debugging on vue-devtools (#273)
1 parent 090db55 commit c2e72c0

File tree

3 files changed

+55
-75
lines changed

3 files changed

+55
-75
lines changed

packages/vue-i18n/src/devtools.ts

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type { App } from 'vue'
1717
import type {
1818
DevtoolsPluginApi,
1919
InspectedComponentData,
20-
CustomInspectorNode,
2120
CustomInspectorState,
2221
ComponentStateBase,
2322
HookPayloads
@@ -34,43 +33,6 @@ type _I18n<
3433
Legacy extends boolean
3534
> = I18n<Messages, DateTimeFormats, NumberFormats, Legacy> & I18nInternal
3635

37-
interface I18nRecord {
38-
id: number
39-
version: string
40-
}
41-
42-
const enum DevtoolsHooks {
43-
REGISTER = 'intlify:register'
44-
}
45-
46-
interface DevtoolsHook {
47-
emit(event: string, ...payload: unknown[]): void
48-
on(event: string, handler: Function): void
49-
off(event: string, handler: Function): void
50-
i18nRecords: I18nRecord[]
51-
}
52-
53-
export let devtools: DevtoolsHook
54-
55-
export function setDevtoolsHook(hook: DevtoolsHook): void {
56-
devtools = hook
57-
}
58-
59-
export function devtoolsRegisterI18n<
60-
Messages,
61-
DateTimeFormats,
62-
NumberFormats,
63-
Legacy extends boolean
64-
>(
65-
i18n: I18n<Messages, DateTimeFormats, NumberFormats, Legacy>,
66-
version: string
67-
): void {
68-
if (!devtools) {
69-
return
70-
}
71-
devtools.emit(DevtoolsHooks.REGISTER, i18n, version)
72-
}
73-
7436
let devtoolsApi: DevtoolsPluginApi
7537

7638
export async function enableDevTools<
@@ -96,7 +58,8 @@ export async function enableDevTools<
9658
api.on.walkComponentTree((payload, ctx) => {
9759
updateComponentTreeDataTags(
9860
ctx.currentAppRecord,
99-
payload.componentTreeData
61+
payload.componentTreeData,
62+
i18n
10063
)
10164
})
10265

@@ -106,10 +69,23 @@ export async function enableDevTools<
10669
componentInstance.vnode.el.__INTLIFY__ &&
10770
payload.instanceData
10871
) {
109-
inspectComposer(
110-
payload.instanceData,
111-
componentInstance.vnode.el.__INTLIFY__ as Composer
112-
)
72+
if (i18n.mode === 'legacy') {
73+
// ignore global scope on legacy mode
74+
if (
75+
componentInstance.vnode.el.__INTLIFY__ !==
76+
((i18n.global as unknown) as VueI18nInternal).__composer
77+
) {
78+
inspectComposer(
79+
payload.instanceData,
80+
componentInstance.vnode.el.__INTLIFY__ as Composer
81+
)
82+
}
83+
} else {
84+
inspectComposer(
85+
payload.instanceData,
86+
componentInstance.vnode.el.__INTLIFY__ as Composer
87+
)
88+
}
11389
}
11490
})
11591

@@ -155,23 +131,36 @@ export async function enableDevTools<
155131
})
156132
}
157133

158-
function updateComponentTreeDataTags(
134+
function updateComponentTreeDataTags<
135+
Messages,
136+
DateTimeFormats,
137+
NumberFormats,
138+
Legacy extends boolean
139+
>(
159140
appRecord: AppRecord,
160-
treeData: ComponentTreeNode
141+
treeData: ComponentTreeNode,
142+
i18n: _I18n<Messages, DateTimeFormats, NumberFormats, Legacy>
161143
): void {
162144
const instance = appRecord.instanceMap.get(treeData.id)
163145
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
146+
// prettier-ignore
147+
const global = i18n.mode === 'composition'
148+
? i18n.global
149+
: (i18n.global as unknown as VueI18nInternal).__composer
150+
// add custom tags local scope only
151+
if (instance.vnode.el.__INTLIFY__ !== global) {
152+
const label =
153+
instance.type.name || instance.type.displayName || instance.type.__file
154+
const tag = {
155+
label: `i18n (${label} Scope)`,
156+
textColor: 0x000000,
157+
backgroundColor: 0xffcd19
158+
}
159+
treeData.tags = [tag]
170160
}
171-
treeData.tags = [tag]
172161
}
173162
for (const node of treeData.children) {
174-
updateComponentTreeDataTags(appRecord, node)
163+
updateComponentTreeDataTags(appRecord, node, i18n)
175164
}
176165
}
177166

@@ -275,26 +264,31 @@ function registerScope<
275264
payload: HookPayloads[Hooks.GET_INSPECTOR_TREE],
276265
i18n: _I18n<Messages, DateTimeFormats, NumberFormats, Legacy>
277266
): void {
278-
const children: CustomInspectorNode[] = []
267+
payload.rootNodes.push({
268+
id: 'global',
269+
label: 'Global Scope'
270+
})
271+
// prettier-ignore
272+
const global = i18n.mode === 'composition'
273+
? i18n.global
274+
: (i18n.global as unknown as VueI18nInternal).__composer
279275
for (const [keyInstance, instance] of i18n.__instances) {
280276
// prettier-ignore
281277
const composer = i18n.mode === 'composition'
282278
? instance
283279
: (instance as unknown as VueI18nInternal).__composer
280+
if (global === composer) {
281+
continue
282+
}
284283
const label =
285284
keyInstance.type.name ||
286285
keyInstance.type.displayName ||
287286
keyInstance.type.__file
288-
children.push({
287+
payload.rootNodes.push({
289288
id: composer.id.toString(),
290289
label: `${label} Scope`
291290
})
292291
}
293-
payload.rootNodes.push({
294-
id: 'global',
295-
label: 'Global Scope',
296-
children
297-
})
298292
}
299293

300294
function inspectScope<

packages/vue-i18n/src/i18n.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ import { I18nWarnCodes, getWarnMessage } from './warnings'
2424
import { I18nErrorCodes, createI18nError } from './errors'
2525
import { apply } from './plugin'
2626
import { defineMixin } from './mixin'
27-
import {
28-
devtoolsRegisterI18n,
29-
enableDevTools,
30-
addTimelineEvent
31-
} from './devtools'
27+
import { enableDevTools, addTimelineEvent } from './devtools'
3228
import { createEmitter } from '@intlify/core-base'
33-
import { VERSION } from './misc'
3429

3530
import type { ComponentInternalInstance, ComponentOptions, App } from 'vue'
3631
import type {
@@ -449,13 +444,6 @@ export function createI18n<
449444
}
450445
}
451446

452-
if ((__DEV__ || __FEATURE_PROD_DEVTOOLS__) && !__NODE_JS__) {
453-
devtoolsRegisterI18n(
454-
i18n as I18n<Messages, DateTimeFormats, NumberFormats, Legacy>,
455-
VERSION
456-
)
457-
}
458-
459447
return i18n as I18n<Messages, DateTimeFormats, NumberFormats, Legacy>
460448
}
461449

packages/vue-i18n/src/misc.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getGlobalThis } from '@intlify/shared'
2-
import { setDevtoolsHook } from './devtools'
32

43
/**
54
* Vue I18n Version
@@ -50,7 +49,6 @@ export function initDev(): void {
5049
const target = getGlobalThis()
5150

5251
target.__INTLIFY__ = true
53-
setDevtoolsHook(target.__INTLIFY_DEVTOOLS_GLOBAL_HOOK__)
5452

5553
if (__BROWSER__) {
5654
console.info(

0 commit comments

Comments
 (0)