forked from jsnbuchanan/crowd-funder-for-time-pwa
- Properly disable service workers in electron builds - Add CSP headers for electron security - Fix path resolution in electron context - Improve preload script error handling and IPC setup - Update build scripts for better electron/capacitor compatibility - Fix router path handling in electron context - Remove electron-builder dependency - Streamline build process and output structure This change improves the stability and security of electron builds while maintaining PWA functionality in web builds. Service workers are now properly disabled in electron context, and path resolution issues are fixed.
121 lines
3.4 KiB
JavaScript
121 lines
3.4 KiB
JavaScript
import { defineConfig } from "vite";
|
|
import { VitePWA } from "vite-plugin-pwa";
|
|
import vue from "@vitejs/plugin-vue";
|
|
import dotenv from "dotenv";
|
|
import { loadAppConfig } from "./vite.config.utils";
|
|
import path from "path";
|
|
import { fileURLToPath } from 'url';
|
|
|
|
// Load environment variables from .env file
|
|
dotenv.config();
|
|
|
|
// Get dirname in ESM context
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
// Load application configuration
|
|
const appConfig = loadAppConfig();
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
const isElectron = mode === "electron";
|
|
const isCapacitor = mode === "capacitor";
|
|
|
|
// Completely disable PWA features for electron builds
|
|
if (isElectron) {
|
|
process.env.VITE_PWA_ENABLED = 'false';
|
|
}
|
|
|
|
return {
|
|
base: isElectron ? "./" : "/",
|
|
server: {
|
|
port: process.env.VITE_PORT || 8080,
|
|
fs: {
|
|
strict: false
|
|
}
|
|
},
|
|
build: {
|
|
outDir: isElectron ? "dist-electron" : "dist",
|
|
assetsDir: 'assets',
|
|
rollupOptions: {
|
|
external: ['electron', 'path'],
|
|
input: {
|
|
main: path.resolve(__dirname, 'index.html')
|
|
},
|
|
output: {
|
|
manualChunks(id) {
|
|
if (isElectron && (
|
|
id.includes('registerServiceWorker') ||
|
|
id.includes('register-service-worker') ||
|
|
id.includes('workbox') ||
|
|
id.includes('sw_scripts') ||
|
|
id.includes('PushNotificationPermission')
|
|
)) {
|
|
return null; // Exclude these modules completely
|
|
}
|
|
}
|
|
}
|
|
},
|
|
chunkSizeWarningLimit: 1000
|
|
},
|
|
define: {
|
|
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
|
'process.env.VITE_PWA_ENABLED': JSON.stringify(!isElectron),
|
|
__dirname: isElectron ? JSON.stringify(process.cwd()) : '""',
|
|
'navigator.serviceWorker': isElectron ? 'undefined' : 'navigator.serviceWorker'
|
|
},
|
|
plugins: [
|
|
vue(),
|
|
{
|
|
name: 'remove-sw-imports',
|
|
transform(code, id) {
|
|
if (isElectron) {
|
|
if (
|
|
id.includes('registerServiceWorker') ||
|
|
id.includes('register-service-worker') ||
|
|
id.includes('sw_scripts') ||
|
|
id.includes('PushNotificationPermission') ||
|
|
code.includes('navigator.serviceWorker')
|
|
) {
|
|
return {
|
|
code: code
|
|
.replace(/import.*registerServiceWorker.*$/mg, '')
|
|
.replace(/import.*register-service-worker.*$/mg, '')
|
|
.replace(/navigator\.serviceWorker/g, 'undefined')
|
|
.replace(/if\s*\([^)]*serviceWorker[^)]*\)\s*{[^}]*}/g, '')
|
|
};
|
|
}
|
|
}
|
|
}
|
|
},
|
|
...(!isElectron && !isCapacitor ? [
|
|
VitePWA({
|
|
disable: true,
|
|
registerType: 'autoUpdate',
|
|
injectRegister: null,
|
|
workbox: {
|
|
cleanupOutdatedCaches: true,
|
|
skipWaiting: true,
|
|
clientsClaim: true,
|
|
sourcemap: true
|
|
},
|
|
manifest: appConfig.pwaConfig?.manifest,
|
|
devOptions: {
|
|
enabled: false
|
|
}
|
|
}),
|
|
] : []),
|
|
],
|
|
resolve: {
|
|
alias: appConfig.aliasConfig
|
|
},
|
|
optimizeDeps: {
|
|
exclude: isElectron ? [
|
|
'register-service-worker',
|
|
'workbox-window',
|
|
'web-push',
|
|
'serviceworker-webpack-plugin'
|
|
] : []
|
|
}
|
|
};
|
|
});
|