Skip to content

A local-first personal finance app

:electron: Reduce electron package size (#3553)

Michael Clarkdf92c80c2719
diff --git a/packages/desktop-electron/bin/update-client b/packages/desktop-electron/bin/update-client
index 778b79386e..24da05f6fc 100755
--- a/packages/desktop-electron/bin/update-client
+++ b/packages/desktop-electron/bin/update-client
@@ -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
+
diff --git a/packages/desktop-electron/index.ts b/packages/desktop-electron/index.ts
index febd2e2cf1..d8471bb24b 100644
--- a/packages/desktop-electron/index.ts
+++ b/packages/desktop-electron/index.ts
@@ -1,5 +1,4 @@
import fs from 'fs';
-import Module from 'module';
import path from 'path';
import {
@@ -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
@@ -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()',
+ );
}
}
});
diff --git a/packages/desktop-electron/modules.d.ts b/packages/desktop-electron/modules.d.ts
index 7341f7749b..bde546bb8e 100644
--- a/packages/desktop-electron/modules.d.ts
+++ b/packages/desktop-electron/modules.d.ts
@@ -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 };
-}
diff --git a/packages/desktop-electron/package.json b/packages/desktop-electron/package.json
index 5733a9f00e..ad6d9e6cd0 100644
--- a/packages/desktop-electron/package.json
+++ b/packages/desktop-electron/package.json
@@ -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": {
@@ -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"
},
diff --git a/packages/desktop-electron/server.ts b/packages/desktop-electron/server.ts
index a49e14d409..1e6f836678 100644
--- a/packages/desktop-electron/server.ts
+++ b/packages/desktop-electron/server.ts
@@ -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;
diff --git a/packages/desktop-electron/window-state.ts b/packages/desktop-electron/window-state.ts
index 46b1a07712..cbcb365ef1 100644
--- a/packages/desktop-electron/window-state.ts
+++ b/packages/desktop-electron/window-state.ts
@@ -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');
@@ -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',
);
diff --git a/upcoming-release-notes/3553.md b/upcoming-release-notes/3553.md
new file mode 100644
index 0000000000..61e5497c34
--- /dev/null
+++ b/upcoming-release-notes/3553.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MikesGlitch]
+---
+
+Reducing Desktop app package size
diff --git a/yarn.lock b/yarn.lock
index 11aae8c778..ac760a1db1 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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"
@@ -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: