Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Clean node modules in desktop packagin

Hakan Shehu75cb80be5036
diff --git a/apps/desktop/forge.config.ts b/apps/desktop/forge.config.ts
index 271e08e8..51707efe 100644
--- a/apps/desktop/forge.config.ts
+++ b/apps/desktop/forge.config.ts
@@ -5,6 +5,7 @@ import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { VitePlugin } from '@electron-forge/plugin-vite';
import type { ForgeConfig } from '@electron-forge/shared-types';
+import path from 'path';
import fs from 'fs/promises';
const config: ForgeConfig = {
@@ -28,7 +29,16 @@ const config: ForgeConfig = {
return false;
}
- if (path.startsWith('/node_modules')) {
+ if (path === '/node_modules') {
+ return false;
+ }
+
+ // there are the only native modules that are needed in the main process as external dependencies
+ if (
+ path.startsWith('/node_modules/better-sqlite3') ||
+ path.startsWith('/node_modules/bindings') ||
+ path.startsWith('/node_modules/file-uri-to-path')
+ ) {
return false;
}
@@ -131,9 +141,76 @@ const config: ForgeConfig = {
});
},
postPackage: async () => {
- // remove the node_modules directory
+ // Remove the node_modules directory
await fs.rm('./node_modules', { recursive: true, force: true });
},
+ packageAfterPrune: async (_, buildPath) => {
+ // Remove empty node_modules folders that are left behind
+ // after the package and prune process. We also delete all non-necessary
+ // files, for example md files, license files, development config files etc.
+ const nodeModulesPath = path.join(buildPath, 'node_modules');
+ const nodeModules = await fs.readdir(nodeModulesPath, {
+ withFileTypes: true,
+ });
+
+ for (const nodeModule of nodeModules) {
+ if (nodeModule.isDirectory() && nodeModule.name !== 'node_modules') {
+ const files = await fs.readdir(
+ path.join(nodeModulesPath, nodeModule.name),
+ {
+ withFileTypes: true,
+ }
+ );
+
+ if (files.length === 0) {
+ await fs.rmdir(path.join(nodeModulesPath, nodeModule.name));
+ } else {
+ for (const file of files) {
+ if (!file.isFile()) {
+ continue;
+ }
+
+ const shouldDelete =
+ file.name === 'LICENSE' ||
+ file.name.endsWith('.md') ||
+ file.name.endsWith('.txt') ||
+ file.name.endsWith('.lock') ||
+ file.name.endsWith('.yaml') ||
+ file.name.endsWith('.yml') ||
+ file.name.endsWith('.gitignore') ||
+ file.name.endsWith('.npmignore') ||
+ file.name.endsWith('.npmrc') ||
+ file.name.endsWith('.npm');
+
+ if (shouldDelete) {
+ await fs.rm(
+ path.join(nodeModulesPath, nodeModule.name, file.name)
+ );
+ }
+ }
+ }
+ }
+ }
+
+ // Clean up the package.json file and remove all fields that are not needed
+ const packageJsonPath = path.join(buildPath, 'package.json');
+ const packageJson = await fs.readFile(packageJsonPath, 'utf-8');
+ const content = JSON.parse(packageJson);
+ const allowedKeys = [
+ 'name',
+ 'productName',
+ 'version',
+ 'description',
+ 'main',
+ 'author',
+ 'license',
+ ];
+
+ const result = Object.fromEntries(
+ Object.entries(content).filter(([key]) => allowedKeys.includes(key))
+ );
+ await fs.writeFile(packageJsonPath, JSON.stringify(result, null, 2));
+ },
},
};
diff --git a/apps/desktop/vite.base.config.ts b/apps/desktop/vite.base.config.ts
index c9bbcbdd..218e92cb 100644
--- a/apps/desktop/vite.base.config.ts
+++ b/apps/desktop/vite.base.config.ts
@@ -4,19 +4,14 @@ import { builtinModules } from 'node:module';
import type { AddressInfo } from 'node:net';
import path from 'path';
-import pkg from './package.json';
-
export const builtins = [
'electron',
...builtinModules.map((m) => [m, `node:${m}`]).flat(),
];
-export const external = [
- ...builtins,
- ...Object.keys(
- 'dependencies' in pkg ? (pkg.dependencies as Record<string, unknown>) : {}
- ).filter((key) => !key.startsWith('@colanode')),
-];
+const nativeModules = ['better-sqlite3'];
+
+export const external = [...builtins, ...nativeModules];
export function getBuildConfig(env: ConfigEnv<'build'>): UserConfig {
const { root, mode, command } = env;