Skip to content

Commit 89fd11c

Browse files
authored
feat(reactive): allow usage of reactive before Vue.use (#515)
1 parent 2275bf9 commit 89fd11c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/reactivity/reactive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AnyObject } from '../types/basic'
2-
import { getVueConstructor } from '../runtimeContext'
2+
import { getRegisteredVueOrDefault } from '../runtimeContext'
33
import { isPlainObject, def, warn } from '../utils'
44
import { isComponentInstance, defineComponentInstance } from '../utils/helper'
55
import { RefKey } from '../utils/symbols'
@@ -94,7 +94,7 @@ export function defineAccessControl(target: AnyObject, key: any, val?: any) {
9494
}
9595

9696
function observe<T>(obj: T): T {
97-
const Vue = getVueConstructor()
97+
const Vue = getRegisteredVueOrDefault()
9898
let observed: T
9999
if (Vue.observable) {
100100
observed = Vue.observable(obj)

src/runtimeContext.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import type { VueConstructor } from 'vue'
22
import { ComponentInstance } from './component'
33
import { assert, hasOwn, warn } from './utils'
44

5+
let vueDependency: VueConstructor | undefined = undefined
6+
7+
try {
8+
vueDependency = require('vue')
9+
} catch {
10+
// not available
11+
}
12+
513
let vueConstructor: VueConstructor | null = null
614
let currentInstance: ComponentInstance | null = null
715

@@ -26,6 +34,17 @@ export function getVueConstructor(): VueConstructor {
2634
return vueConstructor!
2735
}
2836

37+
// returns registered vue or `vue` dependency
38+
export function getRegisteredVueOrDefault(): VueConstructor {
39+
let constructor = vueConstructor || vueDependency
40+
41+
if (__DEV__) {
42+
assert(vueConstructor, `No vue dependency found.`)
43+
}
44+
45+
return constructor!
46+
}
47+
2948
export function setVueConstructor(Vue: VueConstructor) {
3049
if (__DEV__ && vueConstructor) {
3150
warn('Another instance of vue installed')

0 commit comments

Comments
 (0)