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