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

fix(runtime-core): pass options to plugins #561

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/runtime-core/__tests__/apiApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ describe('api: createApp', () => {
test('use', () => {
const PluginA: Plugin = app => app.provide('foo', 1)
const PluginB: Plugin = {
install: app => app.provide('bar', 2)
install: (app, arg1, arg2) => app.provide('bar', arg1 + arg2)
}
const PluginC: any = undefined

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

const Root = {
setup() {
Expand Down
10 changes: 5 additions & 5 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createVNode, cloneVNode } from './vnode'

export interface App<HostElement = any> {
config: AppConfig
use(plugin: Plugin, options?: any): this
use(plugin: Plugin, ...options: any[]): this
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component(name: string, component: Component): this
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface AppContext {
reload?: () => void // HMR only
}

type PluginInstallFunction = (app: App) => any
type PluginInstallFunction = (app: App, ...options: any[]) => any

export type Plugin =
| PluginInstallFunction
Expand Down Expand Up @@ -97,15 +97,15 @@ export function createAppAPI<HostNode, HostElement>(
}
},

use(plugin: Plugin) {
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)
plugin(app, ...options)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use arguments to avoid overhead of transpiled code?

} else if (plugin && isFunction(plugin.install)) {
installedPlugins.add(plugin)
plugin.install(app)
plugin.install(app, ...options)
} else if (__DEV__) {
warn(
`A plugin must either be a function or an object with an "install" ` +
Expand Down