Skip to content

Commit 9f813ca

Browse files
authored
feat: support components maually instalation (#55)
* feat: support components maually instalation * fix lint error
1 parent d8bd252 commit 9f813ca

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ export {
4545
UseI18nOptions
4646
} from './i18n'
4747
export {
48+
Translation,
4849
TranslationProps,
50+
NumberFormat,
4951
NumberFormatProps,
52+
DatetimeFormat,
5053
DatetimeFormatProps,
5154
FormattableProps,
5255
BaseFormatProps,

src/plugin.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { I18nSymbol, I18n, I18nInternal } from './i18n'
33
import { Translation, NumberFormat, DatetimeFormat } from './components'
44
import { vTDirective } from './directive'
55
import { I18nWarnCodes, getWarnMessage } from './warnings'
6-
import { isPlainObject, warn } from './utils'
6+
import { isPlainObject, warn, isBoolean } from './utils'
77

88
/**
99
* I18n plugin options
@@ -13,6 +13,7 @@ import { isPlainObject, warn } from './utils'
1313
*/
1414
export interface I18nPluginOptions {
1515
useI18nComponentName?: boolean
16+
globalInstall?: boolean
1617
}
1718

1819
export function apply(
@@ -24,19 +25,27 @@ export function apply(
2425
? (options[0] as I18nPluginOptions)
2526
: {}
2627
const useI18nComponentName = !!pluginOptions.useI18nComponentName
28+
const globalInstall = isBoolean(pluginOptions.globalInstall)
29+
? pluginOptions.globalInstall
30+
: true
2731

28-
if (__DEV__ && useI18nComponentName) {
32+
if (__DEV__ && globalInstall && useI18nComponentName) {
2933
warn(
3034
getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
3135
name: Translation.name
3236
})
3337
)
3438
}
3539

36-
// install components
37-
app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation)
38-
app.component(NumberFormat.name, NumberFormat)
39-
app.component(DatetimeFormat.name, DatetimeFormat)
40+
if (globalInstall) {
41+
// install components
42+
app.component(
43+
!useI18nComponentName ? Translation.name : 'i18n',
44+
Translation
45+
)
46+
app.component(NumberFormat.name, NumberFormat)
47+
app.component(DatetimeFormat.name, DatetimeFormat)
48+
}
4049

4150
// install directive
4251
app.directive('t', vTDirective(i18n))

test/plugin.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,24 @@ describe('useI18nComponentName option', () => {
4444
})
4545
})
4646

47+
describe('globalInstall option', () => {
48+
test('default', () => {
49+
const app = createApp({})
50+
const i18n = {} as I18n & I18nInternal
51+
const spy = jest.spyOn(app, 'component')
52+
53+
apply(app, i18n)
54+
expect(spy).toHaveBeenCalledTimes(3)
55+
})
56+
57+
test('false', () => {
58+
const app = createApp({})
59+
const i18n = {} as I18n & I18nInternal
60+
const spy = jest.spyOn(app, 'component')
61+
62+
apply(app, i18n, { globalInstall: false })
63+
expect(spy).not.toHaveBeenCalled()
64+
})
65+
})
66+
4767
/* eslint-enable @typescript-eslint/no-empty-function */

0 commit comments

Comments
 (0)