Skip to content

Commit 5bfab06

Browse files
authored
experimental: improve meta for intlify devtools (#446)
* experimental: improve meta for intlify devtools * fix: lint errors
1 parent 22095d4 commit 5bfab06

File tree

7 files changed

+72
-55
lines changed

7 files changed

+72
-55
lines changed

packages/core-base/src/context.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export type MessageCompiler<Message = string> = (
7272
) => MessageFunction<Message>
7373

7474
export interface CoreOptions<Message = string> {
75+
version?: string
7576
locale?: Locale
7677
fallbackLocale?: FallbackLocale
7778
messages?: LocaleMessages<Message>
@@ -101,6 +102,7 @@ export interface CoreInternalOptions {
101102

102103
export interface CoreCommonContext<Message = string> {
103104
cid: number
105+
version: string
104106
locale: Locale
105107
fallbackLocale: FallbackLocale
106108
missing: CoreMissingHandler<Message> | null
@@ -147,7 +149,7 @@ export interface CoreInternalContext {
147149
__numberFormatters: Map<string, Intl.NumberFormat>
148150
__localeChainCache?: Map<Locale, Locale[]>
149151
__v_emitter?: VueDevToolsEmitter // eslint-disable-line camelcase
150-
__meta?: MetaInfo
152+
__meta: MetaInfo // for Intlify DevTools
151153
}
152154

153155
/**
@@ -184,6 +186,18 @@ export function registerMessageCompiler<Message>(
184186
_compiler = compiler
185187
}
186188

189+
// Additional Meta for Intlify DevTools
190+
let _additionalMeta: MetaInfo | null = /* #__PURE__*/ null
191+
192+
export const setAdditionalMeta = /* #__PURE__*/ (
193+
meta: MetaInfo | null
194+
): void => {
195+
_additionalMeta = meta
196+
}
197+
198+
export const getAdditionalMeta = /* #__PURE__*/ (): MetaInfo | null =>
199+
_additionalMeta
200+
187201
// ID for CoreContext
188202
let _cid = 0
189203

@@ -211,6 +225,7 @@ export function createCoreContext<
211225
Message
212226
> {
213227
// setup options
228+
const version = isString(options.version) ? options.version : VERSION
214229
const locale = isString(options.locale) ? options.locale : 'en-US'
215230
const fallbackLocale =
216231
isArray(options.fallbackLocale) ||
@@ -266,11 +281,12 @@ export function createCoreContext<
266281
const __numberFormatters = isObject(internalOptions.__numberFormatters)
267282
? internalOptions.__numberFormatters
268283
: new Map<string, Intl.NumberFormat>()
269-
const { __meta } = internalOptions
284+
const __meta = isObject(internalOptions.__meta) ? internalOptions.__meta : {}
270285

271286
_cid++
272287

273288
const context = {
289+
version,
274290
cid: _cid,
275291
locale,
276292
fallbackLocale,
@@ -310,7 +326,7 @@ export function createCoreContext<
310326

311327
// NOTE: experimental !!
312328
if (__DEV__ || __FEATURE_PROD_INTLIFY_DEVTOOLS__) {
313-
initI18nDevTools(context, VERSION, __meta)
329+
initI18nDevTools(context, version, __meta)
314330
}
315331

316332
return context

packages/core-base/src/translate.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
isTranslateFallbackWarn,
2222
handleMissing,
2323
getLocaleChain,
24-
NOT_REOSLVED
24+
NOT_REOSLVED,
25+
getAdditionalMeta
2526
} from './context'
2627
import { CoreWarnCodes, getWarnMessage } from './warnings'
2728
import { CoreErrorCodes, createCoreError } from './errors'
@@ -385,22 +386,30 @@ export function translate<Messages, Message = string>(
385386

386387
// NOTE: experimental !!
387388
if (__DEV__ || __FEATURE_PROD_INTLIFY_DEVTOOLS__) {
389+
// prettier-ignore
388390
const payloads = {
389391
timestamp: Date.now(),
390-
key:
391-
!isMessageFunction(format) && isString(key)
392-
? key
393-
: (format as MessageFunctionInternal).key!,
394-
locale: targetLocale!,
392+
key: isString(key)
393+
? key
394+
: isMessageFunction(format)
395+
? (format as MessageFunctionInternal).key!
396+
: '',
397+
locale: targetLocale || (isMessageFunction(format)
398+
? (format as MessageFunctionInternal).locale!
399+
: ''),
395400
format:
396-
!isMessageFunction(format) && isString(format)
397-
? format
398-
: (format as MessageFunctionInternal).source!,
401+
isString(format)
402+
? format
403+
: isMessageFunction(format)
404+
? (format as MessageFunctionInternal).source!
405+
: '',
399406
message: ret as string
400407
}
401-
if (((context as unknown) as CoreInternalContext).__meta) {
402-
;(payloads as AdditionalPayloads).meta = ((context as unknown) as CoreInternalContext).__meta
403-
}
408+
;(payloads as AdditionalPayloads).meta = Object.assign(
409+
{},
410+
((context as unknown) as CoreInternalContext).__meta,
411+
getAdditionalMeta() || {}
412+
)
404413
translateDevTools(payloads)
405414
}
406415

packages/vue-i18n/src/composer.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ import {
3737
getLocaleChain,
3838
NOT_REOSLVED,
3939
handleFlatJson,
40-
MessageFunction
40+
MessageFunction,
41+
setAdditionalMeta
4142
} from '@intlify/core-base'
4243
import { VueDevToolsTimelineEvents } from '@intlify/vue-devtools'
4344
import { I18nWarnCodes, getWarnMessage } from './warnings'
4445
import { I18nErrorCodes, createI18nError } from './errors'
46+
import { VERSION } from './misc'
4547

4648
import type { ComponentInternalInstance, VNode, VNodeArrayChildren } from 'vue'
4749
import type { WritableComputedRef, ComputedRef } from '@vue/reactivity'
@@ -323,7 +325,6 @@ export interface ComposerInternalOptions<
323325
__i18n?: CustomBlocks<Message>
324326
__i18nGlobal?: CustomBlocks<Message>
325327
__root?: Composer<Messages, DateTimeFormats, NumberFormats, Message>
326-
__meta?: MetaInfo
327328
}
328329

329330
/**
@@ -1171,6 +1172,14 @@ function deepCopy(src: any, des: any): void {
11711172
}
11721173
}
11731174

1175+
// for Intlify DevTools
1176+
const getMetaInfo = /* #__PURE__*/ (): MetaInfo | null => {
1177+
const instance = getCurrentInstance()
1178+
return instance && (instance.type as any)[DEVTOOLS_META] // eslint-disable-line @typescript-eslint/no-explicit-any
1179+
? { [DEVTOOLS_META]: (instance.type as any)[DEVTOOLS_META] } // eslint-disable-line @typescript-eslint/no-explicit-any
1180+
: null
1181+
}
1182+
11741183
/**
11751184
* Create composer interface factory
11761185
*
@@ -1199,7 +1208,7 @@ export function createComposer<
11991208
Options['numberFormats'],
12001209
Message
12011210
> {
1202-
const { __root, __meta } = options as ComposerInternalOptions<
1211+
const { __root } = options as ComposerInternalOptions<
12031212
Messages,
12041213
DateTimeFormats,
12051214
NumberFormats,
@@ -1312,6 +1321,7 @@ export function createComposer<
13121321
Message
13131322
> {
13141323
return createCoreContext<Message>({
1324+
version: VERSION,
13151325
locale: _locale.value,
13161326
fallbackLocale: _fallbackLocale.value,
13171327
messages: _messages.value,
@@ -1336,7 +1346,7 @@ export function createComposer<
13361346
__v_emitter: isPlainObject(_context)
13371347
? ((_context as unknown) as CoreInternalContext).__v_emitter
13381348
: undefined,
1339-
__meta: Object.assign({ framework: 'vue' }, __meta || {})
1349+
__meta: { framework: 'vue' }
13401350
} as CoreOptions<Message>) as CoreContext<
13411351
Messages,
13421352
DateTimeFormats,
@@ -1434,7 +1444,18 @@ export function createComposer<
14341444
successCondition: (val: unknown) => boolean
14351445
): U {
14361446
trackReactivityValues() // track reactive dependency
1437-
const ret = fn(_context)
1447+
// NOTE: experimental !!
1448+
let ret: unknown
1449+
if (__DEV__ || __FEATURE_PROD_INTLIFY_DEVTOOLS__) {
1450+
try {
1451+
setAdditionalMeta(getMetaInfo())
1452+
ret = fn(_context)
1453+
} finally {
1454+
setAdditionalMeta(null)
1455+
}
1456+
} else {
1457+
ret = fn(_context)
1458+
}
14381459
if (isNumber(ret) && ret === NOT_REOSLVED) {
14391460
const [key, arg2] = argumentParser()
14401461
if (
@@ -1842,8 +1863,7 @@ export function createComposer<
18421863
[TransrateVNodeSymbol]: transrateVNode,
18431864
[NumberPartsSymbol]: numberParts,
18441865
[DatetimePartsSymbol]: datetimeParts,
1845-
[SetPluralRulesSymbol]: setPluralRules,
1846-
[DevToolsMetaSymbol]: __meta
1866+
[SetPluralRulesSymbol]: setPluralRules
18471867
}
18481868

18491869
// for vue-devtools timeline event

packages/vue-i18n/src/i18n.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
createEmitter
1616
} from '@intlify/shared'
1717
import {
18-
DEVTOOLS_META,
1918
getLocaleMessages,
2019
createComposer,
2120
EnableEmitter,
@@ -631,11 +630,6 @@ export function useI18n<
631630
composerOptions.__i18n = type.__i18n
632631
}
633632

634-
if (type[DEVTOOLS_META]) {
635-
composerOptions.__meta = composerOptions.__meta || {}
636-
composerOptions.__meta[DEVTOOLS_META] = type[DEVTOOLS_META]
637-
}
638-
639633
if (global) {
640634
composerOptions.__root = global
641635
}
@@ -822,7 +816,7 @@ function injectGlobalFields<Messages, DateTimeFormats, NumberFormats>(
822816

823817
globalExportMethods.forEach(method => {
824818
const desc = Object.getOwnPropertyDescriptor(composer, method)
825-
if (!desc) {
819+
if (!desc || !desc.value) {
826820
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
827821
}
828822
Object.defineProperty(app.config.globalProperties, `$${method}`, desc)

packages/vue-i18n/src/legacy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ function convertComposerOptions<
11121112
return messages
11131113
}, (messages || {}) as LocaleMessages<VueMessageType>) as typeof options.messages
11141114
}
1115-
const { __i18n, __root, __meta } = options
1115+
const { __i18n, __root } = options
11161116

11171117
const datetimeFormats = options.datetimeFormats
11181118
const numberFormats = options.numberFormats
@@ -1137,8 +1137,7 @@ function convertComposerOptions<
11371137
escapeParameter,
11381138
inheritLocale,
11391139
__i18n,
1140-
__root,
1141-
__meta
1140+
__root
11421141
}
11431142
}
11441143

packages/vue-i18n/src/mixin.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { getCurrentInstance } from 'vue'
2-
import {
3-
getLocaleMessages,
4-
SetPluralRulesSymbol,
5-
DEVTOOLS_META,
6-
DevToolsMetaSymbol
7-
} from './composer'
2+
import { getLocaleMessages, SetPluralRulesSymbol } from './composer'
83
import { createVueI18n } from './legacy'
94
import { createI18nError, I18nErrorCodes } from './errors'
105
import { addTimelineEvent } from './devtools'
@@ -53,9 +48,6 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
5348
if (options.__i18n) {
5449
optionsI18n.__i18n = options.__i18n
5550
}
56-
optionsI18n.__meta = options[DEVTOOLS_META]
57-
? { [DEVTOOLS_META]: options[DEVTOOLS_META] }
58-
: {}
5951
optionsI18n.__root = composer
6052
if (this === this.$root) {
6153
this.$i18n = mergeToRoot(vuei18n, optionsI18n)
@@ -68,9 +60,6 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
6860
} else {
6961
this.$i18n = createVueI18n({
7062
__i18n: (options as ComposerInternalOptions<Messages>).__i18n,
71-
__meta: options[DEVTOOLS_META]
72-
? { [DEVTOOLS_META]: options[DEVTOOLS_META] }
73-
: {},
7463
__root: composer
7564
} as VueI18nOptions)
7665
}
@@ -182,16 +171,6 @@ function mergeToRoot<Messages, DateTimeFormats, NumberFormats>(
182171
;(root as any).__composer[SetPluralRulesSymbol](
183172
options.pluralizationRules || root.pluralizationRules
184173
)
185-
/* eslint-disable @typescript-eslint/no-explicit-any */
186-
if (
187-
(options as any)[DEVTOOLS_META] &&
188-
!(root as any).__composer[DevToolsMetaSymbol]
189-
) {
190-
;(root as any).__composer[DevToolsMetaSymbol] = {
191-
[DEVTOOLS_META]: (options as any)[DEVTOOLS_META]
192-
}
193-
}
194-
/* eslint-enable @typescript-eslint/no-explicit-any */
195174
const messages = getLocaleMessages<VueMessageType>(root.locale, {
196175
messages: options.messages,
197176
__i18n: options.__i18n

packages/vue-i18n/src/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
DateTimeOptions,
99
NumberOptions
1010
} from '@intlify/core'
11-
import type { CustomBlocks, DEVTOOLS_META, VueMessageType } from './composer'
11+
import type { CustomBlocks, VueMessageType } from './composer'
1212
import type {
1313
VueI18n,
1414
VueI18nOptions,

0 commit comments

Comments
 (0)