From f98132d26bc5235c8a28e509f5d11dc9cc124eac Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 27 May 2020 01:38:04 +0900 Subject: [PATCH 1/2] feat: support components maually instalation --- src/index.ts | 3 +++ src/plugin.ts | 18 ++++++++++++------ test/plugin.test.ts | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 888cd14ef..25fe9ec0a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,8 +45,11 @@ export { UseI18nOptions } from './i18n' export { + Translation, TranslationProps, + NumberFormat, NumberFormatProps, + DatetimeFormat, DatetimeFormatProps, FormattableProps, BaseFormatProps, diff --git a/src/plugin.ts b/src/plugin.ts index 43780294b..6998888f6 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,7 +3,7 @@ import { I18nSymbol, I18n, I18nInternal } from './i18n' import { Translation, NumberFormat, DatetimeFormat } from './components' import { vTDirective } from './directive' import { I18nWarnCodes, getWarnMessage } from './warnings' -import { isPlainObject, warn } from './utils' +import { isPlainObject, warn, isBoolean } from './utils' /** * I18n plugin options @@ -13,6 +13,7 @@ import { isPlainObject, warn } from './utils' */ export interface I18nPluginOptions { useI18nComponentName?: boolean + globalInstall?: boolean } export function apply( @@ -24,8 +25,11 @@ export function apply( ? (options[0] as I18nPluginOptions) : {} const useI18nComponentName = !!pluginOptions.useI18nComponentName + const globalInstall = isBoolean(pluginOptions.globalInstall) + ? pluginOptions.globalInstall + : true - if (__DEV__ && useI18nComponentName) { + if (__DEV__ && globalInstall && useI18nComponentName) { warn( getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, { name: Translation.name @@ -33,10 +37,12 @@ export function apply( ) } - // install components - app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation) - app.component(NumberFormat.name, NumberFormat) - app.component(DatetimeFormat.name, DatetimeFormat) + if (globalInstall) { + // install components + app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation) + app.component(NumberFormat.name, NumberFormat) + app.component(DatetimeFormat.name, DatetimeFormat) + } // install directive app.directive('t', vTDirective(i18n)) diff --git a/test/plugin.test.ts b/test/plugin.test.ts index 24990bbf1..618522c1c 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -44,4 +44,24 @@ describe('useI18nComponentName option', () => { }) }) +describe('globalInstall option', () => { + test('default', () => { + const app = createApp({}) + const i18n = {} as I18n & I18nInternal + const spy = jest.spyOn(app, 'component') + + apply(app, i18n) + expect(spy).toHaveBeenCalledTimes(3) + }) + + test('false', () => { + const app = createApp({}) + const i18n = {} as I18n & I18nInternal + const spy = jest.spyOn(app, 'component') + + apply(app, i18n, { globalInstall: false }) + expect(spy).not.toHaveBeenCalled() + }) +}) + /* eslint-enable @typescript-eslint/no-empty-function */ From 9bcdf167f56e0f38c394d81518478ff0a7526a7e Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 27 May 2020 01:41:47 +0900 Subject: [PATCH 2/2] fix lint error --- src/plugin.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/plugin.ts b/src/plugin.ts index 6998888f6..dab77815a 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -39,7 +39,10 @@ export function apply( if (globalInstall) { // install components - app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation) + app.component( + !useI18nComponentName ? Translation.name : 'i18n', + Translation + ) app.component(NumberFormat.name, NumberFormat) app.component(DatetimeFormat.name, DatetimeFormat) }