Core build functionality for packaging Electron applications into distributable formats. The build system orchestrates the complete pipeline from source code to platform-specific installers.
Main entry point for building Electron applications with comprehensive configuration options.
/**
* Build Electron application with specified configuration and targets
* @param options - Combined packager and publish options
* @param packager - Optional custom packager instance
* @returns Promise resolving to array of built artifact paths
*/
function build(options: PackagerOptions & PublishOptions, packager?: Packager): Promise<Array<string>>;
interface PackagerOptions {
/** Target platform and architecture mapping */
targets?: TargetMap;
/** macOS target types (e.g., ["dmg", "mas"]) */
mac?: Array<string>;
/** Windows target types (e.g., ["nsis", "portable"]) */
win?: Array<string>;
/** Linux target types (e.g., ["AppImage", "deb"]) */
linux?: Array<string>;
/** Configuration object, file path, or null for auto-detection */
config?: Configuration | string | null;
/** Project directory path (defaults to current working directory) */
projectDir?: string | null;
/** Path to prepackaged app directory */
prepackaged?: string | null;
/** Platform-specific packager factory for custom implementations */
platformPackagerFactory?: (info: BuildInfo, platform: Platform, cleanupTasks: Array<() => Promise<any>>) => PlatformPackager;
/** Custom configuration computed callback */
effectiveOptionComputed?: (options: any) => boolean;
}
interface PublishOptions {
/** Publishing policy determining when to publish artifacts */
publish?: PublishPolicy | null;
}
type PublishPolicy = "onTag" | "onTagOrDraft" | "always" | "never";
type TargetMap = Map<Platform, Map<Arch, Array<string>>>;Usage Examples:
import { build, Platform, Arch } from "electron-builder";
// Basic build for current platform
const artifactPaths = await build({
config: {
appId: "com.example.myapp",
productName: "My App"
}
});
// Cross-platform build with specific targets
await build({
targets: new Map([
[Platform.MAC, new Map([[Arch.x64, ["dmg"]], [Arch.arm64, ["dmg"]]])],
[Platform.WINDOWS, new Map([[Arch.x64, ["nsis", "zip"]]])],
[Platform.LINUX, new Map([[Arch.x64, ["AppImage", "deb"]]])]
])
});
// Build with publishing
await build({
config: "./electron-builder.json",
publish: "onTagOrDraft"
});Central orchestrator managing the build process across platforms and targets.
/**
* Main packager class coordinating the build process
*/
class Packager {
constructor(options: PackagerOptions, cancellationToken?: CancellationToken);
/** Application information container */
readonly appInfo: AppInfo;
/** Build configuration */
readonly config: Configuration;
/** Cancellation token for stopping builds */
readonly cancellationToken: CancellationToken;
/** Project directory path */
readonly projectDir: string;
/** Build output directory */
readonly outDir: string;
/** Execute the build process */
build(): Promise<BuildResult>;
/** Validate configuration before building */
validateConfig(): Promise<void>;
/** Get platform-specific packager instance */
createHelper(platform: Platform): PlatformPackager;
/** Clean up build resources */
clearPackagerEventListeners(): void;
}
interface BuildResult {
/** Paths to all generated artifacts */
artifactPaths: Array<string>;
/** Platform to target mapping used in build */
platformToTargets: Map<Platform, Map<String, Target>>;
/** Output directory path */
outDir: string;
/** Final resolved configuration */
configuration: Configuration;
}Command-line interface options extending core packager functionality.
/**
* CLI-specific options with architecture flags
*/
interface CliOptions extends PackagerOptions, PublishOptions {
/** Build for x64 architecture */
x64?: boolean;
/** Build for ia32 architecture */
ia32?: boolean;
/** Build for armv7l architecture */
armv7l?: boolean;
/** Build for arm64 architecture */
arm64?: boolean;
/** Build universal binary (macOS) */
universal?: boolean;
/** Build unpacked directory only */
dir?: boolean;
}
/**
* Normalize CLI options to build options
* @param args - CLI arguments and flags
* @returns Normalized build options
*/
function normalizeOptions(args: CliOptions): BuildOptions;
interface BuildOptions extends PackagerOptions, PublishOptions {}Container for parsed application metadata used throughout the build process.
/**
* Application information parsed from package.json and configuration
*/
class AppInfo {
constructor(packager: Packager, buildVersion?: string | null);
/** Application ID (e.g., com.example.myapp) */
readonly id: string;
/** Product name for display */
readonly productName: string;
/** Application name (package.json name) */
readonly name: string;
/** Application version */
readonly version: string;
/** Build version (different from app version) */
readonly buildVersion: string;
/** Application description */
readonly description: string;
/** Author information */
readonly companyName: string;
/** Application executable name */
readonly productFilename: string;
/** Update channel for auto-updater */
readonly channel: string | null;
/** Application type/framework */
readonly type: string;
}Event-driven build process with hooks for customization.
/**
* Artifact creation event data
*/
interface ArtifactCreated {
/** Target that created the artifact */
readonly target: Target;
/** Path to created artifact file */
readonly file: string;
/** Artifact architecture */
readonly arch: Arch | null;
/** Packager instance */
readonly packager: PlatformPackager;
/** Publish configuration (if publishing) */
readonly publishConfig?: PublishConfiguration;
/** Safe artifact name for publishing */
readonly safeArtifactName?: string;
/** Update information */
readonly updateInfo?: any;
}
/**
* Build start event data
*/
interface ArtifactBuildStarted {
/** Target being built */
readonly target: Target;
/** Target architecture */
readonly arch: Arch;
/** Packager instance */
readonly packager: PlatformPackager;
}Build-specific error types and validation.
/**
* Validate build request options
* @param options - Combined packager and publish options
* @throws InvalidConfigurationError for invalid options
*/
function checkBuildRequestOptions(options: PackagerOptions & PublishOptions): void;
/**
* Configuration validation error
*/
class InvalidConfigurationError extends Error {
constructor(message: string, fileName?: string);
}
/**
* Process execution error
*/
class ExecError extends Error {
readonly alreadyLogged: boolean;
readonly exitCode: number;
}Usage Examples:
import { build, InvalidConfigurationError, ExecError } from "electron-builder";
try {
await build({
config: {
appId: "com.example.myapp",
productName: "My App"
}
});
} catch (error) {
if (error instanceof InvalidConfigurationError) {
console.error("Configuration error:", error.message);
} else if (error instanceof ExecError) {
console.error("Build process failed:", error.message);
console.error("Exit code:", error.exitCode);
} else {
console.error("Unexpected error:", error);
}
}The build system provides comprehensive control over the Electron packaging process while maintaining sensible defaults for common use cases.