Sistema de Plugins
Sistema de Plugins
Tauri mantiene el core minimo. Las funcionalidades adicionales se agregan mediante plugins.
Plugins oficiales
Comunicacion y red
| Plugin | Paquete | Funcion |
|---|---|---|
| HTTP Client | @tauri-apps/plugin-http | Peticiones HTTP |
| WebSocket | @tauri-apps/plugin-websocket | Conexiones WebSocket |
| Shell | @tauri-apps/plugin-shell | Ejecutar comandos del SO |
Almacenamiento
| Plugin | Paquete | Funcion |
|---|---|---|
| Store | @tauri-apps/plugin-store | Key-value persistente |
| SQL | @tauri-apps/plugin-sql | SQLite, MySQL, PostgreSQL |
| File System | @tauri-apps/plugin-fs | Leer/escribir archivos |
UI/UX
| Plugin | Paquete | Funcion |
|---|---|---|
| Dialog | @tauri-apps/plugin-dialog | Dialogos nativos (abrir, guardar) |
| Clipboard | @tauri-apps/plugin-clipboard-manager | Copiar/pegar |
| Notification | @tauri-apps/plugin-notification | Notificaciones del SO |
Utilidades
| Plugin | Paquete | Funcion |
|---|---|---|
| Updater | @tauri-apps/plugin-updater | Actualizaciones automaticas |
| Deep Link | @tauri-apps/plugin-deep-link | URL schemes custom |
| CLI | @tauri-apps/plugin-cli | Argumentos de linea de comandos |
| Process | @tauri-apps/plugin-process | Info y control del proceso |
| Log | @tauri-apps/plugin-log | Logging estructurado |
Instalar un plugin
Dos pasos: dependencia Rust + paquete JavaScript.
# 1. Agregar crate Rust
cd src-tauri
cargo add tauri-plugin-store
# 2. Agregar paquete JS
npm install @tauri-apps/plugin-store
Registrar en src-tauri/src/lib.rs:
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::new().build())
.run(tauri::generate_context!())
.expect("error");
}
Usar desde JavaScript:
import { Store } from '@tauri-apps/plugin-store';
const store = await Store.load('settings.json');
await store.set('theme', 'dark');
const theme = await store.get('theme');
Permisos de plugins
Los comandos de plugins estan deshabilitados por defecto. Debes habilitar permisos en src-tauri/capabilities/default.json:
{
"identifier": "default",
"windows": ["main"],
"permissions": [
"core:default",
"store:allow-get",
"store:allow-set",
"fs:allow-read-text-file",
"dialog:allow-open"
]
}
Configuracion de plugins
Algunos plugins aceptan configuracion en tauri.conf.json:
{
"plugins": {
"http": {
"timeout": 30
},
"log": {
"level": "info"
}
}
}
Crear un plugin custom
Scaffold
npx @tauri-apps/cli plugin new mi-plugin
Opciones:
--no-api: sin bindings JavaScript--android: soporte Android--ios: soporte iOS
Estructura generada
tauri-plugin-mi-plugin/
├── src/
│ ├── lib.rs # Entry point del plugin
│ ├── commands.rs # Comandos expuestos
│ ├── desktop.rs # Logica desktop
│ └── mobile.rs # Logica movil
├── permissions/ # Permisos del plugin
├── guest-js/ # API JavaScript
├── build.rs # Autogeneracion de permisos
└── Cargo.toml
Definir comandos
// src/commands.rs
use tauri::command;
#[command]
pub async fn process_data(input: String) -> Result<String, String> {
Ok(format!("Procesado: {}", input))
}
Registrar permisos
En build.rs:
const COMMANDS: &[&str] = &["process_data"];
fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
}
Esto autogenera allow-process-data y deny-process-data.
Ciclo de vida
Los plugins pueden interceptar eventos:
- setup: inicializacion
- on_navigation: validar navegacion del webview
- on_webview_ready: nueva ventana creada
- on_event: eventos del event loop
- on_drop: destruccion del plugin
Resumen
- El core es minimo — funcionalidades via plugins
- 30+ plugins oficiales para HTTP, filesystem, dialogs, updater, etc.
- Instalar = crate Rust + paquete npm + registrar en builder
- Los comandos de plugins requieren permisos explicitos en capabilities
npx @tauri-apps/cli plugin newpara crear plugins custom- Los permisos se autogeneran desde
build.rs
← Gestion de Estado | Indice | Siguiente: Seguridad y Permisos →