fix(electron): app loads
This commit is contained in:
@@ -184,6 +184,10 @@
|
||||
{
|
||||
"from": "dist-electron/www",
|
||||
"to": "www"
|
||||
},
|
||||
{
|
||||
"from": "dist-electron/resources/preload.js",
|
||||
"to": "preload.js"
|
||||
}
|
||||
],
|
||||
"linux": {
|
||||
|
||||
@@ -47,4 +47,21 @@ indexContent = indexContent.replace(
|
||||
// Write the modified index.html to dist-electron/www
|
||||
fs.writeFileSync(finalIndexPath, indexContent);
|
||||
|
||||
// Copy preload script to resources
|
||||
const preloadSrc = path.join(electronDistPath, "preload.js");
|
||||
const preloadDest = path.join(electronDistPath, "resources", "preload.js");
|
||||
|
||||
// Ensure resources directory exists
|
||||
const resourcesDir = path.join(electronDistPath, "resources");
|
||||
if (!fs.existsSync(resourcesDir)) {
|
||||
fs.mkdirSync(resourcesDir, { recursive: true });
|
||||
}
|
||||
|
||||
if (fs.existsSync(preloadSrc)) {
|
||||
fs.copyFileSync(preloadSrc, preloadDest);
|
||||
console.log("Preload script copied to resources directory");
|
||||
} else {
|
||||
console.error("Preload script not found at:", preloadSrc);
|
||||
}
|
||||
|
||||
console.log("Electron index.html copied and patched for Electron context.");
|
||||
|
||||
@@ -20,16 +20,20 @@ const logger = {
|
||||
const isDev = process.argv.includes("--inspect");
|
||||
|
||||
function createWindow(): void {
|
||||
// Add before createWindow function
|
||||
const preloadPath = path.join(__dirname, "preload.js");
|
||||
logger.log("Checking preload path:", preloadPath);
|
||||
logger.log("Preload exists:", fs.existsSync(preloadPath));
|
||||
// Resolve preload path based on environment
|
||||
const preloadPath = app.isPackaged
|
||||
? path.join(process.resourcesPath, "preload.js")
|
||||
: path.join(__dirname, "preload.js");
|
||||
|
||||
logger.log("[Electron] Preload path:", preloadPath);
|
||||
logger.log("[Electron] Preload exists:", fs.existsSync(preloadPath));
|
||||
|
||||
// Log environment and paths
|
||||
logger.log("process.cwd():", process.cwd());
|
||||
logger.log("__dirname:", __dirname);
|
||||
logger.log("app.getAppPath():", app.getAppPath());
|
||||
logger.log("app.isPackaged:", app.isPackaged);
|
||||
logger.log("[Electron] process.cwd():", process.cwd());
|
||||
logger.log("[Electron] __dirname:", __dirname);
|
||||
logger.log("[Electron] app.getAppPath():", app.getAppPath());
|
||||
logger.log("[Electron] app.isPackaged:", app.isPackaged);
|
||||
logger.log("[Electron] process.resourcesPath:", process.resourcesPath);
|
||||
|
||||
// List files in __dirname and __dirname/www
|
||||
try {
|
||||
@@ -53,7 +57,7 @@ function createWindow(): void {
|
||||
contextIsolation: true,
|
||||
webSecurity: true,
|
||||
allowRunningInsecureContent: false,
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
preload: preloadPath, // Use the resolved preload path
|
||||
},
|
||||
});
|
||||
|
||||
@@ -66,33 +70,36 @@ function createWindow(): void {
|
||||
urls: [
|
||||
"file://*/*/assets/*",
|
||||
"file://*/assets/*",
|
||||
"file:///assets/*", // Catch absolute paths
|
||||
"<all_urls>", // Catch all URLs as a fallback
|
||||
"file:///assets/*",
|
||||
"<all_urls>",
|
||||
],
|
||||
},
|
||||
(details, callback) => {
|
||||
let url = details.url;
|
||||
logger.debug("[Electron] Intercepted URL:", url);
|
||||
|
||||
// Get the base directory for assets
|
||||
const baseDir = app.isPackaged
|
||||
? `file://${process.resourcesPath}`
|
||||
: `file://${__dirname}`;
|
||||
|
||||
// Handle paths that don't start with file://
|
||||
if (!url.startsWith("file://") && url.includes("/assets/")) {
|
||||
url = `file://${path.join(__dirname, "www", url)}`;
|
||||
url = `${baseDir}/www${url}`;
|
||||
logger.debug("[Electron] Rewritten non-file URL to:", url);
|
||||
}
|
||||
|
||||
// Handle absolute paths starting with /assets/
|
||||
if (url.includes("/assets/") && !url.includes("/www/assets/")) {
|
||||
const baseDir = url.includes("dist-electron")
|
||||
? url.substring(
|
||||
0,
|
||||
url.indexOf("/dist-electron") + "/dist-electron".length,
|
||||
)
|
||||
: `file://${__dirname}`;
|
||||
const assetPath = url.split("/assets/")[1];
|
||||
const newUrl = `${baseDir}/www/assets/${assetPath}`;
|
||||
logger.debug("[Electron] Rewritten asset URL to:", newUrl);
|
||||
callback({ redirectURL: newUrl });
|
||||
return;
|
||||
}
|
||||
|
||||
callback({}); // No redirect for other URLs
|
||||
logger.debug("[Electron] No rewrite needed for URL:", url);
|
||||
callback({});
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user