Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
fix: copy WebView2Loader.dll beside kept GNU Windows binary (#1403)
Paragon3e906e8188e6
diff --git a/bin/builders/WinBuilder.ts b/bin/builders/WinBuilder.tsindex 7ffc03f1..81063f21 100644--- a/bin/builders/WinBuilder.ts+++ b/bin/builders/WinBuilder.ts@@ -1,13 +1,18 @@import path from 'path';+import fsExtra from 'fs-extra';import BaseBuilder from './BaseBuilder';import { PakeAppOptions } from '@/types';import { generateIdentifierSafeName } from '@/utils/name';import type { ShellCommand } from '@/utils/shell';+import logger from '@/options/logger';++const WEBVIEW2_LOADER_DLL = 'WebView2Loader.dll';export default class WinBuilder extends BaseBuilder {private buildFormat: string = 'msi';private buildArch: string;private toolchain: 'msvc' | 'gnu';+ private keptLoaderDllPath: string | null = null;// MSYS2/MinGW only ships an x86_64 GCC toolchain, so gnu is x64-only;// arm64 falls through to getTauriTarget returning null, which the@@ -92,4 +97,44 @@ export default class WinBuilder extends BaseBuilder {protected getBinaryName(appName: string): string {return `pake-${generateIdentifierSafeName(appName)}.exe`;}++ protected async copyRawBinary(+ npmDirectory: string,+ appName: string,+ ): Promise<void> {+ await super.copyRawBinary(npmDirectory, appName);+ if (this.toolchain !== 'gnu') return;++ // GNU builds import WebView2Loader.dll at runtime (MSVC links+ // WebView2LoaderStatic); tauri-build places it beside the binary and the+ // MSI bundles it, so the kept .exe needs it beside it too.+ const src = path.join(+ path.dirname(this.getRawBinarySourcePath(npmDirectory, appName)),+ WEBVIEW2_LOADER_DLL,+ );+ const dest = path.join(+ path.dirname(path.resolve(this.getRawBinaryPath(appName))),+ WEBVIEW2_LOADER_DLL,+ );+ if (await fsExtra.pathExists(src)) {+ await fsExtra.copy(src, dest);+ this.keptLoaderDllPath = dest;+ } else {+ logger.warn(+ `✼ ${WEBVIEW2_LOADER_DLL} not found at ${src}; the raw binary will not start without it.`,+ );+ }+ }++ protected async copyBuildArtifacts(target: string, logSuccess = true) {+ await super.copyBuildArtifacts(target, logSuccess);+ if (!this.keptLoaderDllPath) return;++ await this.recordArtifact(this.keptLoaderDllPath, 'dll');+ if (logSuccess) {+ logger.success(+ `✔ ${WEBVIEW2_LOADER_DLL} copied beside it — keep them together`,+ );+ }+ }}diff --git a/dist/cli.js b/dist/cli.jsindex 3cfd21de..ba9e4079 100644--- a/dist/cli.js+++ b/dist/cli.js@@ -2117,10 +2117,12 @@ class MacBuilder extends BaseBuilder {}}+const WEBVIEW2_LOADER_DLL = 'WebView2Loader.dll';class WinBuilder extends BaseBuilder {constructor(options) {super(options);this.buildFormat = 'msi';+ this.keptLoaderDllPath = null;const validArchs = ['x64', 'arm64', 'auto'];this.buildArch = validArchs.includes(options.targets || '')? this.resolveTargetArch(options.targets)@@ -2175,6 +2177,32 @@ class WinBuilder extends BaseBuilder {getBinaryName(appName) {return `pake-${generateIdentifierSafeName(appName)}.exe`;}+ async copyRawBinary(npmDirectory, appName) {+ await super.copyRawBinary(npmDirectory, appName);+ if (this.toolchain !== 'gnu')+ return;+ // GNU builds import WebView2Loader.dll at runtime (MSVC links+ // WebView2LoaderStatic); tauri-build places it beside the binary and the+ // MSI bundles it, so the kept .exe needs it beside it too.+ const src = path.join(path.dirname(this.getRawBinarySourcePath(npmDirectory, appName)), WEBVIEW2_LOADER_DLL);+ const dest = path.join(path.dirname(path.resolve(this.getRawBinaryPath(appName))), WEBVIEW2_LOADER_DLL);+ if (await fsExtra.pathExists(src)) {+ await fsExtra.copy(src, dest);+ this.keptLoaderDllPath = dest;+ }+ else {+ logger.warn(`✼ ${WEBVIEW2_LOADER_DLL} not found at ${src}; the raw binary will not start without it.`);+ }+ }+ async copyBuildArtifacts(target, logSuccess = true) {+ await super.copyBuildArtifacts(target, logSuccess);+ if (!this.keptLoaderDllPath)+ return;+ await this.recordArtifact(this.keptLoaderDllPath, 'dll');+ if (logSuccess) {+ logger.success(`✔ ${WEBVIEW2_LOADER_DLL} copied beside it — keep them together`);+ }+ }}// MSYS2/MinGW only ships an x86_64 GCC toolchain, so gnu is x64-only;// arm64 falls through to getTauriTarget returning null, which thediff --git a/docs/cli-usage.md b/docs/cli-usage.mdindex e9d4dbb0..d857970c 100644--- a/docs/cli-usage.md+++ b/docs/cli-usage.md@@ -375,6 +375,8 @@ Select the Rust toolchain used for Windows builds. Windows only; ignored on otheIf no MSVC Build Tools are detected and a GNU toolchain is available, Pake logs a hint to retry with `--windows-toolchain gnu` rather than failing deep in the build with an unexplained linker error. Requesting `gnu` does not change your default Rust toolchain or any global `rustup`/environment settings; it only affects the current build subprocess.+With `gnu`, the executable loads `WebView2Loader.dll` at runtime instead of linking it statically as MSVC does. The MSI already includes it, and `--keep-binary` copies it next to `AppName.exe`; keep the two files together or the raw binary will not start.+#### [no-bundle]Skip packaging and output only the compiled executable. Linux only. Useful on RPM-based distros (Fedora, RHEL, Oracle Linux, etc.) where the native bundler can abort during the packaging stage, so you still get a runnable binary.@@ -490,7 +492,7 @@ Keep the raw binary file alongside the installer. Default is `false`. When enablpake https://github.com --name GitHub --keep-binary```-**Output**: Creates both installer and standalone executable (`AppName-binary` on Unix, `AppName.exe` on Windows).+**Output**: Creates both installer and standalone executable (`AppName-binary` on Unix, `AppName.exe` on Windows). With `--windows-toolchain gnu`, `WebView2Loader.dll` is copied next to `AppName.exe` as well.#### [iterative-build]diff --git a/docs/cli-usage_CN.md b/docs/cli-usage_CN.mdindex c1dba6a2..15df9a6f 100644--- a/docs/cli-usage_CN.md+++ b/docs/cli-usage_CN.md@@ -373,6 +373,8 @@ pake https://github.com --name GitHub如果未检测到 MSVC Build Tools 但检测到可用的 GNU 工具链,Pake 会提示可以改用 `--windows-toolchain gnu` 重试,而不是让构建在链接阶段以难以理解的错误失败。使用 `gnu` 不会更改你的默认 Rust 工具链或任何全局 `rustup`/环境设置,仅影响当次构建子进程。+使用 `gnu` 时,可执行文件会在运行时加载 `WebView2Loader.dll`,而不是像 MSVC 那样静态链接。MSI 安装包已包含该文件;`--keep-binary` 会把它复制到 `AppName.exe` 旁边,请将两者放在一起,否则原始可执行文件无法启动。+#### [no-bundle]跳过打包,只输出编译好的可执行文件。仅 Linux 可用。适用于 Fedora、RHEL、Oracle Linux 等 RPM 系发行版,这些系统上原生打包器可能在打包阶段中止,用此选项仍能拿到可运行的二进制。@@ -488,7 +490,7 @@ pake https://planka.example.com --name PlankApp --enable-drag-droppake https://github.com --name GitHub --keep-binary```-**输出结果**:同时创建安装包和独立可执行文件(Unix 系统为 `AppName-binary`,Windows 为 `AppName.exe`)。+**输出结果**:同时创建安装包和独立可执行文件(Unix 系统为 `AppName-binary`,Windows 为 `AppName.exe`)。使用 `--windows-toolchain gnu` 时,还会把 `WebView2Loader.dll` 复制到 `AppName.exe` 旁边。#### [iterative-build]diff --git a/tests/unit/windows-toolchain.test.ts b/tests/unit/windows-toolchain.test.tsindex 76a0c31d..4a834d13 100644--- a/tests/unit/windows-toolchain.test.ts+++ b/tests/unit/windows-toolchain.test.ts@@ -1,4 +1,6 @@+import os from 'os';import path from 'path';+import fsExtra from 'fs-extra';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';vi.mock('@/utils/dir', () => ({@@ -16,6 +18,7 @@ vi.mock('@/utils/platform', async (original) => ({import BaseBuilder from '@/builders/BaseBuilder';import WinBuilder from '@/builders/WinBuilder';import { getWindowsGnuBuildEnvironment } from '@/builders/env';+import logger from '@/options/logger';import { shellExec } from '@/utils/shell';import { PakeAppOptions } from '@/types';@@ -215,3 +218,128 @@ describe('BaseBuilder.runBuildCommand Windows GNU RUSTFLAGS injection', () => {expect(env?.RUSTFLAGS).toBeUndefined();});});++describe('WinBuilder raw binary WebView2Loader.dll', () => {+ const originalCargoTargetDir = process.env.CARGO_TARGET_DIR;+ const originalCwd = process.cwd();+ const tempDirs: string[] = [];++ afterEach(async () => {+ process.chdir(originalCwd);+ vi.restoreAllMocks();+ if (originalCargoTargetDir === undefined) {+ delete process.env.CARGO_TARGET_DIR;+ } else {+ process.env.CARGO_TARGET_DIR = originalCargoTargetDir;+ }+ await Promise.all(tempDirs.splice(0).map((dir) => fsExtra.remove(dir)));+ });++ // Lays out a fake cargo target dir holding the raw binary (and optionally+ // the loader DLL) for the given triple, plus an empty output dir.+ async function createFixture(triple: string, withDll = true) {+ const tempDir = await fsExtra.mkdtemp(+ path.join(os.tmpdir(), 'pake-win-binary-'),+ );+ tempDirs.push(tempDir);+ const cargoTargetDir = path.join(tempDir, 'target');+ const releaseDir = path.join(cargoTargetDir, triple, 'release');+ const outDir = path.join(tempDir, 'out');+ await fsExtra.outputFile(path.join(releaseDir, 'pake-demo.exe'), 'exe');+ if (withDll) {+ await fsExtra.outputFile(+ path.join(releaseDir, 'WebView2Loader.dll'),+ 'dll',+ );+ }+ await fsExtra.ensureDir(outDir);+ process.env.CARGO_TARGET_DIR = cargoTargetDir;+ return { releaseDir, outDir };+ }++ function makeBuilder(+ outDir: string,+ windowsToolchain: 'msvc' | 'gnu',+ extra: Partial<PakeAppOptions> = {},+ ) {+ const builder = new WinBuilder({+ name: 'Demo',+ appVersion: '1.0.0',+ installerLanguage: 'en-US',+ targets: 'x64',+ debug: false,+ windowsToolchain,+ ...extra,+ } as PakeAppOptions);+ vi.spyOn(builder as any, 'getRawBinaryPath').mockReturnValue(+ path.join(outDir, 'Demo.exe'),+ );+ return builder;+ }++ it('copies WebView2Loader.dll beside the raw binary for gnu', async () => {+ const { outDir } = await createFixture('x86_64-pc-windows-gnu');+ const builder = makeBuilder(outDir, 'gnu');++ await (builder as any).copyRawBinary(process.cwd(), 'Demo');++ expect(await fsExtra.pathExists(path.join(outDir, 'Demo.exe'))).toBe(true);+ expect(+ await fsExtra.readFile(path.join(outDir, 'WebView2Loader.dll'), 'utf8'),+ ).toBe('dll');+ });++ it('does not copy WebView2Loader.dll for msvc', async () => {+ const { outDir } = await createFixture('x86_64-pc-windows-msvc');+ const builder = makeBuilder(outDir, 'msvc');++ await (builder as any).copyRawBinary(process.cwd(), 'Demo');++ expect(await fsExtra.pathExists(path.join(outDir, 'Demo.exe'))).toBe(true);+ expect(+ await fsExtra.pathExists(path.join(outDir, 'WebView2Loader.dll')),+ ).toBe(false);+ });++ it('warns without throwing when the gnu build has no WebView2Loader.dll', async () => {+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});+ const { outDir } = await createFixture('x86_64-pc-windows-gnu', false);+ const builder = makeBuilder(outDir, 'gnu');++ await expect(+ (builder as any).copyRawBinary(process.cwd(), 'Demo'),+ ).resolves.toBeUndefined();++ expect(warnSpy).toHaveBeenCalledWith(+ expect.stringContaining('WebView2Loader.dll not found'),+ );+ expect(+ await fsExtra.pathExists(path.join(outDir, 'WebView2Loader.dll')),+ ).toBe(false);+ });++ it('records the DLL as an artifact after the binary with --keep-binary', async () => {+ const successSpy = vi.spyOn(logger, 'success').mockImplementation(() => {});+ const { releaseDir, outDir } = await createFixture('x86_64-pc-windows-gnu');+ await fsExtra.outputFile(+ path.join(releaseDir, 'bundle', 'msi', 'Demo_1.0.0_x64_en-US.msi'),+ 'msi',+ );+ // copyBuildArtifacts writes the installer relative to the cwd.+ process.chdir(outDir);+ const builder = makeBuilder(outDir, 'gnu', { keepBinary: true });++ await (builder as any).copyBuildArtifacts('msi');++ const artifacts = builder.getArtifacts();+ expect(artifacts.map((artifact) => artifact.format)).toEqual([+ 'msi',+ 'binary',+ 'dll',+ ]);+ expect(artifacts[2].path).toBe(path.join(outDir, 'WebView2Loader.dll'));+ expect(successSpy).toHaveBeenLastCalledWith(+ expect.stringContaining('WebView2Loader.dll copied beside it'),+ );+ });+});