fix builds

This commit is contained in:
Kesku 2025-06-23 13:02:32 +01:00
parent 2403232f48
commit 7f275162fd
4 changed files with 28 additions and 84 deletions

View File

@ -1,78 +0,0 @@
const fs = require('fs');
const path = require('path');
// Fix asset paths in generated HTML files for Electron
function fixAssetPaths(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
fixAssetPaths(filePath);
} else if (file.endsWith('.html')) {
let content = fs.readFileSync(filePath, 'utf8');
// Replace all absolute paths with relative paths
// CSS and JS files in attributes
content = content.replace(/href="\/_next\//g, 'href="./_next/');
content = content.replace(/src="\/_next\//g, 'src="./_next/');
// Images and icons
content = content.replace(/href="\/favicon/g, 'href="./favicon');
content = content.replace(/src="\/logo/g, 'src="./logo');
content = content.replace(/src="\/([^"]*\.(png|jpg|jpeg|gif|svg))/g, 'src="./$1');
// Fix paths in inline scripts and JSON data
content = content.replace(/"\/_next\//g, '"./_next/');
content = content.replace(/'\/_next\//g, "'./_next/");
// Fix other asset references
content = content.replace(/"\/static\//g, '"./static/');
content = content.replace(/'\/static\//g, "'./static/");
// Fix favicon in JSON data
content = content.replace(/"href":"\/favicon/g, '"href":"./favicon');
content = content.replace(/"src":"\/favicon/g, '"src":"./favicon');
// Fix any remaining standalone asset paths
content = content.replace(/href="\/([^"]*\.(css|js|woff2?|ttf|eot))/g, 'href="./$1"');
content = content.replace(/src="\/([^"]*\.(js|css|woff2?|ttf|eot))/g, 'src="./$1"');
// Fix paths in Next.js inline JSON data - more comprehensive
content = content.replace(/"static\/chunks\//g, '"_next/static/chunks/');
content = content.replace(/,\\"static\/chunks\//g, ',\\"_next/static/chunks/');
content = content.replace(/\[\"static\/chunks\//g, '["_next/static/chunks/');
// Fix favicon references in JSON metadata
content = content.replace(/"rel":"icon","href":"\/favicon/g, '"rel":"icon","href":"./favicon');
content = content.replace(/"href":"\/favicon\.ico"/g, '"href":"./favicon.ico"');
// Fix any script or link tags that might have been missed
content = content.replace(/<script[^>]+src="\/(?!http)/g, (match) => {
return match.replace('src="/', 'src="./');
});
content = content.replace(/<link[^>]+href="\/(?!http)/g, (match) => {
return match.replace('href="/', 'href="./');
});
// Fix paths in Next.js build manifest data
content = content.replace(/\\"\/([^"]*\.(js|css|woff2?|ttf|eot))\\"/g, '\\"./$1\\"');
content = content.replace(/\\"static\//g, '\\"_next/static/');
fs.writeFileSync(filePath, content);
console.log(`Fixed paths in ${filePath}`);
}
});
}
// Fix paths in the out directory
const outDir = path.join(__dirname, '../web/out');
if (fs.existsSync(outDir)) {
fixAssetPaths(outDir);
console.log('Asset path fixing complete!');
} else {
console.error('Out directory not found:', outDir);
}

View File

@ -79,10 +79,29 @@ function createWindow() {
// Register file protocol for better asset handling
app.whenReady().then(() => {
// Register a custom protocol to handle file loading
protocol.registerFileProtocol('file', (request, callback) => {
const pathname = decodeURI(request.url.replace('file:///', ''));
callback(pathname);
protocol.interceptFileProtocol('file', (request, callback) => {
try {
const url = new URL(request.url);
const pathname = url.pathname;
if (pathname.startsWith('/_next/') ||
pathname.startsWith('/favicon') ||
pathname.startsWith('/logo') ||
(pathname.endsWith('.txt') && url.search.includes('_rsc='))) {
const webDir = path.join(__dirname, 'web');
const assetPath = path.join(webDir, pathname);
console.log(`Redirecting ${pathname} to ${assetPath}`);
return callback({ path: assetPath });
}
const originalPath = url.pathname;
return callback({ path: originalPath });
} catch (err) {
console.error('Protocol intercept error:', err);
const fallbackPath = request.url.replace('file://', '');
callback({ path: fallbackPath });
}
});
createWindow();

View File

@ -1,13 +1,13 @@
{
"name": "opencut-desktop",
"version": "0.1.0",
"version": "0.3.0",
"main": "main.js",
"private": true,
"description": "Electron build for OpenCut",
"author": "OpenCut",
"scripts": {
"dev": "concurrently \"npm --prefix ../web run dev\" \"wait-on http://localhost:3000 && electron .\"",
"build": "cross-env ELECTRON_BUILD=true npm --prefix ../web run build && node fix-paths.js && electron-builder"
"build": "cross-env ELECTRON_BUILD=true npm --prefix ../web run build && electron-builder"
},
"devDependencies": {
"electron": "^36.5.0",

View File

@ -10,6 +10,9 @@ const nextConfig: NextConfig = {
? {
output: "export",
basePath: "",
images: {
unoptimized: true,
},
}
: {}),
};