Skip to content

Commit 285aa76

Browse files
authored
fix: use electron-rebuilder API directly so as to override the platform for cross-platform prebuild compilations (#7629)
1 parent 98f535e commit 285aa76

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

.changeset/forty-ways-pay.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"app-builder-lib": patch
3+
"electron-builder": patch
4+
---
5+
6+
fix: use electron/rebuild Rebuilder directly for cross-platform builds

packages/app-builder-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@develar/schema-utils": "~2.6.5",
5151
"@electron/notarize": "^1.2.3",
5252
"@electron/osx-sign": "^1.0.4",
53-
"@electron/rebuild": "^3.2.13",
53+
"@electron/rebuild": "3.2.13",
5454
"@electron/universal": "1.3.4",
5555
"@malept/flatpak-bundler": "^0.4.0",
5656
"@types/fs-extra": "9.0.13",

packages/app-builder-lib/src/packager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ export class Packager {
495495
const frameworkInfo = { version: this.framework.version, useCustomDist: true }
496496
const config = this.config
497497
if (config.nodeGypRebuild === true) {
498-
await nodeGypRebuild(frameworkInfo, arch)
498+
await nodeGypRebuild(frameworkInfo, arch, platform)
499499
}
500500

501501
if (config.npmRebuild === false) {

packages/app-builder-lib/src/util/yarn.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { pathExists } from "fs-extra"
33
import { homedir } from "os"
44
import * as path from "path"
55
import { Configuration } from "../configuration"
6-
import * as electronRebuild from "@electron/rebuild"
6+
import * as electronRebuild from "@electron/rebuild/lib/rebuild"
77
import * as searchModule from "@electron/rebuild/lib/search-module"
8+
import { EventEmitter } from "events"
9+
import { Platform } from "../core"
810

911
export async function installOrRebuild(config: Configuration, appDir: string, options: RebuildOptions, forceInstall = false) {
1012
let isDependenciesInstalled = false
@@ -24,7 +26,8 @@ export async function installOrRebuild(config: Configuration, appDir: string, op
2426
await installDependencies(appDir, effectiveOptions)
2527
} else {
2628
const arch = archFromString(options.arch || process.arch)
27-
await rebuild(appDir, config.buildDependenciesFromSource === true, options.frameworkInfo, arch)
29+
const platform = Platform.fromString(options.platform || process.platform)
30+
await rebuild(appDir, config.buildDependenciesFromSource === true, options.frameworkInfo, arch, platform)
2831
}
2932
}
3033

@@ -117,8 +120,8 @@ function installDependencies(appDir: string, options: RebuildOptions): Promise<a
117120
})
118121
}
119122

120-
export async function nodeGypRebuild(frameworkInfo: DesktopFrameworkInfo, arch: Arch) {
121-
return rebuild(process.cwd(), false, frameworkInfo, arch)
123+
export async function nodeGypRebuild(frameworkInfo: DesktopFrameworkInfo, arch: Arch, platform: Platform) {
124+
return rebuild(process.cwd(), false, frameworkInfo, arch, platform)
122125
}
123126

124127
function getPackageToolPath() {
@@ -146,18 +149,21 @@ export interface RebuildOptions {
146149
}
147150

148151
/** @internal */
149-
export async function rebuild(appDir: string, buildFromSource: boolean, frameworkInfo: DesktopFrameworkInfo, arch: Arch) {
150-
log.info({ arch: Arch[arch], version: frameworkInfo.version, appDir }, "executing @electron/rebuild")
152+
export async function rebuild(appDir: string, buildFromSource: boolean, frameworkInfo: DesktopFrameworkInfo, arch: Arch, platform: Platform) {
153+
log.info({ arch: Arch[arch], platform: platform.name, version: frameworkInfo.version, appDir }, "executing @electron/rebuild")
151154
const rootPath = await searchModule.getProjectRootPath(appDir)
152-
const options: electronRebuild.RebuildOptions = {
155+
const rebuilderOptions: electronRebuild.RebuilderOptions = {
153156
buildPath: appDir,
154157
electronVersion: frameworkInfo.version,
155158
arch: Arch[arch],
156159
projectRootPath: rootPath,
157160
disablePreGypCopy: true,
161+
lifecycle: new EventEmitter(),
158162
}
159163
if (buildFromSource) {
160-
options.prebuildTagPrefix = "totally-not-a-real-prefix-to-force-rebuild"
164+
rebuilderOptions.prebuildTagPrefix = "totally-not-a-real-prefix-to-force-rebuild"
161165
}
162-
return electronRebuild.rebuild(options)
166+
const rebuilder = new electronRebuild.Rebuilder(rebuilderOptions)
167+
rebuilder.platform = platform.nodeName
168+
return rebuilder.rebuild()
163169
}

packages/electron-builder/src/cli/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ async function checkIsOutdated() {
7878
async function rebuildAppNativeCode(args: any) {
7979
const projectDir = process.cwd()
8080
// this script must be used only for electron
81-
return nodeGypRebuild({ version: await getElectronVersion(projectDir), useCustomDist: true }, args.arch)
81+
return nodeGypRebuild({ version: await getElectronVersion(projectDir), useCustomDist: true }, args.arch, args.platform)
8282
}

pnpm-lock.yaml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)