import { mergeConfig } from "vite"; import electron from "./index.mjs"; import "node:module"; async function electronSimple(options) { const opts = [options.main]; if (options.preload) { const { input, vite: viteConfig = {}, ...preloadOptions } = options.preload; const preload = { onstart(args) { args.reload(); }, ...preloadOptions, vite: mergeConfig({ build: { rollupOptions: { input, output: { // For use the Electron API - `import { contextBridge, ipcRenderer } from 'electron'`, // Note, however, that `preload.ts` should not be split. 🚧 format: "cjs", // Whether Node.js is enabled in the Main process or not, the Preload scripts supports loading `electron` module, // so we need to build it in `cjs` format. // e.g. // import { ipcRenderer } from 'electron' // // ↓↓↓↓ Build with `cjs` format ↓↓↓↓ // const { ipcRenderer } = require('electron') // When Rollup builds code in `cjs` format, it will automatically split the code into multiple chunks, and use `require()` to load them, // and use `require()` to load other modules when `nodeIntegration: false` in the Main process Errors will occur. // So we need to configure Rollup not to split the code when building to ensure that it works correctly with `nodeIntegration: false`. inlineDynamicImports: true, // https://github.com/vitejs/vite/blob/v4.4.9/packages/vite/src/node/build.ts#L604 entryFileNames: "[name].js", chunkFileNames: "[name].js", assetFileNames: "[name].[ext]" } } } }, viteConfig) }; opts.push(preload); } const plugins = electron(opts); if (options.renderer) { try { const renderer = await import("vite-plugin-electron-renderer"); plugins.push(renderer.default(options.renderer)); } catch (error) { if (error.code === "ERR_MODULE_NOT_FOUND") { throw new Error( `\`renderer\` option dependency "vite-plugin-electron-renderer" not found. Did you install it? Try \`npm i -D vite-plugin-electron-renderer\`.` ); } throw error; } } return plugins; } export { electronSimple as default };