Platform abstraction and target configuration system for cross-platform Electron builds. Manages combinations of operating systems, architectures, and output formats.
Represents build platforms with static instances and target creation capabilities.
/**
* Platform abstraction for cross-platform builds
*/
class Platform {
/** macOS platform instance */
static readonly MAC: Platform;
/** Windows platform instance */
static readonly WINDOWS: Platform;
/** Linux platform instance */
static readonly LINUX: Platform;
/** Get current platform based on process.platform */
static current(): Platform;
/** Platform name string */
readonly name: string;
/** Node.js platform identifier */
readonly nodeName: NodeJS.Platform;
/** Create target map for this platform */
createTarget(type?: string | Array<string> | null, archs?: Array<Arch>): TargetMap;
/** Build target configuration for specific types and architectures */
buildConfigurationKey: string;
}
type TargetMap = Map<Platform, Map<Arch, Array<string>>>;Usage Examples:
import { Platform, Arch } from "electron-builder";
// Get current platform
const currentPlatform = Platform.current();
// Create targets for current platform
const targets = currentPlatform.createTarget();
// Create specific targets
const macTargets = Platform.MAC.createTarget(["dmg", "zip"], [Arch.x64, Arch.arm64]);
const winTargets = Platform.WINDOWS.createTarget("nsis", [Arch.x64]);
const linuxTargets = Platform.LINUX.createTarget(["AppImage", "deb"]);CPU architecture enumeration with conversion utilities.
/**
* Supported CPU architectures
*/
enum Arch {
ia32 = "ia32",
x64 = "x64",
armv7l = "armv7l",
arm64 = "arm64",
universal = "universal"
}
/**
* Convert architecture string to Arch enum
* @param archString - Architecture string (e.g., "x64", "arm64")
* @returns Arch enum value
*/
function archFromString(archString: string): Arch;
/**
* Get filename suffix for architecture
* @param arch - Architecture enum value
* @returns Architecture suffix for filenames
*/
function getArchSuffix(arch: Arch): string;
/**
* Convert arch to Linux-specific string
* @param arch - Architecture enum value
* @returns Linux architecture string
*/
function toLinuxArchString(arch: Arch): string;Usage Examples:
import { Arch, archFromString, getArchSuffix } from "electron-builder";
// Convert string to arch
const arch = archFromString("x64"); // Arch.x64
// Get filename suffix
const suffix = getArchSuffix(Arch.arm64); // "-arm64"
// Check architecture
if (arch === Arch.universal) {
console.log("Building universal binary");
}Utility functions for creating target configurations across platforms.
/**
* Create target map for specified platforms
* @param platforms - Array of platforms to build for
* @param type - Target type (e.g., "dmg", "nsis") or null for default
* @param arch - Architecture string, "all", or null for current
* @returns Target map for build configuration
*/
function createTargets(
platforms: Array<Platform>,
type?: string | null,
arch?: string | null
): TargetMap;Usage Examples:
import { createTargets, Platform } from "electron-builder";
// Create targets for all platforms with default types
const allPlatforms = createTargets([Platform.MAC, Platform.WINDOWS, Platform.LINUX]);
// Create DMG targets for macOS on all architectures
const dmgTargets = createTargets([Platform.MAC], "dmg", "all");
// Create NSIS targets for Windows x64
const nsisTargets = createTargets([Platform.WINDOWS], "nsis", "x64");Interfaces for configuring individual build targets.
/**
* Configuration for individual build target
*/
interface TargetConfiguration {
/** Target type name (e.g., "dmg", "nsis", "AppImage") */
target: string;
/** Target architectures */
arch?: Array<Arch> | string | Arch | null;
}
/**
* Base interface for target-specific options
*/
interface TargetSpecificOptions {
/** Artifact file name template */
artifactName?: string | null;
/** Publish configuration for this target */
publish?: Array<PublishConfiguration> | PublishConfiguration | string | null;
}
/**
* Union type for target configuration variations
*/
type TargetConfigType = string | TargetConfiguration | Array<string | TargetConfiguration>;Usage Examples:
import { TargetConfiguration, Arch } from "electron-builder";
// Simple target configurations
const simpleTargets: Array<string> = ["dmg", "zip"];
// Detailed target configurations
const detailedTargets: Array<TargetConfiguration> = [
{
target: "dmg",
arch: [Arch.x64, Arch.arm64]
},
{
target: "nsis",
arch: Arch.x64
}
];
// Mixed configuration
const mixedTargets: TargetConfigType = [
"zip",
{ target: "dmg", arch: [Arch.x64, Arch.arm64] }
];Base class for all platform-specific build targets.
/**
* Abstract base class for build targets
*/
abstract class Target {
/** Target name (e.g., "dmg", "nsis") */
readonly name: string;
/** Platform packager instance */
readonly packager: PlatformPackager;
/** Output directory for this target */
readonly outDir: string;
/** Target options */
readonly options: TargetSpecificOptions;
constructor(name: string, packager: PlatformPackager, outDir: string);
/** Build the target and return artifact paths */
abstract build(appOutDir: string, arch: Arch): Promise<any>;
/** Finalize target after build */
finishBuild(): Promise<any>;
}Context information passed to build targets and hooks.
/**
* Context provided to before-build hooks
*/
interface BeforeBuildContext {
/** Application information */
appInfo: AppInfo;
/** Platform being built */
platform: Platform;
/** Architecture being built */
arch: Arch;
/** Electron version */
electronVersion: string;
}
/**
* Source repository information
*/
interface SourceRepositoryInfo {
/** Repository type (e.g., "git") */
type?: string;
/** Repository domain */
domain?: string;
/** Repository user/organization */
user: string;
/** Repository project name */
project: string;
}Pre-defined target constants for common use cases.
/** Default target identifier */
const DEFAULT_TARGET = "default";
/** Directory target identifier (unpacked build) */
const DIR_TARGET = "dir";
/** Compression levels for archives */
type CompressionLevel = "store" | "normal" | "maximum";Usage Examples:
import { DEFAULT_TARGET, DIR_TARGET, Platform } from "electron-builder";
// Use default targets for platform
const defaultTargets = Platform.current().createTarget(DEFAULT_TARGET);
// Create unpacked directory build
const dirTargets = Platform.current().createTarget(DIR_TARGET);
// Build with compression
await build({
config: {
compression: "maximum" as CompressionLevel
}
});The platform and target system provides flexible cross-platform build configuration while maintaining type safety and intuitive APIs for common build scenarios.