The Plugin Ecosystem
The Plugin Ecosystem
Section titled “The Plugin Ecosystem”Tauri v2 embraces a modular architecture. To keep the core application lightweight and secure, many features that were previously built-in (or features you might expect from Electron) are now separated into plugins. This ensures you only bundle code for the features you actually use.
How Plugins Work
Section titled “How Plugins Work”Using a plugin generally involves three steps:
- Install: Add the Rust crate via
cargoand the JavaScript API via your package manager (npm/pnpm/yarn). - Register: Initialize the plugin in your Rust
lib.rsbuilder chain. - Usage: Import the JavaScript API in your frontend to use the functionality.
Persistent Settings: Store
Section titled “Persistent Settings: Store”The plugin-store allows you to save simple key-value data, like user preferences or application state, to a file on disk. It handles serialization automatically.
import { Store } from '@tauri-apps/plugin-store';
const store = new Store('settings.json');
// Set a valueawait store.set('theme', 'dark');
// Save changes to diskawait store.save();
// Retrieve a valueconst val = await store.get('theme');File System Access: FS
Section titled “File System Access: FS”The plugin-fs plugin provides access to the file system. Unlike standard browser APIs, this allows you to read and write files anywhere the OS permissions allow (scoped for security).
import { readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
// Write to the App Data directoryawait writeTextFile('app.conf', 'config_data', { baseDir: BaseDirectory.AppData});
// Read the file backconst content = await readTextFile('app.conf', { baseDir: BaseDirectory.AppData});Networking: HTTP
Section titled “Networking: HTTP”While fetch works in the browser, it is restricted by CORS (Cross-Origin Resource Sharing). The plugin-http allows your Rust backend to perform the request, bypassing CORS issues entirely, which is essential for calling third-party APIs.
Local Databases: SQL
Section titled “Local Databases: SQL”For more complex data needs, plugin-sql provides a connection to SQLite, MySQL, or PostgreSQL. SQLite is particularly popular for local desktop apps as it requires no server setup.
import Database from '@tauri-apps/plugin-sql';
const db = await Database.load('sqlite:test.db');
await db.execute('INSERT INTO users (name) VALUES ($1)', ['Alice']);Plugins are the building blocks of a rich Tauri application, allowing you to extend functionality without bloating your application core.