Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow plugin to be installed in localVue #497

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import type { VueConstructor } from 'vue'
import { AnyObject } from './types/basic'
import { hasSymbol, hasOwn, isPlainObject, assert } from './utils'
import { isRef } from './reactivity'
import {
setVueConstructor,
isVueRegistered,
isPluginInstalled,
} from './runtimeContext'
import { setVueConstructor, isVueRegistered } from './runtimeContext'
import { mixin } from './mixin'

/**
Expand Down Expand Up @@ -44,7 +40,7 @@ function mergeData(from: AnyObject, to: AnyObject): Object {
}

export function install(Vue: VueConstructor) {
if (isPluginInstalled() || isVueRegistered(Vue)) {
if (isVueRegistered(Vue)) {
if (__DEV__) {
assert(
false,
Expand All @@ -55,7 +51,7 @@ export function install(Vue: VueConstructor) {
}

if (__DEV__) {
if (Vue.version[0] !== '2' || Vue.version[1] !== '.') {
if (Vue.version && (Vue.version[0] !== '2' || Vue.version[1] !== '.')) {
pikax marked this conversation as resolved.
Show resolved Hide resolved
assert(false, `only works with Vue 2, v${Vue.version} found.`)
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/runtimeContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { VueConstructor } from 'vue'
import { ComponentInstance } from './component'
import { assert, hasOwn } from './utils'
import { assert, hasOwn, warn } from './utils'

let vueConstructor: VueConstructor | null = null
let currentInstance: ComponentInstance | null = null
Expand All @@ -27,6 +27,12 @@ export function getVueConstructor(): VueConstructor {
}

export function setVueConstructor(Vue: VueConstructor) {
if (__DEV__ && vueConstructor) {
// don't warn in jest
if ('jest' in global) {
pikax marked this conversation as resolved.
Show resolved Hide resolved
warn('Another instance of vue installed')
}
}
vueConstructor = Vue
Object.defineProperty(Vue, PluginInstalledFlag, {
configurable: true,
Expand Down
28 changes: 28 additions & 0 deletions test/helpers/create-local-vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Vue, { VueConstructor } from 'vue'

// based on https://github.com/vuejs/vue-test-utils/blob/dev/packages/test-utils/src/create-local-vue.js

export function createLocalVue(_Vue: VueConstructor = Vue) {
const instance = _Vue.extend()

// @ts-ignore
if (instance._installedPlugins && instance._installedPlugins.length) {
// @ts-ignore
instance._installedPlugins.length = 0
}

instance.config = _Vue.config

const use = instance.use
//@ts-ignore
instance.use = (plugin, ...rest) => {
if (plugin.installed === true) {
plugin.installed = false
}
if (plugin.install && plugin.install.installed === true) {
plugin.install.installed = false
}
use.call(instance, plugin, ...rest)
}
return instance
}
47 changes: 47 additions & 0 deletions test/use.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import CompositionApi from '../src'
import { createLocalVue } from './helpers/create-local-vue'
import { mockWarn } from './helpers'

describe('use', () => {
mockWarn(true)

const __jest = global.jest

beforeEach(() => {
global.jest = __jest
})
afterEach(() => {
global.jest = __jest
})

it('should allow install in multiple vue', () => {
// @ts-ignore
global.jest = undefined
pikax marked this conversation as resolved.
Show resolved Hide resolved
const localVueOne = createLocalVue()
localVueOne.use(CompositionApi)

const localVueTwo = createLocalVue()
localVueTwo.use(CompositionApi)

expect('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() {
//@ts-ignore
CompositionApi.install(...arguments)
},
pikax marked this conversation as resolved.
Show resolved Hide resolved
})
}).toThrowError(
'already installed. Vue.use(VueCompositionAPI) should be called only once.'
)

expect('Another instance of vue installed').toHaveBeenWarned()
})
})