diff --git a/package.json b/package.json index 9d87540b..a2cc9490 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "check:ios-device": "xcrun xctrace list devices 2>&1 | grep -w 'Booted' || (echo 'No iOS simulator running' && exit 1)", "clean:electron": "rimraf dist-electron", "build:pywebview": "vite build --config vite.config.pywebview.mts", - "build:electron": "npm run clean:electron && tsc -p tsconfig.electron.json && vite build --config vite.config.electron.mts && node scripts/build-electron.js", + "build:electron": "npm run clean:electron && npm run build:web && tsc -p tsconfig.electron.json && vite build --config vite.config.electron.mts && node scripts/build-electron.js", "build:capacitor": "vite build --mode capacitor --config vite.config.capacitor.mts", "build:web": "VITE_GIT_HASH=`git log -1 --pretty=format:%h` vite build --config vite.config.web.mts", "electron:dev": "npm run build && electron .", diff --git a/scripts/build-electron.js b/scripts/build-electron.js index 01ba62d8..f9d8fed6 100644 --- a/scripts/build-electron.js +++ b/scripts/build-electron.js @@ -1,165 +1,50 @@ -const fs = require('fs'); -const path = require('path'); +const fs = require("fs"); +const fse = require("fs-extra"); +const path = require("path"); -console.log('Starting electron build process...'); +console.log("Starting Electron build finalization..."); // Define paths -const electronDistPath = path.join(__dirname, '..', 'dist-electron'); -const wwwPath = path.join(electronDistPath, 'www'); +const distPath = path.join(__dirname, "..", "dist"); +const electronDistPath = path.join(__dirname, "..", "dist-electron"); +const wwwPath = path.join(electronDistPath, "www"); +const builtIndexPath = path.join(distPath, "index.html"); +const finalIndexPath = path.join(wwwPath, "index.html"); -// Create www directory if it doesn't exist +// Ensure target directory exists if (!fs.existsSync(wwwPath)) { fs.mkdirSync(wwwPath, { recursive: true }); } -// Create a platform-specific index.html for Electron -const initialIndexContent = ` - - - - - - - TimeSafari - - - -
- - -`; - -// Write the Electron-specific index.html -fs.writeFileSync(path.join(wwwPath, 'index.html'), initialIndexContent); - -// Copy only necessary assets from web build -const webDistPath = path.join(__dirname, '..', 'dist'); -if (fs.existsSync(webDistPath)) { - // Copy assets directory - const assetsSrc = path.join(webDistPath, 'assets'); - const assetsDest = path.join(wwwPath, 'assets'); - if (fs.existsSync(assetsSrc)) { - fs.cpSync(assetsSrc, assetsDest, { recursive: true }); - } - - // Copy favicon - const faviconSrc = path.join(webDistPath, 'favicon.ico'); - if (fs.existsSync(faviconSrc)) { - fs.copyFileSync(faviconSrc, path.join(wwwPath, 'favicon.ico')); - } -} - -// Remove service worker files -const swFilesToRemove = [ - 'sw.js', - 'sw.js.map', - 'workbox-*.js', - 'workbox-*.js.map', - 'registerSW.js', - 'manifest.webmanifest', - '**/workbox-*.js', - '**/workbox-*.js.map', - '**/sw.js', - '**/sw.js.map', - '**/registerSW.js', - '**/manifest.webmanifest' -]; - -console.log('Removing service worker files...'); -swFilesToRemove.forEach(pattern => { - const files = fs.readdirSync(wwwPath).filter(file => - file.match(new RegExp(pattern.replace(/\*/g, '.*'))) - ); - files.forEach(file => { - const filePath = path.join(wwwPath, file); - console.log(`Removing ${filePath}`); - try { - fs.unlinkSync(filePath); - } catch (err) { - console.warn(`Could not remove ${filePath}:`, err.message); - } - }); -}); - -// Also check and remove from assets directory -const assetsPath = path.join(wwwPath, 'assets'); -if (fs.existsSync(assetsPath)) { - swFilesToRemove.forEach(pattern => { - const files = fs.readdirSync(assetsPath).filter(file => - file.match(new RegExp(pattern.replace(/\*/g, '.*'))) - ); - files.forEach(file => { - const filePath = path.join(assetsPath, file); - console.log(`Removing ${filePath}`); - try { - fs.unlinkSync(filePath); - } catch (err) { - console.warn(`Could not remove ${filePath}:`, err.message); - } - }); - }); +// Copy assets directory +const assetsSrc = path.join(distPath, "assets"); +const assetsDest = path.join(wwwPath, "assets"); +if (fs.existsSync(assetsSrc)) { + fse.copySync(assetsSrc, assetsDest, { overwrite: true }); } -// Modify index.html to remove service worker registration -const indexPath = path.join(wwwPath, 'index.html'); -if (fs.existsSync(indexPath)) { - console.log('Modifying index.html to remove service worker registration...'); - let indexContent = fs.readFileSync(indexPath, 'utf8'); - - // Remove service worker registration script - indexContent = indexContent - .replace(/]*id="vite-plugin-pwa:register-sw"[^>]*><\/script>/g, '') - .replace(/]*registerServiceWorker[^>]*><\/script>/g, '') - .replace(/]*rel="manifest"[^>]*>/g, '') - .replace(/]*rel="serviceworker"[^>]*>/g, '') - .replace(/navigator\.serviceWorker\.register\([^)]*\)/g, '') - .replace(/if\s*\(\s*['"]serviceWorker['"]\s*in\s*navigator\s*\)\s*{[^}]*}/g, ''); - - fs.writeFileSync(indexPath, indexContent); - console.log('Successfully modified index.html'); +// Copy favicon.ico +const faviconSrc = path.join(distPath, "favicon.ico"); +if (fs.existsSync(faviconSrc)) { + fs.copyFileSync(faviconSrc, path.join(wwwPath, "favicon.ico")); } -// Fix asset paths -console.log('Fixing asset paths in index.html...'); -let modifiedIndexContent = fs.readFileSync(indexPath, 'utf8'); -modifiedIndexContent = modifiedIndexContent - .replace(/\/assets\//g, './assets/') - .replace(/href="\//g, 'href="./') - .replace(/src="\//g, 'src="./'); - -fs.writeFileSync(indexPath, modifiedIndexContent); - -// Verify no service worker references remain -const finalContent = fs.readFileSync(indexPath, 'utf8'); -if (finalContent.includes('serviceWorker') || finalContent.includes('workbox')) { - console.warn('Warning: Service worker references may still exist in index.html'); +// Copy manifest.webmanifest +const manifestSrc = path.join(distPath, "manifest.webmanifest"); +if (fs.existsSync(manifestSrc)) { + fs.copyFileSync(manifestSrc, path.join(wwwPath, "manifest.webmanifest")); } -// Check for remaining /assets/ paths -console.log('After path fixing, checking for remaining /assets/ paths:', finalContent.includes('/assets/')); -console.log('Sample of fixed content:', finalContent.substring(0, 500)); - -console.log('Copied and fixed web files in:', wwwPath); +// Load and modify index.html from Vite output +let indexContent = fs.readFileSync(builtIndexPath, "utf-8"); -// Copy main process files -console.log('Copying main process files...'); +// Inject the window.process shim after the first