Plugin Import Proposal
See original GitHub issuePlease feel free to comment on this proposal. Thanks!
@capacitor/core
should provide a registerPlugin
function that takes an object of implementations for each platform. For Android and iOS, the plugins proxy can be used. For web, the web implementation will need to be provided.
registerPlugin
will return a RegisteredPlugin
object with a getImplementation
method that returns the instance of the plugin for the current platform.
The “echo” example will be used for this proposal.
echo-plugin/index.ts
import { Plugins, registerPlugin } from '@capacitor/core';
import { EchoPluginWeb } from './web';
export interface EchoPlugin {
echo: (something: string) => string;
}
const Plugin = registerPlugin<EchoPlugin>('Echo', {
android: Plugins.EchoPlugin,
ios: Plugins.EchoPlugin,
web: new EchoPluginWeb(),
}).getImplementation();
export { Plugin };
echo-plugin/web.ts
import { WebPlugin } from '@capacitor/core';
import type { EchoPlugin } from '.';
export class EchoPluginWeb extends WebPlugin implements EchoPlugin {
constructor() {
super({ name: 'Echo' });
}
...
}
my-app/some-page.tsx
import { Echo } from 'echo-plugin';
await Echo.echo('hello world');
// TODO: Figure out testing.
export class Foo {
constructor() {
Echo.echo();
}
}
This shouldn’t be a breaking change because plugins will need to opt-in to using registerPlugin
in order for the new plugin import to work. (Previously, users of the plugin would have to import { Plugins } from '@capacitor/core';
).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5 (4 by maintainers)
Top GitHub Comments
Yeah, but not super important to the proposal, I’ll add
...
to make it clear there’s more implementation details.Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.