Inlined plugins
tauri version:2 I have a custom plugin(not a lib package,just a rs file with some commands), in the rs file,there is an init function like below:
// custom.rs
pub fn init() -> TauriPlugin<Wry> {
Builder::new("custom")
.invoke_handler(generate_handler![test_command])
.build()
}
and inner the main.rs
// main.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.plugin(tauri_plugin_http::init())
// custom plugin
.plugin(custom::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
but i can't invoke this command from frontend.
await invoke(plugin::custom|test_command);
Error from frontend:Unhandled Promise Rejection: custom:test_command not allowed. Plugin not found.
and i can't add this custom command to capability too.
In the previous version tauri V1, I could invoke custom command using this way. so,what i should do for this in v2?
@ErickWei Have you read this page?
I think, since it is just a rust file, and you've not "wrapped it" into a JS package, you have to invoke it like this:
import { invoke } from '@tauri-apps/api'
invoke('plugin:custom|test_command')
@rmcsharry yes i did it like your reply,It worked fine in V1, but it won't work when I upgrade to V2-rc
import { invoke } from '@tauri-apps/api'
invoke('plugin:custom|test_command')
@ErickWei Have you read this page?
I think, since it is just a rust file, and you've not "wrapped it" into a JS package, you have to invoke it like this:
import { invoke } from '@tauri-apps/api' invoke('plugin:custom|test_command')
@ErickWei Apologies, I gave you the link to v1 docs.
// custom_commands.rs
#[tauri::command]
pub fn test_command()
// do whatever
}
// lib.rs
mod custom_commands;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![custom_commands::test_command])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Thanks @rmcsharry i think ur solution need to create a lib project of tauri plugin. but i don't want to do it like that way. i read amount of document and example project...but nothing is helpful
@ErickWei Apologies, I gave you the link to v1 docs.
// custom_commands.rs #[tauri::command] pub fn test_command() // do whatever } // lib.rs mod custom_commands; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![custom_commands::test_command]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }
The plugin documentation is not complete about this, thanks for your patience as we finalize last few pieces of v2 and then documentation will be updated.
In v2, inlined plugins have to be declared in the build script like this https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/build.rs#L12-L15 and here is the plugin code https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/src/menu_plugin.rs then you will have to add the permission to your capability file
I will move the issue to tauri-docs as this is a documentation issue
So do I have to declared command to the build.rs file? Is there any other more convenient way?
The way like v1 do is very convenient and easier to manage our code.
The plugin documentation is not complete about this, thanks for your patience as we finalize last few pieces of v2 and then documentation will be updated.
In v2, inlined plugins have to be declared in the build script like this https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/build.rs#L12-L15 and here is the plugin code https://github.com/tauri-apps/tauri/blob/be18ed50d8e04261da1553662a768e7ce0f1dd8f/examples/api/src-tauri/src/menu_plugin.rs then you will have to add the permission to your capability file
and here is another report https://github.com/tauri-apps/tauri/issues/10075