Skip to content

Commit

Permalink
fix(runtime-core): allow classes to be passed as plugins (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD authored and yyx990803 committed Jan 8, 2020
1 parent 453e688 commit 8f616a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/runtime-core/__tests__/apiApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,18 @@ describe('api: createApp', () => {
const PluginB: Plugin = {
install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
}
const PluginC: any = undefined
class PluginC {
someProperty = {}
static install() {
app.provide('baz', 2)
}
}
const PluginD: any = undefined

const app = createApp()
app.use(PluginA)
app.use(PluginB, 1, 1)
app.use(PluginC)

const Root = {
setup() {
Expand All @@ -266,7 +273,7 @@ describe('api: createApp', () => {
`Plugin has already been applied to target app`
).toHaveBeenWarnedTimes(1)

app.use(PluginC)
app.use(PluginD)
expect(
`A plugin must either be a function or an object with an "install" ` +
`function.`
Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface AppContext {
type PluginInstallFunction = (app: App, ...options: any[]) => any

export type Plugin =
| PluginInstallFunction
| PluginInstallFunction & { install?: PluginInstallFunction }
| {
install: PluginInstallFunction
}
Expand Down Expand Up @@ -103,12 +103,12 @@ export function createAppAPI<HostNode, HostElement>(
use(plugin: Plugin, ...options: any[]) {
if (installedPlugins.has(plugin)) {
__DEV__ && warn(`Plugin has already been applied to target app.`)
} else if (isFunction(plugin)) {
installedPlugins.add(plugin)
plugin(app, ...options)
} else if (plugin && isFunction(plugin.install)) {
installedPlugins.add(plugin)
plugin.install(app, ...options)
} else if (isFunction(plugin)) {
installedPlugins.add(plugin)
plugin(app, ...options)
} else if (__DEV__) {
warn(
`A plugin must either be a function or an object with an "install" ` +
Expand Down

0 comments on commit 8f616a8

Please sign in to comment.