Fix asset loading for production build

This commit is contained in:
Hakan Shehu
2024-10-26 23:11:37 +02:00
parent 302a2a1ebe
commit e1ff6440cf
9 changed files with 40 additions and 7 deletions

View File

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@@ -10,9 +10,28 @@ import { FuseV1Options, FuseVersion } from '@electron/fuses';
const config: ForgeConfig = {
packagerConfig: {
asar: true,
ignore: [
/^\/src/,
/^\/test/,
/^\/tools/,
/^\/release/,
/^\/docs/,
/\.git/,
/\.vscode/,
/\.idea/,
/^\/\.env/,
// Don't ignore node_modules
// /^\/node_modules/
],
extraResource: ['assets'],
},
rebuildConfig: {},
makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
makers: [
new MakerSquirrel({}),
new MakerZIP({}, ['darwin']),
new MakerRpm({}),
new MakerDeb({}),
],
plugins: [
new VitePlugin({
// `build` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.

View File

@@ -5,7 +5,7 @@
<title>Neuron</title>
<meta
http-equiv="Content-Security-Policy"
content="script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: avatar: local-file:; "
content="default-src 'self' 'unsafe-inline' data: avatar: local-file: asset:;"
/>
</head>
<body>

View File

@@ -30,7 +30,7 @@ export const getEmojiById = (id: string): Emoji | undefined => {
};
export const getEmojiUrl = (id: string): string => {
return `/assets/emojis/${id}.svg`;
return `asset://emojis/${id}.svg`;
};
export const searchEmojis = (query: string): Emoji[] => {

View File

@@ -17,7 +17,7 @@ type IconPickerEmojiRow = {
};
export const getIconUrl = (id: string): string => {
return `/assets/icons/${id}.svg`;
return `asset://icons/${id}.svg`;
};
export const searchIcons = (query: string): Icon[] => {

View File

@@ -1,4 +1,4 @@
import { app, ipcMain, BrowserWindow, protocol, dialog } from 'electron';
import { app, ipcMain, BrowserWindow, protocol, dialog, net } from 'electron';
import path from 'path';
import { eventBus } from '@/lib/event-bus';
import { MutationInput, MutationMap } from '@/operations/mutations';
@@ -53,6 +53,20 @@ const createWindow = async () => {
protocol.handle('local-file', (request) => {
return fileManager.handleFileRequest(request);
});
protocol.handle('asset', (request) => {
const url = request.url.replace('asset://', '');
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
return net.fetch(`${MAIN_WINDOW_VITE_DEV_SERVER_URL}/assets/${url}`);
}
const localFileUrl = `file://${path.join(
process.resourcesPath,
'assets',
url,
)}`;
return net.fetch(localFileUrl);
});
};
// This method will be called when Electron has finished

View File

@@ -18,7 +18,7 @@ const Icon = React.forwardRef<SVGSVGElement, IconProps>((props, ref) => {
width={computedSize}
{...props}
>
<use xlinkHref={`/assets/icons.svg#ri-${props.name}`} />
<use href={`asset://icons/sprite.svg#ri-${props.name}`} />
</svg>
);
});

View File

@@ -86,7 +86,7 @@
@font-face {
font-family: 'neotrax';
src: url('/assets/neotrax.otf') format('truetype');
src: url('asset://fonts/neotrax.otf') format('truetype');
font-weight: normal;
font-style: normal;
}