To extend a Capacitor V3 compatible plugin with Electron support, follow these steps to set up the directory structure, build configuration, and registration:
1. Directory Structure
Create an electron folder in the root of your plugin, containing a src sub-folder. Place your implementation in electron/src/index.ts.
2. Configuration Files
Create the following files inside the electron folder:
.gitignore: Include dist..npmignore: Include src.rollup.config.js: Configures the build output to electron/dist/plugin.js in CommonJS format.tsconfig.json: Configures TypeScript compilation with outDir set to build.
3. Update Root package.json
Modify your plugin's root package.json with these changes:
capacitor object: Add the electron platform.files array: Include electron/ to ensure the build artifacts are published.scripts: Add a build-electron script and update the main build script to include the electron build step.
4. Register the Plugin
In your plugin's main entry point (<root>/src/index.ts), register the Electron implementation using CapacitorCustomPlatform:
const Dialog = registerPlugin<DialogPlugin>('Dialog', {
web: () => import('./web').then(m => new m.DialogWeb()),
electron: () => (window as any).CapacitorCustomPlatform.plugins.DialogElectron
});
5. Build and Sync
Run npm run build to compile the plugin. Once published, use npx cap sync (or copy/update) in your Capacitor app to include the new Electron platform support.
// Example package.json modifications
{
"capacitor": {
"ios": { "src": "ios" },
"android": { "src": "android" },
"electron": { "src": "electron" }
},
"files": [
"electron/"
],
"scripts": {
"build-electron": "tsc --project electron/tsconfig.json && rollup -c electron/rollup.config.js && rimraf ./electron/build",
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js && npm run build-electron"
}
}