Custom plugin 'No web implementation' unless registerWebPlugin manually called in/after app constructor #1985
-
I'm trying to develop a custom plugin that makes full use of the https://capacitor.ionicframework.com/docs/getting-started/ I modify my starter project to install the plugin node-module, and I import it in When running natively, the plugin runs, but when running on web, I'm told there's no web implementation. If I make a call to I can see that Here are repositories for the app and plugin that manifest the issue. App: https://github.com/Cloov/capacitor-plugin-test.git 'Concatenate' is my sample plugin; it has one method, that combines two input strings. Currently, the extra |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Same issue here. No web implementation. |
Beta Was this translation helpful? Give feedback.
-
I think I found something interesting. It seems it has something to do with tree shaking? See this example using the standard Echo plugin generated by capacitor cli: import { Plugins } from '@capacitor/core';
import { Echo } from 'echo';
// reference import to prevent tree shaking (?), if this is removed, calls to Plugins.Echo no longer works
if (Echo) {}
// Web implementation available as long as Echo is imported AND referenced in code
console.log(Plugins.Echo.echo({ value: 'Ping' }));
// Also, For some reason typings are not available on Plugins.Echo
console.log(Plugins.Echo.thisMethodDoesNotExist());
// Typings work direcly on import
console.log(Echo.thisMethodDoesNotExist()); Screenshot to illustrate typing errors: Can we get some attention to this ticket, pretty please? :D @jcesarmobile @mlynch |
Beta Was this translation helpful? Give feedback.
-
Any news on that? I've the same issue here. |
Beta Was this translation helpful? Give feedback.
-
I ended up creating a custom api that imports and typecasts the plugin to work around the issue for now. import { MusicPlayerPlugin } from './definitions';
const MusicPlayer = Plugins.MusicPlayer as MusicPlayerPlugin;
class MusicPlayerRx {
public async play(): Promise<void> {
return MusicPlayer.play();
}
} Then the developer can import the custom API class and have full typings. import { Injectable } from '@angular/core';
import { MusicPlayerRx } from '@biesbjerg/capacitor-music-player';
@Injectable()
export class PlayerService {
public constructor(protected musicPlayer: MusicPlayerRx) {
musicPlayer.setupPlayer();
}
} It sucks having to proxy methods to the real plugin (if that is all you do with the custom API), but it's a workaround for now. |
Beta Was this translation helpful? Give feedback.
-
I'll have a look. Thanks @biesbjerg ! |
Beta Was this translation helpful? Give feedback.
-
You can also just import your plugin inside your app. import 'my-awesome-plugin';
import { Plugins } from '@capacitor/core';
function SomewhereInYourApp()
{
const {MyAwesomePlugin} = Plugins;
MyAwesomePlugin.doStuff();
} If you don't import your Plugin, Edit: Removed my old answer to avoid confusion. |
Beta Was this translation helpful? Give feedback.
You can also just import your plugin inside your app.
If you don't import your Plugin,
registerWebPlugin
won't get called, so the registry doesn't know there is a web implementation.The reason you native implementations work, is because they get picked up by capacitors auto-binding.
Edit:
Removed my old answer to avoid confusion.