feat: warn duplicate plugin installations

This commit is contained in:
Evan You 2019-11-05 10:49:00 -05:00
parent 2d5f9b58ac
commit c61e5463fa
2 changed files with 11 additions and 1 deletions

View File

@ -259,6 +259,11 @@ describe('api: createApp', () => {
const root = nodeOps.createElement('div')
app.mount(Root, root)
expect(serializeInner(root)).toBe(`1,2`)
app.use(PluginA)
expect(
`Plugin has already been applied to target app`
).toHaveBeenWarnedTimes(1)
})
test('config.errorHandler', () => {

View File

@ -79,6 +79,7 @@ export function createAppAPI<HostNode, HostElement>(
): () => App<HostElement> {
return function createApp(): App {
const context = createAppContext()
const installedPlugins = new Set()
let isMounted = false
@ -96,9 +97,13 @@ export function createAppAPI<HostNode, HostElement>(
},
use(plugin: Plugin) {
if (isFunction(plugin)) {
if (installedPlugins.has(plugin)) {
__DEV__ && warn(`Plugin has already been applied to target app.`)
} else if (isFunction(plugin)) {
installedPlugins.add(plugin)
plugin(app)
} else if (isFunction(plugin.install)) {
installedPlugins.add(plugin)
plugin.install(app)
} else if (__DEV__) {
warn(