Skip to content

Latest commit

 

History

History
86 lines (73 loc) · 2.12 KB

README.md

File metadata and controls

86 lines (73 loc) · 2.12 KB

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"
      ],
    }
  }
}