Skip to content

Commit 8f616a8

Browse files
KaelWDyyx990803
authored andcommitted
fix(runtime-core): allow classes to be passed as plugins (#588)
1 parent 453e688 commit 8f616a8

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

packages/runtime-core/__tests__/apiApp.spec.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,18 @@ describe('api: createApp', () => {
244244
const PluginB: Plugin = {
245245
install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
246246
}
247-
const PluginC: any = undefined
247+
class PluginC {
248+
someProperty = {}
249+
static install() {
250+
app.provide('baz', 2)
251+
}
252+
}
253+
const PluginD: any = undefined
248254

249255
const app = createApp()
250256
app.use(PluginA)
251257
app.use(PluginB, 1, 1)
258+
app.use(PluginC)
252259

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

269-
app.use(PluginC)
276+
app.use(PluginD)
270277
expect(
271278
`A plugin must either be a function or an object with an "install" ` +
272279
`function.`

packages/runtime-core/src/apiCreateApp.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface AppContext {
5656
type PluginInstallFunction = (app: App, ...options: any[]) => any
5757

5858
export type Plugin =
59-
| PluginInstallFunction
59+
| PluginInstallFunction & { install?: PluginInstallFunction }
6060
| {
6161
install: PluginInstallFunction
6262
}
@@ -103,12 +103,12 @@ export function createAppAPI<HostNode, HostElement>(
103103
use(plugin: Plugin, ...options: any[]) {
104104
if (installedPlugins.has(plugin)) {
105105
__DEV__ && warn(`Plugin has already been applied to target app.`)
106-
} else if (isFunction(plugin)) {
107-
installedPlugins.add(plugin)
108-
plugin(app, ...options)
109106
} else if (plugin && isFunction(plugin.install)) {
110107
installedPlugins.add(plugin)
111108
plugin.install(app, ...options)
109+
} else if (isFunction(plugin)) {
110+
installedPlugins.add(plugin)
111+
plugin(app, ...options)
112112
} else if (__DEV__) {
113113
warn(
114114
`A plugin must either be a function or an object with an "install" ` +

0 commit comments

Comments
 (0)