Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

📦 build(desktop): package only target-platform native prebuilds and drop .pdb (#19490)

Innei4af3510d8192
diff --git a/apps/desktop/module-deps.config.mjs b/apps/desktop/module-deps.config.mjs
index bbbb0074a6b..180feab7949 100644
--- a/apps/desktop/module-deps.config.mjs
+++ b/apps/desktop/module-deps.config.mjs
@@ -10,7 +10,7 @@ const sourceNodeModules = path.join(__dirname, 'node_modules');
* @param {string} moduleName - The module to resolve
* @param {Set<string>} visited - Set of already visited modules
* @param {string} nodeModulesPath - Path to node_modules directory
- * @param {{skipOptionalDependenciesFor?: Set<string>}} options - Dependency traversal options
+ * @param {{skipOptionalDependenciesFor?: Set<string>, targetPlatform?: string}} options - Dependency traversal options
* @returns {Set<string>} Set of all dependencies
*/
function resolveDependencies(
@@ -23,12 +23,16 @@ function resolveDependencies(
return visited;
}
+ const packageJsonPath = path.join(nodeModulesPath, moduleName, 'package.json');
+
+ if (!supportsPlatform(packageJsonPath, options.targetPlatform)) {
+ return visited;
+ }
+
// Always add the module name first. Workspace and optional platform modules
// may not be materialized locally, but they still need stable package rules.
visited.add(moduleName);
- const packageJsonPath = path.join(nodeModulesPath, moduleName, 'package.json');
-
if (!fs.existsSync(packageJsonPath)) {
return visited;
}
@@ -55,10 +59,21 @@ function resolveDependencies(
return visited;
}
+function supportsPlatform(packageJsonPath, targetPlatform) {
+ if (!targetPlatform || !fs.existsSync(packageJsonPath)) return true;
+
+ try {
+ const { os } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
+ return !Array.isArray(os) || os.includes(targetPlatform);
+ } catch {
+ return true;
+ }
+}
+
/**
* Get all transitive dependencies for a set of top-level modules.
* @param {string[]} modules
- * @param {{skipOptionalDependenciesFor?: Set<string>}} options
+ * @param {{skipOptionalDependenciesFor?: Set<string>, targetPlatform?: string}} options
* @returns {string[]}
*/
export function getDependenciesForModules(modules, options = {}) {
@@ -78,12 +93,12 @@ export function getDependenciesForModules(modules, options = {}) {
* Generate object-form electron-builder files config.
* Object form is required because pnpm symlinks are resolved before packaging.
* @param {string[]} modules
- * @param {{skipOptionalDependenciesFor?: Set<string>}} options
+ * @param {{skipOptionalDependenciesFor?: Set<string>, targetPlatform?: string}} options
* @returns {Array<{from: string, to: string, filter: string[]}>}
*/
export function getModuleFilesConfig(modules, options = {}) {
return getDependenciesForModules(modules, options).map((dep) => ({
- filter: ['**/*', '!**/*.map'],
+ filter: ['**/*', '!**/*.map', '!**/*.pdb'],
from: `node_modules/${dep}`,
to: `node_modules/${dep}`,
}));
@@ -94,7 +109,7 @@ export function getModuleFilesConfig(modules, options = {}) {
* electron-builder can include them via file rules.
* @param {string[]} modules
* @param {string} label
- * @param {{skipOptionalDependenciesFor?: Set<string>}} options
+ * @param {{skipOptionalDependenciesFor?: Set<string>, targetPlatform?: string}} options
*/
export async function copyModulesToSource(modules, label, options = {}) {
const deps = getDependenciesForModules(modules, options);
diff --git a/apps/desktop/native-deps.config.mjs b/apps/desktop/native-deps.config.mjs
index f7dba41d872..ca0f8781026 100644
--- a/apps/desktop/native-deps.config.mjs
+++ b/apps/desktop/native-deps.config.mjs
@@ -30,14 +30,18 @@ const configDir = path.dirname(fileURLToPath(import.meta.url));
function getTargetPlatform() {
return process.env.npm_config_platform || os.platform();
}
-const isDarwin = getTargetPlatform() === 'darwin';
+const targetPlatform = getTargetPlatform();
+const isDarwin = targetPlatform === 'darwin';
// The packaged macOS runtime invokes get-windows' native helper directly.
// Its optional dependencies are build/install tooling and the Windows loader
// chain, neither of which is required in a macOS application artifact.
-export const dependencyOptions = isDarwin
- ? { skipOptionalDependenciesFor: new Set(['get-windows']) }
- : {};
+// Platform prebuild packages (`@lydell/node-pty-win32-*`, `node-screenshots-linux-*`)
+// declare `os` in package.json; only the target platform's are packaged.
+export const dependencyOptions = {
+ targetPlatform,
+ ...(isDarwin ? { skipOptionalDependenciesFor: new Set(['get-windows']) } : {}),
+};
/**
* First-party native addon packages are discovered instead of being listed by