Skip to content

Commit 5301d49

Browse files
authored
fix: change duplicate installation from error to warn, close #631 (#632)
1 parent 5e5001a commit 5301d49

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/install.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { VueConstructor } from 'vue'
22
import { AnyObject } from './types/basic'
3-
import { hasSymbol, hasOwn, isPlainObject, assert, warn } from './utils'
3+
import { hasSymbol, hasOwn, isPlainObject, warn } from './utils'
44
import { isRef } from './reactivity'
55
import { setVueConstructor, isVueRegistered } from './runtimeContext'
66
import { mixin } from './mixin'
@@ -42,9 +42,8 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
4242
export function install(Vue: VueConstructor) {
4343
if (isVueRegistered(Vue)) {
4444
if (__DEV__) {
45-
assert(
46-
false,
47-
'already installed. Vue.use(VueCompositionAPI) should be called only once.'
45+
warn(
46+
'[vue-composition-api] already installed. Vue.use(VueCompositionAPI) should be called only once.'
4847
)
4948
}
5049
return
@@ -53,10 +52,12 @@ export function install(Vue: VueConstructor) {
5352
if (__DEV__) {
5453
if (Vue.version) {
5554
if (Vue.version[0] !== '2' || Vue.version[1] !== '.') {
56-
assert(false, `only works with Vue 2, v${Vue.version} found.`)
55+
warn(
56+
`[vue-composition-api] only works with Vue 2, v${Vue.version} found.`
57+
)
5758
}
5859
} else {
59-
warn('Vue version not found')
60+
warn('[vue-composition-api] no Vue version found')
6061
}
6162
}
6263

src/runtimeContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function getRegisteredVueOrDefault(): VueConstructor {
6161
export function setVueConstructor(Vue: VueConstructor) {
6262
// @ts-ignore
6363
if (__DEV__ && vueConstructor && Vue.__proto__ !== vueConstructor.__proto__) {
64-
warn('Another instance of vue installed')
64+
warn('[vue-composition-api] another instance of Vue installed')
6565
}
6666
vueConstructor = Vue
6767
Object.defineProperty(Vue, PluginInstalledFlag, {

src/utils/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export function hasOwn(obj: Object, key: string): boolean {
4545
}
4646

4747
export function assert(condition: any, msg: string) {
48-
if (!condition) throw new Error(`[vue-composition-api] ${msg}`)
48+
if (!condition) {
49+
throw new Error(`[vue-composition-api] ${msg}`)
50+
}
4951
}
5052

5153
export function isPrimitive(value: any): boolean {

test/use.spec.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ describe('use', () => {
1313
const localVueTwo = createLocalVue()
1414
localVueTwo.use(CompositionApi)
1515

16-
expect('Another instance of vue installed').not.toHaveBeenWarned()
16+
expect(
17+
'[vue-composition-api] another instance of Vue installed'
18+
).not.toHaveBeenWarned()
1719
})
1820

1921
it('should warn install in multiple vue', () => {
@@ -28,26 +30,30 @@ describe('use', () => {
2830

2931
// @ts-ignore
3032
CompositionApi.install(fakeVue)
31-
expect('Another instance of vue installed').toHaveBeenWarned()
33+
expect(
34+
'[vue-composition-api] another instance of Vue installed'
35+
).toHaveBeenWarned()
3236
} finally {
3337
Vue.use(CompositionApi)
34-
expect('Another instance of vue installed').toHaveBeenWarned()
38+
expect(
39+
'[vue-composition-api] another instance of Vue installed'
40+
).toHaveBeenWarned()
3541
}
3642
})
3743

3844
it('should warn installing multiple times', () => {
3945
const localVueOne = createLocalVue()
4046
localVueOne.use(CompositionApi)
4147

42-
expect(() => {
43-
// vue prevents the same plugin of being installed, this will create a new plugin instance
44-
localVueOne.use({
45-
install(v) {
46-
CompositionApi.install(v)
47-
},
48-
})
49-
}).toThrowError(
50-
'already installed. Vue.use(VueCompositionAPI) should be called only once.'
51-
)
48+
// vue prevents the same plugin of being installed, this will create a new plugin instance
49+
localVueOne.use({
50+
install(v) {
51+
CompositionApi.install(v)
52+
},
53+
})
54+
55+
expect(
56+
'[vue-composition-api] already installed. Vue.use(VueCompositionAPI) should be called only once.'
57+
).toHaveBeenWarned()
5258
})
5359
})

0 commit comments

Comments
 (0)