forked from jsnbuchanan/crowd-funder-for-time-pwa
WIP (fix): improve electron build configuration and service worker handling
- 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.
This commit is contained in:
114
vite.config.mjs
114
vite.config.mjs
@@ -4,10 +4,15 @@ 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();
|
||||
|
||||
@@ -15,48 +20,101 @@ export default defineConfig(({ mode }) => {
|
||||
const isElectron = mode === "electron";
|
||||
const isCapacitor = mode === "capacitor";
|
||||
|
||||
// Set output directory based on build mode
|
||||
const outDir = isElectron
|
||||
? "dist-electron/www"
|
||||
: isCapacitor
|
||||
? "dist-capacitor"
|
||||
: "dist";
|
||||
// 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,
|
||||
outDir: isElectron ? "dist-electron" : "dist",
|
||||
assetsDir: 'assets',
|
||||
rollupOptions: {
|
||||
...(isElectron && {
|
||||
input: {
|
||||
index: path.resolve(__dirname, 'index.html')
|
||||
},
|
||||
output: {
|
||||
dir: outDir,
|
||||
format: 'cjs',
|
||||
entryFileNames: 'assets/[name].[hash].js',
|
||||
chunkFileNames: 'assets/[name].[hash].js',
|
||||
assetFileNames: 'assets/[name].[hash][extname]'
|
||||
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(),
|
||||
...(isElectron
|
||||
? []
|
||||
: [
|
||||
VitePWA({
|
||||
...appConfig.pwaConfig,
|
||||
disable: isElectron
|
||||
}),
|
||||
]),
|
||||
{
|
||||
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,
|
||||
alias: appConfig.aliasConfig
|
||||
},
|
||||
optimizeDeps: {
|
||||
exclude: isElectron ? [
|
||||
'register-service-worker',
|
||||
'workbox-window',
|
||||
'web-push',
|
||||
'serviceworker-webpack-plugin'
|
||||
] : []
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user