summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTobias Järvelöv <tobias.jarvelov@mullvad.net>2025-02-10 09:49:38 +0100
committerMarkus Pettersson <markus.pettersson@mullvad.net>2025-02-25 12:51:51 +0100
commitd6aeb6b8bc2aea525ce2bd5712a296973ac39eab (patch)
treeb6799beecb4ad5b57cbc6a85d05d69a5c2bec03e
parent738f1788dddbdad0db0389306accb8c15b7f1305 (diff)
downloadmullvadvpn-d6aeb6b8bc2aea525ce2bd5712a296973ac39eab.tar.xz
mullvadvpn-d6aeb6b8bc2aea525ce2bd5712a296973ac39eab.zip
Add vite config
-rw-r--r--desktop/packages/mullvad-vpn/vite.config.ts117
1 files changed, 117 insertions, 0 deletions
diff --git a/desktop/packages/mullvad-vpn/vite.config.ts b/desktop/packages/mullvad-vpn/vite.config.ts
new file mode 100644
index 0000000000..657111cf9a
--- /dev/null
+++ b/desktop/packages/mullvad-vpn/vite.config.ts
@@ -0,0 +1,117 @@
+import react from '@vitejs/plugin-react';
+import { defineConfig } from 'vite';
+import { startup } from 'vite-plugin-electron';
+import electron from 'vite-plugin-electron/simple';
+
+import { treeKillSync } from './vite-utils';
+
+// NOTE: We have to monkey patch the exit handler to override the default
+// behavior for how to kill the electron app. We use a custom variant of the
+// vite-plugin-electron's treeKillSync function to target only the electron
+// application's process and its children and not the current behavior where
+// the current process' children is targeted. This is because the current
+// process spawns two processes, the electron app and esbuild.
+//
+// The default behavior of vite-plugin-electron when the electron app needs to
+// restart is to kill both the electron app and the esbuild processes, however
+// after that only the electron app gets respawned, leaving the esbuild process
+// permanently dead after the first time the electron app has restarted.
+//
+// This should be fixed upstream but until then this is an okay workaround.
+// As this is a hack I didn't bother fixing the types for process.electronApp
+// correctly, hence the ts-ignore below.
+startup.exit = async () => {
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ const electronApp = process.electronApp;
+ if (electronApp) {
+ await new Promise((resolve) => {
+ electronApp.removeAllListeners();
+ electronApp.once('exit', resolve);
+ treeKillSync(electronApp.pid);
+ });
+ }
+};
+
+const MAIN = process.env.NODE_ENV === 'test' ? 'test/e2e/setup/main.ts' : 'src/main/index.ts';
+const OUT_DIR = 'build';
+
+const viteConfig = defineConfig({
+ define: {
+ global: 'window',
+ process: {
+ platform: process.platform,
+ env: {
+ NODE_ENV: process.env.NODE_ENV,
+ },
+ },
+ },
+ build: {
+ outDir: OUT_DIR,
+ },
+ plugins: [
+ electron({
+ main: {
+ entry: MAIN,
+ async onstart({ startup }) {
+ // NOTE: vite-plugin-electron automatically adds --no-sandbox to its
+ // command line arguments when spawning electron. From a security
+ // standpoint this is not a good default so we omit it to allow
+ // us setting it programmatically in the main process.
+ //
+ // Another consequence of the default --no-sandbox being added was
+ // that it caused a crash when the devtools opened if the sandbox
+ // had not been enabled again. However, after the default --no-sandbox
+ // was omitted we can open the devtools regardless of whether the
+ // sandbox is enabled or not.
+ await startup(['.']);
+ },
+ vite: {
+ build: {
+ outDir: OUT_DIR,
+ commonjsOptions: {
+ include: [
+ // Packages in workspace which exports common js
+ /management-interface/,
+ /nseventforwarder/,
+ /win-shortcuts/,
+ // External dependencies which exports common js
+ /node_modules/,
+ ],
+ },
+ rollupOptions: {
+ output: {
+ // We have to specify main.js here as otherwise it would
+ // inherit the name from the entry file, i.e. index
+ entryFileNames: 'main.js',
+ },
+ external: [
+ // Packages in workspace which can not be bundled
+ 'win-shortcuts',
+ // External dependencies
+ '@grpc/grpc-js',
+ 'google-protobuf',
+ 'simple-plist',
+ ],
+ },
+ },
+ // Dependencies which can be transformed to e.g. become smaller or more efficient
+ optimizeDeps: {
+ include: ['management-interface', 'nseventforwarder'],
+ },
+ },
+ },
+ preload: {
+ input: 'src/renderer/preload.ts',
+ vite: {
+ build: {
+ outDir: OUT_DIR,
+ },
+ },
+ },
+ }),
+ react(),
+ ],
+});
+
+export default viteConfig;