Skip to content

Commit

Permalink
improve bundling and asset resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
iamEvanYT committed Dec 8, 2024
1 parent 3d41965 commit 09628c7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 25 deletions.
9 changes: 9 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@electron-forge/plugin-fuses": "^7.6.0",
"@electron-forge/plugin-webpack": "^7.6.0",
"@electron/fuses": "^1.8.0",
"@types/mime-types": "^2.1.4",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@vercel/webpack-asset-relocator-loader": "^1.7.3",
Expand All @@ -34,6 +35,7 @@
"eslint": "^8.57.1",
"eslint-plugin-import": "^2.31.0",
"fork-ts-checker-webpack-plugin": "^7.3.0",
"mime-types": "^2.1.35",
"node-loader": "^2.1.0",
"style-loader": "^3.3.4",
"ts-loader": "^9.5.1",
Expand Down
3 changes: 3 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { app } from "electron";
import ipc from "./ipc";
import { createOverlayWindow } from "./windows/overlay";
import { createSpotterWindow } from "./windows/spotter";
import { registerFileProtocol } from "./modules/protocols/file";

// single instance only
const gotLock = app.requestSingleInstanceLock();
Expand All @@ -22,6 +23,8 @@ if (!gotLock) {
app.on("ready", () => {
createSpotterWindow();
createOverlayWindow();

registerFileProtocol();
});

app.on("activate", () => {
Expand Down
42 changes: 42 additions & 0 deletions backend/src/modules/protocols/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { app, protocol } from "electron";
import * as path from "path";
import * as fs from "fs";
import * as mime from "mime-types";

export function registerFileProtocol() {
async function respondWithFile(filePath: string) {
const fileContent = await fs.promises.readFile(filePath);
const contentType = mime.lookup(filePath) || "application/octet-stream";

return new Response(fileContent, {
status: 200,
headers: { "Content-Type": contentType }
});
}

async function handleFileRequest(fileUrl: string): Promise<Response> {
const concantatedFilePath = path.join(app.getAppPath(), ".webpack", "renderer", fileUrl);

if (!fs.existsSync(concantatedFilePath)) {
return respondWithFile(fileUrl);
}

try {
return respondWithFile(concantatedFilePath);
} catch (error) {
console.error(`Error reading file: ${error.message}`);
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
headers: { "Content-Type": "application/json" }
});
}
}

protocol.handle("file", async (request) => {
const fileUrl = request.url.substring(7); // Remove the "file://" prefix
if (fileUrl.startsWith("/")) {
return handleFileRequest(fileUrl);
}
return respondWithFile(fileUrl);
});
}
21 changes: 1 addition & 20 deletions backend/webpack.renderer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,13 @@ export const rendererConfig: Configuration = {
plugins: [
new CopyPlugin({
patterns: [
// main (dev)
// main
{
from: "src/out",
to: ".",
globOptions: {
ignore: ["**/index.html"]
}
},
// spotter window (prod)
{
from: "src/out",
to: "./main_window",
globOptions: {
ignore: ["**/index.html"]
}
},
// overlay window (prod)
{
from: "src/out",
to: "./overlay_window",
globOptions: {
ignore: ["**/index.html"]
}
}
],
options: {
Expand All @@ -50,8 +34,5 @@ export const rendererConfig: Configuration = {
],
resolve: {
extensions: [".js", ".ts", ".jsx", ".tsx", ".css"]
},
output: {
publicPath: "./"
}
};
2 changes: 1 addition & 1 deletion frontend/components/spotter/elements/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const Icon = React.memo(function Icon({ src, width = 24, height = 24, cla
);
} else {
// Local asset case
const assetPath = src.startsWith("/") ? src : `./assets/${src}`;
const assetPath = src.startsWith("/") ? src : `/assets/${src}`;
return (
<div className={cn(className, "overflow-hidden")}>
<Image src={assetPath} className="object-contain" alt="Icon" width={widthPixels} height={heightPixels} />
Expand Down
8 changes: 4 additions & 4 deletions frontend/next.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextConfig } from "next";

const isDev = process.env.NODE_ENV === "development";
// const isDev = process.env.NODE_ENV === "development";

const nextConfig: NextConfig = {
output: "export",
Expand All @@ -12,8 +12,8 @@ const nextConfig: NextConfig = {
}
};

if (!isDev) {
nextConfig.assetPrefix = ".";
}
// if (!isDev) {
// nextConfig.assetPrefix = ".";
// }

export default nextConfig;

0 comments on commit 09628c7

Please sign in to comment.