Skip to content

Commit

Permalink
:electron: Reduce electron package size (actualbudget#3553)
Browse files Browse the repository at this point in the history
* remove unneeded loot core ref and keeping better-sqlite external

* release notes

* putting the package config back

* cleaning up

* add missing db files

* updates

* window state updates
  • Loading branch information
MikesGlitch authored Oct 4, 2024
1 parent d0caf9f commit df92c80
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 35 deletions.
8 changes: 8 additions & 0 deletions packages/desktop-electron/bin/update-client
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ rm -rf ${ROOT}/build/client-build/data
rm -rf ${ROOT}/build/client-build/*kcab.*
rm -rf ${ROOT}/build/client-build/*.wasm
rm -rf ${ROOT}/build/client-build/*.map

# Copy loot-core into build directory
mkdir -p ${ROOT}/build/loot-core/lib-dist
ls ${ROOT}/build/loot-core/lib-dist
cp ${ROOT}/../loot-core/lib-dist/bundle.desktop.js ${ROOT}/build/loot-core/lib-dist/bundle.desktop.js
cp ${ROOT}/../loot-core/default-db.sqlite ${ROOT}/build/loot-core/default-db.sqlite
cp -r ${ROOT}/../loot-core/migrations ${ROOT}/build/loot-core/migrations

9 changes: 6 additions & 3 deletions packages/desktop-electron/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'fs';
import Module from 'module';
import path from 'path';

import {
Expand Down Expand Up @@ -28,7 +27,9 @@ import {

import './security';

Module.globalPaths.push(__dirname + '/..');
process.env.lootCoreScript = isDev
? 'loot-core/lib-dist/bundle.desktop.js' // serve from local output in development (provides hot-reloading)
: path.resolve(__dirname, 'loot-core/lib-dist/bundle.desktop.js'); // serve from build in production

// This allows relative URLs to be resolved to app:// which makes
// local assets load correctly
Expand Down Expand Up @@ -143,7 +144,9 @@ async function createWindow() {
if (clientWin) {
const url = clientWin.webContents.getURL();
if (url.includes('app://') || url.includes('localhost:')) {
clientWin.webContents.executeJavaScript('__actionsForMenu.focused()');
clientWin.webContents.executeJavaScript(
'window.__actionsForMenu.focused()',
);
}
}
});
Expand Down
6 changes: 0 additions & 6 deletions packages/desktop-electron/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
declare module 'module' {
const globalPaths: string[];
}

// bundles not available until we build them
declare module 'loot-core/lib-dist/bundle.desktop.js' {
const initApp: (isDev: boolean) => void;
const lib: { getDataDir: () => string };
}
9 changes: 3 additions & 6 deletions packages/desktop-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
"appId": "com.actualbudget.actual",
"files": [
"build",
"!node_modules/loot-core/src{,/**/*}",
"!node_modules/loot-core/lib-dist/{browser,bundle.mobile*}",
"!node_modules/absurd-sql",
"!node_modules/better-sqlite3/{benchmark,src,bin,docs,deps,build/Release/obj,build/Release/sqlite3.a,build/Release/test_extension.node}",
"!**/*.js.map",
"!**/stats.json",
"!node_modules/@jlongster/sql.js/**/*",
"!build/client-build/sql-wasm.wasm"
"!build/client-build/sql-wasm.wasm",
"!build/loot-core/lib-dist/{browser,bundle.mobile*}"
],
"beforePack": "./build/beforePackHook.js",
"mac": {
Expand Down Expand Up @@ -88,9 +85,9 @@
"npmRebuild": false
},
"dependencies": {
"better-sqlite3": "^9.6.0",
"electron-is-dev": "2.0.0",
"electron-log": "4.4.8",
"loot-core": "*",
"node-fetch": "^2.7.0",
"promise-retry": "^2.0.1"
},
Expand Down
13 changes: 7 additions & 6 deletions packages/desktop-electron/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Module from 'module';

// @ts-strict-ignore
import fetch from 'node-fetch';

Module.globalPaths.push(__dirname + '/..');
global.fetch = fetch;

const lazyLoadBackend = async (isDev: boolean) => {
// eslint-disable-next-line import/extensions
const bundle = await import('loot-core/lib-dist/bundle.desktop.js');
bundle.initApp(isDev);
try {
const bundle = await import(process.env.lootCoreScript);
bundle.initApp(isDev);
} catch (error) {
console.error('Failed to init the server bundle:', error);
throw new Error(`Failed to init the server bundle: ${error.message}`);
}
};

const isDev = false;
Expand Down
22 changes: 10 additions & 12 deletions packages/desktop-electron/window-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import path from 'path';

import electron, { BrowserWindow } from 'electron';

const backend = undefined;
const getBackend = async () =>
// eslint-disable-next-line import/extensions
backend || (await import('loot-core/lib-dist/bundle.desktop.js'));

type WindowState = Electron.Rectangle & {
isMaximized?: boolean;
isFullScreen?: boolean;
displayBounds?: Electron.Rectangle;
};

const getDataDir = () => {
if (!process.env.ACTUAL_DATA_DIR) {
throw new Error('ACTUAL_DATA_DIR is not set');
}

return process.env.ACTUAL_DATA_DIR;
};

async function loadState() {
let state: WindowState | undefined = undefined;
const backend = await getBackend();
try {
state = JSON.parse(
fs.readFileSync(
path.join(backend.lib.getDataDir(), 'window.json'),
'utf8',
),
fs.readFileSync(path.join(getDataDir(), 'window.json'), 'utf8'),
);
} catch (e) {
console.log('Could not load window state');
Expand All @@ -47,10 +46,9 @@ function updateState(win: BrowserWindow, state: WindowState) {
}

async function saveState(win: BrowserWindow, state: WindowState) {
const backend = await getBackend();
updateState(win, state);
fs.writeFileSync(
path.join(backend.lib.getDataDir(), 'window.json'),
path.join(getDataDir(), 'window.json'),
JSON.stringify(state),
'utf8',
);
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3553.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Reducing Desktop app package size
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8412,13 +8412,13 @@ __metadata:
"@electron/notarize": "npm:2.4.0"
"@electron/rebuild": "npm:3.6.0"
"@types/copyfiles": "npm:^2"
better-sqlite3: "npm:^9.6.0"
copyfiles: "npm:^2.4.1"
cross-env: "npm:^7.0.3"
electron: "npm:30.0.6"
electron-builder: "npm:24.13.3"
electron-is-dev: "npm:2.0.0"
electron-log: "npm:4.4.8"
loot-core: "npm:*"
node-fetch: "npm:^2.7.0"
promise-retry: "npm:^2.0.1"
typescript: "npm:^5.5.4"
Expand Down Expand Up @@ -13111,7 +13111,7 @@ __metadata:
languageName: node
linkType: hard

"loot-core@npm:*, loot-core@workspace:packages/loot-core":
"loot-core@workspace:packages/loot-core":
version: 0.0.0-use.local
resolution: "loot-core@workspace:packages/loot-core"
dependencies:
Expand Down

0 comments on commit df92c80

Please sign in to comment.