Skip to content

Commit

Permalink
fix: change duplicate installation from error to warn, close #631 (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jan 14, 2021
1 parent 5e5001a commit 5301d49
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
13 changes: 7 additions & 6 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { VueConstructor } from 'vue'
import { AnyObject } from './types/basic'
import { hasSymbol, hasOwn, isPlainObject, assert, warn } from './utils'
import { hasSymbol, hasOwn, isPlainObject, warn } from './utils'
import { isRef } from './reactivity'
import { setVueConstructor, isVueRegistered } from './runtimeContext'
import { mixin } from './mixin'
Expand Down Expand Up @@ -42,9 +42,8 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
export function install(Vue: VueConstructor) {
if (isVueRegistered(Vue)) {
if (__DEV__) {
assert(
false,
'already installed. Vue.use(VueCompositionAPI) should be called only once.'
warn(
'[vue-composition-api] already installed. Vue.use(VueCompositionAPI) should be called only once.'
)
}
return
Expand All @@ -53,10 +52,12 @@ export function install(Vue: VueConstructor) {
if (__DEV__) {
if (Vue.version) {
if (Vue.version[0] !== '2' || Vue.version[1] !== '.') {
assert(false, `only works with Vue 2, v${Vue.version} found.`)
warn(
`[vue-composition-api] only works with Vue 2, v${Vue.version} found.`
)
}
} else {
warn('Vue version not found')
warn('[vue-composition-api] no Vue version found')
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function getRegisteredVueOrDefault(): VueConstructor {
export function setVueConstructor(Vue: VueConstructor) {
// @ts-ignore
if (__DEV__ && vueConstructor && Vue.__proto__ !== vueConstructor.__proto__) {
warn('Another instance of vue installed')
warn('[vue-composition-api] another instance of Vue installed')
}
vueConstructor = Vue
Object.defineProperty(Vue, PluginInstalledFlag, {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function hasOwn(obj: Object, key: string): boolean {
}

export function assert(condition: any, msg: string) {
if (!condition) throw new Error(`[vue-composition-api] ${msg}`)
if (!condition) {
throw new Error(`[vue-composition-api] ${msg}`)
}
}

export function isPrimitive(value: any): boolean {
Expand Down
32 changes: 19 additions & 13 deletions test/use.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ describe('use', () => {
const localVueTwo = createLocalVue()
localVueTwo.use(CompositionApi)

expect('Another instance of vue installed').not.toHaveBeenWarned()
expect(
'[vue-composition-api] another instance of Vue installed'
).not.toHaveBeenWarned()
})

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

// @ts-ignore
CompositionApi.install(fakeVue)
expect('Another instance of vue installed').toHaveBeenWarned()
expect(
'[vue-composition-api] another instance of Vue installed'
).toHaveBeenWarned()
} finally {
Vue.use(CompositionApi)
expect('Another instance of vue installed').toHaveBeenWarned()
expect(
'[vue-composition-api] another instance of Vue installed'
).toHaveBeenWarned()
}
})

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

expect(() => {
// vue prevents the same plugin of being installed, this will create a new plugin instance
localVueOne.use({
install(v) {
CompositionApi.install(v)
},
})
}).toThrowError(
'already installed. Vue.use(VueCompositionAPI) should be called only once.'
)
// vue prevents the same plugin of being installed, this will create a new plugin instance
localVueOne.use({
install(v) {
CompositionApi.install(v)
},
})

expect(
'[vue-composition-api] already installed. Vue.use(VueCompositionAPI) should be called only once.'
).toHaveBeenWarned()
})
})

0 comments on commit 5301d49

Please sign in to comment.