Skip to content

Plugins for automatic exposeInMainWorld everything you exported from preload and easily importing exposed api in renderer

License

Notifications You must be signed in to change notification settings

ymhrainy/unplugin-auto-expose

 
 

Repository files navigation

Stand With Ukraine


unplugin-auto-expose

Buy Me A Coffee

Plugins for automatic exposeInMainWorld. Easily export your exposed api from preload to renderer.

Example

// 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__

Limitation

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

Configuration

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',
      }
    })
  ]
})

TypeScript

To configure the TypeScript, add a path to your renderer.

// tsconfig.json`:
{
  "compilerOptions": {
    "paths": {
      "#preload1": [
        "/path/to/preload1"
      ],
      "#preload2": [
        "/path/to/preload2"
      ],
    }
  }
}

About

Plugins for automatic exposeInMainWorld everything you exported from preload and easily importing exposed api in renderer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 71.7%
  • JavaScript 25.1%
  • HTML 3.2%