Skip to content

Commit 671d2f6

Browse files
authored
fix: regression local composer extending (#45)
1 parent cb1cea9 commit 671d2f6

File tree

6 files changed

+79
-27
lines changed

6 files changed

+79
-27
lines changed

packages/vue-i18n-routing/scripts/vitest.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import type { InjectionKey, Ref } from 'vue-demi'
66

77
type InstanceType<V> = V extends { new (...arg: any[]): infer X } ? X : never
88
type VM<V> = InstanceType<V> & { unmount(): void }
9+
type Plugins = [...any]
910

10-
export function mount<V>(Comp: V, plugins: any[] = []) {
11+
export function mount<V>(Comp: V, plugins: Plugins[] = []) {
1112
const el = document.createElement('div')
1213
const app = createApp(Comp as any)
1314

1415
for (const plugin of plugins) {
15-
app.use(plugin)
16+
app.use(plugin[0], plugin[1])
1617
}
1718

1819
// @ts-ignore
@@ -22,7 +23,7 @@ export function mount<V>(Comp: V, plugins: any[] = []) {
2223
return comp
2324
}
2425

25-
export function useSetup<V>(setup: () => V, plugins: any[] = []) {
26+
export function useSetup<V>(setup: () => V, plugins: Plugins[] = []) {
2627
const Comp = defineComponent({
2728
setup,
2829
render() {

packages/vue-i18n-routing/src/__test__/compatibles.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('getRouteBaseName', () => {
3030
history: createMemoryHistory()
3131
})
3232
await router.push('/en')
33-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
33+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
3434
const name = vm.getRouteBaseName()
3535
assert.equal(name, 'home')
3636
})
@@ -54,7 +54,7 @@ describe('getRouteBaseName', () => {
5454
history: createMemoryHistory()
5555
})
5656
await router.push('/en')
57-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
57+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
5858
const name = vm.getRouteBaseName()
5959
assert.equal(name, 'home')
6060
})
@@ -81,7 +81,7 @@ describe('localePath', () => {
8181
history: createMemoryHistory()
8282
})
8383
await router.push('/en')
84-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
84+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
8585

8686
// path
8787
assert.equal(vm.localePath('/'), '/en')
@@ -136,7 +136,7 @@ describe('localePath', () => {
136136
history: createMemoryHistory()
137137
})
138138
await router.push('/')
139-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
139+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
140140

141141
// path
142142
assert.equal(vm.localePath('/'), '/')
@@ -189,7 +189,7 @@ describe('localeRoute', () => {
189189
history: createMemoryHistory()
190190
})
191191
await router.push('/en')
192-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
192+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
193193

194194
// path
195195
assert.include(vm.localeRoute('/'), {
@@ -286,7 +286,7 @@ describe('localeLocation', () => {
286286
history: createMemoryHistory()
287287
})
288288
await router.push('/en')
289-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
289+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
290290

291291
// path
292292
assert.include(vm.localeLocation('/'), {
@@ -408,7 +408,7 @@ describe('switchLocalePath', () => {
408408
history: createMemoryHistory()
409409
})
410410
await router.push('/en/about')
411-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
411+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
412412
await router.push('/ja')
413413

414414
assert.equal(vm.switchLocalePath('ja'), '/ja')
@@ -482,7 +482,7 @@ describe('localeHead', () => {
482482
history: createMemoryHistory()
483483
})
484484
await router.push('/en/about')
485-
const vm = useSetup(() => {}, [router, i18n]) as any // FIXME:
485+
const vm = useSetup(() => {}, [[router], [i18n]]) as any // FIXME:
486486

487487
let head = vm.localeHead({ addDirAttribute: true, addSeoAttributes: true })
488488
expect(head).toMatchSnapshot('en')

packages/vue-i18n-routing/src/composables/__test__/head.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('useLocaleHead', () => {
4444
i18n,
4545
head
4646
}
47-
}, [router, i18n])
47+
}, [[router], [i18n]])
4848

4949
await router.push('/ja')
5050
expect(vm.head).toMatchSnapshot(vm.i18n.locale.value)
@@ -87,7 +87,7 @@ describe('useLocaleHead', () => {
8787
i18n,
8888
head
8989
}
90-
}, [router, i18n])
90+
}, [[router], [i18n]])
9191

9292
await router.push('/ja')
9393
for (const m of vm.head.meta || []) {

packages/vue-i18n-routing/src/composables/__test__/routing.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('useRouteBaseName', () => {
3333
const getRouteBaseName = useRouteBaseName()
3434
const name = getRouteBaseName(route)
3535
assert.equal(name, 'home')
36-
}, [router, i18n])
36+
}, [[router], [i18n]])
3737
})
3838
})
3939

@@ -59,7 +59,7 @@ describe('useRouteBaseName', () => {
5959
const getRouteBaseName = useRouteBaseName()
6060
const name = getRouteBaseName({} as Route)
6161
assert.equal(name, null)
62-
}, [router, i18n])
62+
}, [[router], [i18n]])
6363
})
6464
})
6565

@@ -87,7 +87,7 @@ describe('useRouteBaseName', () => {
8787
const getRouteBaseName = useRouteBaseName({ route, routesNameSeparator: '---' })
8888
const name = getRouteBaseName()
8989
assert.equal(name, 'home')
90-
}, [router, i18n])
90+
}, [[router], [i18n]])
9191
})
9292
})
9393
})
@@ -122,7 +122,7 @@ describe('useLocalePath', () => {
122122
assert.equal(localePath('not-found'), '/en')
123123
// object
124124
assert.equal(localePath({ name: 'about' }, 'ja'), '/ja/about')
125-
}, [router, i18n])
125+
}, [[router], [i18n]])
126126
})
127127
})
128128
})
@@ -155,7 +155,7 @@ describe('useLocalePath', () => {
155155
assert.equal(localePath('not-found'), '/')
156156
// object
157157
assert.equal(localePath({ name: 'about' }, 'ja'), '/about')
158-
}, [router, i18n])
158+
}, [[router], [i18n]])
159159
})
160160
})
161161
})
@@ -229,7 +229,7 @@ describe('useLocaleRoute', () => {
229229
name: 'about___ja',
230230
href: '/ja/about'
231231
})
232-
}, [router, i18n])
232+
}, [[router], [i18n]])
233233
})
234234
})
235235

@@ -302,7 +302,7 @@ describe('useLocaleLocation', () => {
302302
name: 'about___ja',
303303
href: '/ja/about'
304304
})
305-
}, [router, i18n])
305+
}, [[router], [i18n]])
306306
})
307307
})
308308

@@ -333,7 +333,7 @@ describe('useSwitchLocalePath', () => {
333333
return {
334334
switchLocalePath: change
335335
}
336-
}, [router, i18n])
336+
}, [[router], [i18n]])
337337

338338
await router.push('/ja')
339339
assert.equal(vm.switchLocalePath('ja'), '/ja')

packages/vue-i18n-routing/src/extends/__test__/i18n.test.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3-
import { createI18n } from '@intlify/vue-i18n-bridge'
3+
import { createI18n, useI18n } from '@intlify/vue-i18n-bridge'
44
import { vi, describe, it, assert, expect } from 'vitest'
55
import { ref, nextTick } from 'vue-demi'
66

@@ -19,7 +19,7 @@ describe('extendI18n', () => {
1919
localeCodes: ['en', 'ja']
2020
})
2121

22-
const vm = useSetup(() => {}, [i18n])
22+
const vm = useSetup(() => {}, [[i18n]])
2323
const composer = i18n.global as unknown as Composer
2424
assert.deepEqual(composer.locales.value, [{ code: 'en' }, { code: 'ja' }])
2525
assert.deepEqual(composer.localeCodes.value, ['en', 'ja'])
@@ -36,7 +36,7 @@ describe('extendI18n', () => {
3636
localeCodes: ['en', 'ja']
3737
})
3838

39-
const vm = useSetup(() => {}, [i18n])
39+
const vm = useSetup(() => {}, [[i18n]])
4040
const vueI18n = i18n.global as unknown as VueI18n
4141
assert.deepEqual(vueI18n.locales, [{ code: 'en' }, { code: 'ja' }])
4242
assert.deepEqual(vueI18n.localeCodes, ['en', 'ja'])
@@ -69,7 +69,7 @@ describe('extendI18n', () => {
6969
}
7070
}
7171
})
72-
const vm = useSetup(() => {}, [i18n])
72+
const vm = useSetup(() => {}, [[i18n]])
7373
const $i18n = (vm as any).$i18n
7474
const composer = i18n.global as unknown as Composer
7575

@@ -119,7 +119,7 @@ describe('extendI18n', () => {
119119
}
120120
}
121121
})
122-
const vm = useSetup(() => {}, [i18n])
122+
const vm = useSetup(() => {}, [[i18n]])
123123
const $i18n = (vm as any).$i18n
124124
const vueI18n = i18n.global as unknown as VueI18n
125125

@@ -134,6 +134,57 @@ describe('extendI18n', () => {
134134
})
135135
})
136136
})
137+
138+
describe('__composerExtend include in plugin options', () => {
139+
test('should be extended', async () => {
140+
const extendFn = vi.fn()
141+
const disposeFn = vi.fn()
142+
143+
const i18n = createI18n({ legacy: false, globalInjection: true, locale: 'en' })
144+
extendI18n(i18n, {
145+
locales: [{ code: 'en' }, { code: 'ja' }],
146+
localeCodes: ['en', 'ja'],
147+
hooks: {
148+
onExtendComposer(composer: Composer) {
149+
;(composer as any).fn = extendFn
150+
},
151+
onExtendExportedGlobal(g) {
152+
return {
153+
fn: {
154+
get() {
155+
return (g as any).fn
156+
}
157+
}
158+
}
159+
}
160+
}
161+
})
162+
const pluginOptions = {
163+
__composerExtend: (c: Composer) => {
164+
;(c as any).fn = (i18n.global as any).fn
165+
return disposeFn
166+
}
167+
}
168+
const vm = useSetup(() => {
169+
const i18n = useI18n({
170+
useScope: 'local'
171+
})
172+
;(i18n as any).fn()
173+
return {}
174+
}, [[i18n, pluginOptions]])
175+
const $i18n = (vm as any).$i18n
176+
const composer = i18n.global as any
177+
178+
$i18n.fn()
179+
composer.fn()
180+
181+
await nextTick()
182+
expect(extendFn).toHaveBeenCalledTimes(3)
183+
184+
vm.unmount()
185+
expect(disposeFn).toBeCalledTimes(1)
186+
})
187+
})
137188
})
138189

139190
/* eslint-enable @typescript-eslint/no-explicit-any */

packages/vue-i18n-routing/src/extends/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
110110
localComposer.baseUrl = computed(() => globalComposer.baseUrl.value)
111111
let orgComposerDispose: Disposer | undefined
112112
if (isFunction(orgComposerExtend)) {
113-
orgComposerDispose = Reflect.apply(orgComposerExtend, pluginOptions, [globalComposer])
113+
orgComposerDispose = Reflect.apply(orgComposerExtend, pluginOptions, [localComposer])
114114
}
115115
return () => {
116116
orgComposerDispose && orgComposerDispose()

0 commit comments

Comments
 (0)