Created
August 30, 2024 20:09
-
-
Save magnus-trent/1090f4e633dd6b245b793842014a4f0d to your computer and use it in GitHub Desktop.
An electron-vite config which covers: multi-window, TanStack Router, generating a product.json, and using Prisma for an internal SQLite3 database. It's not clean, but it works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typescript from "@rollup/plugin-typescript"; | |
import {TanStackRouterVite} from "@tanstack/router-plugin/vite"; | |
import react from "@vitejs/plugin-react"; | |
import {defineConfig, externalizeDepsPlugin} from "electron-vite"; | |
import {existsSync} from "fs"; | |
import {mkdir, rm, writeFile} from "fs/promises"; | |
import {copyFile} from "node:fs/promises"; | |
import {resolve} from "path"; | |
import {IProduct} from "./src/platform/product/common/product-service.interface.js"; | |
const pkg = require("./package.json"); | |
const product: IProduct = { | |
nameShort: pkg.productName, | |
nameLong: pkg.productName, | |
description: pkg.description, | |
applicationName: pkg.name, | |
license: { | |
name: pkg.license, | |
url: "https://opensource.org/license/mit/", | |
}, | |
update: { | |
url: process.env.TOWER_URL!, | |
channel: process.env.UPDATE_CHANNEL!, | |
date: Date.now().toString(), | |
version: pkg.version, | |
}, | |
win32: { | |
appUserModelId: `${String(pkg.companyNameShort).replaceAll(" ", "")}.${String(pkg.productName).replaceAll(" ", "")}`, | |
}, | |
}; | |
// generate a product.json file using product object | |
const productJson = JSON.stringify(product, null, 2); | |
// write the product.json file to the out directory | |
if (existsSync("out")) | |
// Might as well while we're here | |
await rm("out", {recursive: true}); | |
await mkdir("out"); | |
await writeFile(resolve(__dirname, "out/product.json"), productJson); | |
// ensure we use a copy of the database during development, not copied during packaging | |
await copyFile( | |
resolve(__dirname, `resources/${process.env["VITE_DATABASE_URL"]}`), | |
resolve(__dirname, `out/${process.env["VITE_DATABASE_URL"]}`) | |
); | |
const getWindowHtml = (nameWithoutExtension: string) => | |
resolve(__dirname, "src/workbench/windows/", `${nameWithoutExtension}.html`); | |
const getPreloadFile = (nameWithoutExtension: string) => | |
resolve( | |
__dirname, | |
"src/base/electron-sandbox/", | |
`${nameWithoutExtension}.preload.ts` | |
); | |
const resourcesDirectory = resolve(__dirname, "src/resources"); | |
const workbenchDirectory = resolve(__dirname, "src/workbench"); | |
// @ts-ignore | |
export default defineConfig({ | |
main: { | |
build: { | |
outDir: resolve(__dirname, "out/main"), | |
sourcemap: true, | |
rollupOptions: { | |
output: { | |
format: "es", | |
}, | |
input: resolve(__dirname, "src/core/electron-main/main.ts"), | |
}, | |
}, | |
resolve: { | |
alias: { | |
".prisma/client/default": "./node_modules/.prisma/client/default.js", | |
}, | |
}, | |
// @ts-ignore | |
plugins: [externalizeDepsPlugin(), typescript()], | |
publicDir: resourcesDirectory, | |
}, | |
preload: { | |
build: { | |
sourcemap: true, | |
outDir: resolve(__dirname, "out/preload"), | |
rollupOptions: { | |
output: { | |
format: "es", | |
}, | |
input: [ | |
getPreloadFile("main"), | |
getPreloadFile("registration"), | |
getPreloadFile("view"), | |
getPreloadFile("settings"), | |
], | |
}, | |
}, | |
// @ts-ignore | |
plugins: [externalizeDepsPlugin(), typescript()], | |
publicDir: resourcesDirectory, | |
}, | |
renderer: { | |
root: workbenchDirectory, | |
build: { | |
rollupOptions: { | |
input: { | |
main_window: getWindowHtml("main"), | |
registration_window: getWindowHtml("registration"), | |
view_window: getWindowHtml("view"), | |
settings_window: getWindowHtml("settings"), | |
}, | |
}, | |
sourcemap: true, | |
outDir: resolve(__dirname, "out/renderer"), | |
}, | |
resolve: { | |
alias: { | |
"@/resources": resourcesDirectory, | |
"@/workbench": workbenchDirectory, | |
}, | |
}, | |
plugins: [ | |
react(), | |
typescript(), | |
// TODO this is restricted to only the settings window. Investigate multi-window approach. | |
// TODO drop multi-window for internal pages (i.e about:config) | |
TanStackRouterVite({ | |
routesDirectory: resolve( | |
workbenchDirectory, | |
"windows", | |
"settings/routes" | |
), | |
generatedRouteTree: resolve( | |
workbenchDirectory, | |
"windows", | |
"settings/routeTree.gen.ts" | |
), | |
}), | |
], | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment