3 changed files with 55 additions and 149 deletions
@ -1,165 +1,50 @@ |
|||||
const fs = require('fs'); |
const fs = require("fs"); |
||||
const path = require('path'); |
const fse = require("fs-extra"); |
||||
|
const path = require("path"); |
||||
|
|
||||
console.log('Starting electron build process...'); |
console.log("Starting Electron build finalization..."); |
||||
|
|
||||
// Define paths
|
// Define paths
|
||||
const electronDistPath = path.join(__dirname, '..', 'dist-electron'); |
const distPath = path.join(__dirname, "..", "dist"); |
||||
const wwwPath = path.join(electronDistPath, 'www'); |
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)) { |
if (!fs.existsSync(wwwPath)) { |
||||
fs.mkdirSync(wwwPath, { recursive: true }); |
fs.mkdirSync(wwwPath, { recursive: true }); |
||||
} |
} |
||||
|
|
||||
// Create a platform-specific index.html for Electron
|
// Copy assets directory
|
||||
const initialIndexContent = `<!DOCTYPE html>
|
const assetsSrc = path.join(distPath, "assets"); |
||||
<html lang=""> |
const assetsDest = path.join(wwwPath, "assets"); |
||||
<head> |
if (fs.existsSync(assetsSrc)) { |
||||
<meta charset="utf-8"> |
fse.copySync(assetsSrc, assetsDest, { overwrite: true }); |
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> |
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0,viewport-fit=cover"> |
|
||||
<link rel="icon" href="/favicon.ico"> |
|
||||
<title>TimeSafari</title> |
|
||||
</head> |
|
||||
<body> |
|
||||
<noscript> |
|
||||
<strong>We're sorry but TimeSafari doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> |
|
||||
</noscript> |
|
||||
<div id="app"></div> |
|
||||
<script type="module"> |
|
||||
// Force electron platform
|
|
||||
window.process = { env: { VITE_PLATFORM: 'electron' } }; |
|
||||
import('./src/main.electron.ts'); |
|
||||
</script> |
|
||||
</body> |
|
||||
</html>`; |
|
||||
|
|
||||
// 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); |
|
||||
} |
|
||||
}); |
|
||||
}); |
|
||||
} |
} |
||||
|
|
||||
// Modify index.html to remove service worker registration
|
// Copy favicon.ico
|
||||
const indexPath = path.join(wwwPath, 'index.html'); |
const faviconSrc = path.join(distPath, "favicon.ico"); |
||||
if (fs.existsSync(indexPath)) { |
if (fs.existsSync(faviconSrc)) { |
||||
console.log('Modifying index.html to remove service worker registration...'); |
fs.copyFileSync(faviconSrc, path.join(wwwPath, "favicon.ico")); |
||||
let indexContent = fs.readFileSync(indexPath, 'utf8'); |
|
||||
|
|
||||
// Remove service worker registration script
|
|
||||
indexContent = indexContent |
|
||||
.replace(/<script[^>]*id="vite-plugin-pwa:register-sw"[^>]*><\/script>/g, '') |
|
||||
.replace(/<script[^>]*registerServiceWorker[^>]*><\/script>/g, '') |
|
||||
.replace(/<link[^>]*rel="manifest"[^>]*>/g, '') |
|
||||
.replace(/<link[^>]*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'); |
|
||||
} |
} |
||||
|
|
||||
// Fix asset paths
|
// Copy manifest.webmanifest
|
||||
console.log('Fixing asset paths in index.html...'); |
const manifestSrc = path.join(distPath, "manifest.webmanifest"); |
||||
let modifiedIndexContent = fs.readFileSync(indexPath, 'utf8'); |
if (fs.existsSync(manifestSrc)) { |
||||
modifiedIndexContent = modifiedIndexContent |
fs.copyFileSync(manifestSrc, path.join(wwwPath, "manifest.webmanifest")); |
||||
.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'); |
|
||||
} |
} |
||||
|
|
||||
// Check for remaining /assets/ paths
|
// Load and modify index.html from Vite output
|
||||
console.log('After path fixing, checking for remaining /assets/ paths:', finalContent.includes('/assets/')); |
let indexContent = fs.readFileSync(builtIndexPath, "utf-8"); |
||||
console.log('Sample of fixed content:', finalContent.substring(0, 500)); |
|
||||
|
|
||||
console.log('Copied and fixed web files in:', wwwPath); |
// Inject the window.process shim after the first <script> block
|
||||
|
indexContent = indexContent.replace( |
||||
|
/<script[^>]*type="module"[^>]*>/, |
||||
|
match => `${match}\n window.process = { env: { VITE_PLATFORM: 'electron' } };` |
||||
|
); |
||||
|
|
||||
// Copy main process files
|
// Write the modified index.html to dist-electron/www
|
||||
console.log('Copying main process files...'); |
fs.writeFileSync(finalIndexPath, indexContent); |
||||
|
|
||||
// Copy the main process file instead of creating a template
|
|
||||
const mainSrcPath = path.join(__dirname, '..', 'dist-electron', 'main.js'); |
|
||||
const mainDestPath = path.join(electronDistPath, 'main.js'); |
|
||||
|
|
||||
if (fs.existsSync(mainSrcPath)) { |
|
||||
fs.copyFileSync(mainSrcPath, mainDestPath); |
|
||||
console.log('Copied main process file successfully'); |
|
||||
} else { |
|
||||
console.error('Main process file not found at:', mainSrcPath); |
|
||||
process.exit(1); |
|
||||
} |
|
||||
|
|
||||
console.log('Electron build process completed successfully'); |
console.log("Electron index.html copied and patched for Electron context."); |
||||
|
@ -0,0 +1,21 @@ |
|||||
|
import { defineConfig } from 'vite'; |
||||
|
import vue from '@vitejs/plugin-vue'; |
||||
|
import path from 'path'; |
||||
|
|
||||
|
export default defineConfig({ |
||||
|
root: path.resolve(__dirname, '.'), |
||||
|
base: './', |
||||
|
build: { |
||||
|
outDir: path.resolve(__dirname, 'dist-electron/www'), |
||||
|
emptyOutDir: false, |
||||
|
rollupOptions: { |
||||
|
input: path.resolve(__dirname, 'dist/www/index.html'), |
||||
|
}, |
||||
|
}, |
||||
|
plugins: [vue()], |
||||
|
resolve: { |
||||
|
alias: { |
||||
|
'@': path.resolve(__dirname, 'src'), |
||||
|
}, |
||||
|
}, |
||||
|
}); |
Loading…
Reference in new issue