Plugins for automatic exposeInMainWorld
. Easily export your exposed api from preload
to renderer
.
// preload.ts
export const foo = 'foo string'
// Equivalent
electron.contextBridge.exposeInMainWorld('__electron_preload_foo__', 'foo string')
// renderer.ts
import {foo} from '#preload'
// Equivalent
const foo = window.__electron_preload_foo__
Only named exports are supported.
export * from 'file' // ❌ Will not work
export {prop} from 'file' // ✔
export * as props from 'file' // ❌ Will not work
import * as file from 'file'
export const props = file // ⚠ Will work but not recommended for security reasons
This package contains two plugins: one for preload and one for renderer builds.
// preload/vite.config.ts
import {preload} from 'unplugin-auto-expose';
export default defineConfig({
plugins: [
preload.vite({
exposeName: (name) => name, // must be same as config in renderer config
noContextBridgeEntries: [], // fill filename without .ts here if you must set contextIsolation to false for some window, eg ['preload2']
})
]
})
// renderer/vite.config.ts
import {renderer} from 'unplugin-auto-expose';
export default defineConfig({
plugins: [
renderer.vite({
exposeName: (name) => name, // must be same as config in preload config
virtualModuleMap: {
'#preload1': '/absolute/path/to/preload1.ts',
'#preload2': '/absolute/path/to/preload2.ts',
}
})
]
})
To configure the TypeScript, add a path to your renderer.
// tsconfig.json`:
{
"compilerOptions": {
"paths": {
"#preload1": [
"/path/to/preload1"
],
"#preload2": [
"/path/to/preload2"
],
}
}
}