Electron
Add Webview Bundle to an existing Electron app. First, load your frontend development server, then run a builtin bundle in the same window. Finally, configure packaging with Forge and electron-builder.
Before you begin
Use Electron 15 or later and Node.js 20.19 or later in the 20.x line, or
Node.js 22.12 or later. Start from an Electron project that can build and run
its main process. Put the frontend in web and its static build output in
web/dist. Run commands from the Electron project root.
This guide uses the following file layout:
1. Install dependencies
Add the runtime package:
npm install --save-dev @wvb/electron2. Create an instance and connect the development server
Create the instance in your main-process entry point before Electron becomes
ready. Register proxyProtocol for the app scheme, then open the window
after ready() completes:
import { BrowserWindow } from 'electron';
import { proxyProtocol, webviewBundle } from '@wvb/electron';
const wvb = webviewBundle({
protocols: [
proxyProtocol('app', {
hosts: { 'app.wvb': 'http://localhost:5173' },
}),
],
});
wvb.ready().then(async () => {
const win = new BrowserWindow({
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
},
});
await win.loadURL('app://app.wvb/');
});Start the server at http://localhost:5173 with your frontend's development
command, then run the Electron app. Requests to app://app.wvb/ are forwarded
to that server. The app.wvb key in hosts must match the full hostname of
the request.
HMR connections with proxyProtocol
proxyProtocol forwards HTTP requests from a custom scheme. When using a
development server with HMR, the client may fail to connect to its WebSocket
server. In that case, explicitly configure the development server's WebSocket
address so the client can connect.
For Vite, use the following configuration:
import { defineConfig } from 'vite';
export default defineConfig({
server: {
port: 5173,
strictPort: true,
ws: {
protocol: 'ws',
host: 'localhost',
clientPort: 5173,
},
},
});3. Install the CLI
Add the build-time CLI at the project root:
yarn add -D @wvb/cli4. Install the local build as a builtin bundle
Configure the web workspace to package its dist directory as bundle app,
version 1.0.0:
import { defineConfig } from '@wvb/cli';
export default defineConfig({
pack: {
srcDir: './dist',
},
remote: {
bundleName: 'app',
version: '1.0.0',
},
});At the native project root, point builtin.target at the web workspace:
import { defineConfig } from '@wvb/cli';
export default defineConfig({
builtin: {
target: {
type: 'local',
workspaces: ['./web'],
},
},
});Run your existing frontend build command. Once web/dist/index.html exists,
run the following command from the native project root:
yarn wvb builtinThe CLI packages the web build output and creates
./.wvb/builtin/bundles/manifest.json and
./.wvb/builtin/bundles/app/1.0.0.wvb.
5. Run and verify the builtin bundle
In src/main.ts, replace the proxyProtocol import with bundleProtocol
and update the protocols array:
import { BrowserWindow } from 'electron';
import { proxyProtocol, webviewBundle } from '@wvb/electron';
import { bundleProtocol, webviewBundle } from '@wvb/electron';
const wvb = webviewBundle({
protocols: [
proxyProtocol('app', {
hosts: { 'app.wvb': 'http://localhost:5173' },
}),
bundleProtocol('app'),
],
});
wvb.ready().then(async () => {
const win = new BrowserWindow({
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
},
});
await win.loadURL('app://app.wvb/');
});Stop the frontend server and restart Electron. The same page should appear
at app://app.wvb/.
6. Package the app
Follow these steps to package the app with its builtin bundles.
`node_modules` must exist on disk when packaging
Electron packaging scans the project's node_modules directory to collect
dependencies to include in the app. The module resolution algorithm used for
this process does not support symlinked dependencies or Yarn Plug'n'Play (PnP).
- For Yarn 2 or later, set
nodeLinker: node-modulesin.yarnrc.yml. - For pnpm, add
node-linker=hoistedto.npmrc.
Install @electron/packager
Install the packaging dependency:
npm install --save-dev @electron/packagerWrite a packaging script
Use the @electron/packager API to write a script that packages the Electron
app. Run it with node package.mts:
import { packager } from '@electron/packager';
const electronPkg = await import('electron/package.json', {
with: {
type: 'json',
},
});
const [appDir] = await packager({
dir: '.',
out: './out',
overwrite: true,
// import only 'dist', 'node_modules', and 'package.json'
ignore: [/^\/(?!(?:dist|node_modules)(?:\/|$)|package\.json$)/],
electronVersion: '45.0.0',
name: 'MyWebviewBundleApplication',
asar: {
unpack: `**/*.${process.platform}-*.node`,
},
extraResource: ['./.wvb/builtin/bundles'],
});
console.log(appDir);Create a distributable
Use platform-specific tools to create a distributable from the packaged app. See Distributing Apps With Electron Forge for details.
The following example creates a .dmg file on macOS:
import path from 'node:path';
import fs from 'node:fs/promises';
import { exec } from 'es-toolkit/server';
const appPath = path.join(
import.meta.dirname,
'out',
`MyWebviewBundleApplication-${process.platform}-${process.arch}`
);
const dmgSrc = path.join(import.meta.dirname, 'out', 'dmg');
await fs.rm(dmgSrc, { recursive: true, force: true });
await fs.mkdir(dmgSrc, { recursive: true });
await fs.cp(
path.join(appPath, 'MyWebviewBundleApplication.app'),
path.join(dmgSrc, 'MyWebviewBundleApplication.app'),
{
recursive: true,
verbatimSymlinks: true,
}
);
await fs.symlink('/Applications', path.join(dmgSrc, 'Applications'));
const dmgPath = path.join(
import.meta.dirname,
'out',
`MyWebviewBundleApplication.dmg`
);
await fs.rm(dmgPath, { force: true });
await exec('hdiutil', [
'create',
'-volname',
'MyWebviewBundleApplication',
'-srcfolder',
dmgSrc,
'-format',
'UDZO',
'-ov',
dmgPath,
]);For instructions on signing the app, see Electron Code Signing.
Electron Forge
Use the Webview Bundle plugin to integrate with Electron Forge.
@wvb/electron-forge copies builtin
bundles into the app resources. Install the plugin:
npm install --save-dev @wvb/electron-forgeThen add new WebviewBundlePlugin() to your Forge configuration:
import type { ForgeConfig } from '@electron-forge/shared-types';
import { AutoUnpackNativesPlugin } from '@electron-forge/plugin-auto-unpack-natives';
import { WebviewBundlePlugin } from '@wvb/electron-forge';
const config: ForgeConfig = {
packagerConfig: {
asar: true,
},
plugins: [new AutoUnpackNativesPlugin({}), new WebviewBundlePlugin()],
};
export default config;Include @wvb/electron and its native binding files in the packaged app. If
you build the main process with a bundler, mark @wvb/electron as external
to keep its runtime files available.
For Vite, use the following configuration:
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rolldownOptions: {
external: ['@wvb/electron'],
},
},
});Electron Builder
Integrate Webview Bundle with Electron Builder.
@wvb/electron-builder copies
builtin bundles into the app resources. Install the integration:
npm install --save-dev @wvb/electron-builderThen wrap your existing configuration with withWebviewBundle:
import { withWebviewBundle } from '@wvb/electron-builder';
export default withWebviewBundle({
appId: 'com.example.app',
asar: true,
asarUnpack: ['**/*.node'],
});