FreeDATA/gui/vite.config.ts

103 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2023-10-22 08:12:00 +00:00
import { rmSync } from "node:fs";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import electron from "vite-plugin-electron";
import renderer from "vite-plugin-electron-renderer";
import { notBundle } from "vite-plugin-electron/plugin";
import pkg from "./package.json";
2023-09-02 11:37:03 +00:00
// https://vitejs.dev/config/
2023-10-04 17:54:50 +00:00
export default defineConfig(({ command }) => {
2023-10-22 08:12:00 +00:00
rmSync("dist-electron", { recursive: true, force: true });
2023-09-05 13:28:58 +00:00
2023-10-22 08:12:00 +00:00
const isServe = command === "serve";
const isBuild = command === "build";
const sourcemap = isServe || !!process.env.VSCODE_DEBUG;
2023-09-05 13:28:58 +00:00
2023-10-04 17:54:50 +00:00
return {
2024-01-08 22:01:26 +00:00
optimizeDeps: {
esbuildOptions: {
target: "esnext",
},
2024-01-08 22:01:26 +00:00
},
build: {
target: "esnext",
2024-01-08 22:01:26 +00:00
},
2023-10-04 17:54:50 +00:00
plugins: [
vue(),
electron([
{
// Main process entry file of the Electron App.
2023-10-22 08:12:00 +00:00
entry: "electron/main/index.ts",
2023-10-04 17:54:50 +00:00
onstart({ startup }) {
if (process.env.VSCODE_DEBUG) {
2023-10-22 08:12:00 +00:00
console.log(
/* For `.vscode/.debug.script.mjs` */ "[startup] Electron App",
);
2023-10-04 17:54:50 +00:00
} else {
2023-10-22 08:12:00 +00:00
startup();
2023-10-04 17:54:50 +00:00
}
},
vite: {
build: {
sourcemap,
minify: isBuild,
2023-10-22 08:12:00 +00:00
outDir: "dist-electron/main",
2023-10-04 17:54:50 +00:00
rollupOptions: {
// Some third-party Node.js libraries may not be built correctly by Vite, especially `C/C++` addons,
// we can use `external` to exclude them to ensure they work correctly.
// Others need to put them in `dependencies` to ensure they are collected into `app.asar` after the app is built.
// Of course, this is not absolute, just this way is relatively simple. :)
2023-10-22 08:12:00 +00:00
external: Object.keys(
"dependencies" in pkg ? pkg.dependencies : {},
),
2023-10-04 17:54:50 +00:00
},
},
plugins: [
// This is just an option to improve build performance, it's non-deterministic!
// e.g. `import log from 'electron-log'` -> `const log = require('electron-log')`
isServe && notBundle(),
],
},
2023-09-05 09:22:00 +00:00
},
2023-10-04 17:54:50 +00:00
{
2023-10-22 08:12:00 +00:00
entry: "electron/preload/index.ts",
2023-10-04 17:54:50 +00:00
onstart({ reload }) {
// Notify the Renderer process to reload the page when the Preload scripts build is complete,
// instead of restarting the entire Electron App.
2023-10-22 08:12:00 +00:00
reload();
2023-10-04 17:54:50 +00:00
},
vite: {
build: {
2023-10-22 08:12:00 +00:00
sourcemap: sourcemap ? "inline" : undefined, // #332
2023-10-04 17:54:50 +00:00
minify: isBuild,
2023-10-22 08:12:00 +00:00
outDir: "dist-electron/preload",
2023-10-04 17:54:50 +00:00
rollupOptions: {
2023-10-22 08:12:00 +00:00
external: Object.keys(
"dependencies" in pkg ? pkg.dependencies : {},
),
2023-10-04 17:54:50 +00:00
},
},
2023-10-22 08:12:00 +00:00
plugins: [isServe && notBundle()],
2023-10-04 17:54:50 +00:00
},
2023-10-22 08:12:00 +00:00
},
2023-10-04 17:54:50 +00:00
]),
// Use Node.js API in the Renderer process
renderer(),
2023-09-05 09:22:00 +00:00
],
2023-10-22 08:12:00 +00:00
server:
process.env.VSCODE_DEBUG &&
(() => {
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL);
return {
host: url.hostname,
port: +url.port,
};
})(),
define: {
"import.meta.env.PACKAGE_VERSION": JSON.stringify(pkg.version),
},
2023-10-04 17:54:50 +00:00
clearScreen: false,
2023-10-22 08:12:00 +00:00
};
});