Skip to content

Commit 94d4d87

Browse files
authored
fix: prevent multiple plugins get installed (#427)
1 parent 4ebeda4 commit 94d4d87

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/install.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import type { VueConstructor } from 'vue'
22
import { AnyObject } from './types/basic'
33
import { hasSymbol, hasOwn, isPlainObject, assert } from './utils'
44
import { isRef } from './reactivity'
5-
import { setVueConstructor, isVueRegistered } from './runtimeContext'
5+
import {
6+
setVueConstructor,
7+
isVueRegistered,
8+
isPluginInstalled,
9+
} from './runtimeContext'
610
import { mixin } from './mixin'
711

812
/**
@@ -40,7 +44,7 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
4044
}
4145

4246
export function install(Vue: VueConstructor) {
43-
if (isVueRegistered()) {
47+
if (isPluginInstalled() || isVueRegistered(Vue)) {
4448
if (__DEV__) {
4549
assert(
4650
false,
@@ -52,10 +56,7 @@ export function install(Vue: VueConstructor) {
5256

5357
if (__DEV__) {
5458
if (!Vue.version.startsWith('2.')) {
55-
assert(
56-
false,
57-
`@vue/composition-api only works with Vue 2, v${Vue.version} found.`
58-
)
59+
assert(false, `only works with Vue 2, v${Vue.version} found.`)
5960
}
6061
}
6162

src/runtimeContext.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import { VueConstructor } from 'vue'
1+
import type { VueConstructor } from 'vue'
22
import { ComponentInstance } from './component'
3-
import { assert } from './utils'
3+
import { assert, hasOwn } from './utils'
44

55
let vueConstructor: VueConstructor | null = null
66
let currentInstance: ComponentInstance | null = null
77

8-
export function isVueRegistered() {
8+
const PluginInstalledFlag = '__composition_api_installed__'
9+
10+
export function isPluginInstalled() {
911
return !!vueConstructor
1012
}
1113

14+
export function isVueRegistered(Vue: VueConstructor) {
15+
return hasOwn(Vue, PluginInstalledFlag)
16+
}
17+
1218
export function getVueConstructor(): VueConstructor {
1319
if (__DEV__) {
1420
assert(
@@ -22,6 +28,11 @@ export function getVueConstructor(): VueConstructor {
2228

2329
export function setVueConstructor(Vue: VueConstructor) {
2430
vueConstructor = Vue
31+
Object.defineProperty(Vue, PluginInstalledFlag, {
32+
configurable: true,
33+
writable: true,
34+
value: true,
35+
})
2536
}
2637

2738
export function getCurrentInstance(): ComponentInstance | null {

0 commit comments

Comments
 (0)