New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto generate exposeInMainWorld typings for preload scripts exports #141
Comments
Upd: // vite.config.ts
export default defineConfig({
build: {
watch: {
include: 'src/**'
},
} ...
plugins: [
{
name: 'postbuild-commands', // the name of your custom plugin. Could be anything.
closeBundle: async () => {
await postBuildCommands() // run during closeBundle hook. https://rollupjs.org/guide/en/#closebundle
}
},
]
}) I guess we can write a tiny plugin that would open a process with |
UPD2: Used // electron.vite.config.ts
import { exec } from 'child_process'
// ...
export default defineConfig({
// ...
preload: {
plugins: [
// ...
{
name: 'rebuild-api-types',
writeBundle: (options: any, bundle: { [fileName: string]: any }) => {
exec(
'tsc --declaration --emitDeclarationOnly --outDir ./src/preload/ ./src/preload/api.ts',
(error, stdout, stderr) => {
if (error) return console.error(`Error: ${error.message}`)
if (stderr) return console.error(`Stderr: ${stderr}`)
console.log(' regenerated api.d.ts')
}
)
}
}
],
// ...
},
// ...
}) |
// preload/index.d.ts
import { ElectronAPI } from '@electron-toolkit/preload'
declare global {
interface Window {
electron: ElectronAPI
api: typeof preloadApi
}
} |
duplicate #121 |
@alex8088 I've upgraded my approach - it generates api from handlers automatically so less boilerplate // src/preload/api.ts
import { ipcRenderer } from 'electron'
import { ipcHandlers } from '../src-main/handlers'
type ApiFromHandlers<T extends Record<string, (event: unknown, ...args: any[]) => any>> = {
[K in keyof T]: (
...args: Parameters<T[K]> extends [unknown, ...infer R] ? R : never
) => Promise<ReturnType<T[K]>>
}
function createApi<T extends Record<string, (event: unknown, ...args: any[]) => any>>(
handlers: T
): ApiFromHandlers<T> {
const api: Partial<ApiFromHandlers<T>> = {}
for (const key in handlers) {
api[key as keyof T] = (...args: any[]) => ipcRenderer.invoke(key as string, ...args)
}
return api as ApiFromHandlers<T>
}
export const api = createApi(ipcHandlers)
declare global {
type Api = typeof api
} // src/main/ipcHandlers
export const ipcHandlers = {
downloadPreset: async (event: any, params: { packName: string, url: string, name: string, vst: string }) => {
},
checkPresetInstalled: (event, params: { packName: string, name: string, vst: string }): boolean | Error => {
},
}
export type IpcHandlers = typeof ipcHandlers |
@syabro Thank you! I now have types for my preload api ⚡ @alex8088 Thank you for this great electron framework 😄 For automation, I added a {
"scripts": {
"typecheck:preload": "tsc --declaration --emitDeclarationOnly --outDir ./src/preload/ ./src/preload/api.ts",
"typecheck": "yarn typecheck:preload && yarn typecheck:node && yarn typecheck:web",
...
}
} Now if I make changes, I can just build the app and the typings will be regenerated for me. |
I think we may have a better solution to this problem. For now, we are considering unplugin-auto-expose or unplugin-elexpse as a possible solution. But they have some limitations and don't work out of the box. Considerations for out-of-the-box:
|
Clear and concise description of the problem
Currently api type is unknown
And I have to replace it with real api and keep them synced
Suggested solution
api.ts
index.d.ts
to use globalApi
Alternative
No response
Additional context
The problem here I don't know how to call api type compilation somewhere from vite without running
tsc
separately :(Validations
The text was updated successfully, but these errors were encountered: