I want to run an Angular app with Electron based on this example on my Raspberry Pi. The app runs well on my PC with npm start but it cannot resolve assets on the Pi.
The assets are defined in a theme.scss file in the src/ directory:
$body-background-color: white;
$body-background-image: url("/assets/images/bg-hexa-gray-flat.png");
$body-background-position: top left;
$body-background-repeat: repeat;
$sidepanel-background-color: white;
$sidepanel-background-image: url("/assets/images/bg-hexa-red-flat.png");
$sidepanel-background-position: top left;
$sidepanel-background-repeat: repeat;
$sidepanel-logo: url("/assets/svg/logo.svg");
$sidepanel-logo-white: url("/assets/svg/white.svg");
and this file is imported in most of my components.
My main.ts file:
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import * as url from 'url';
let win: BrowserWindow = null;
const serve = process.argv.slice(1).some(val => val === '--serve');
const createWindow = async () => {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 480,
icon: path.join(__dirname, 'src/assets/images/icon.png'),
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: (serve) ? true : false,
},
});
if (serve) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
win.loadURL('http://localhost:4200');
} else {
win.setFullScreen(true);
win.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
}
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
return win;
};
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => app.quit());
} catch (e) {
// Catch Error
// throw e;
}
The issue is that with serve on my PC, the /assets path is relative to the root of the app (http://localhost:4200/assets/...) and it works fine but in prod I get an error file:///assets/... not found. If I change the paths in the CSS of the bundle on my Pi from /assets/... to assets/... it works. If I change it accordingly in the theme.scss file, it doesn't compile because the path is relative and theme.scss is imported in many components at different levels of the files tree.
I've already tried paths like ~/assets/... but it is compiled as /./assets/... and that makes no sense to me...
How can I make /assets become assets/ in the CSS bundle or make assets/ be resolved as relative to the app root at compile time?
Edit:
Resolution of assets in url() is an old issue of Angular.