From 89fbeecb26363c6c1faee94fc5236a93c5cbb880 Mon Sep 17 00:00:00 2001 From: zhangmo8 Date: Tue, 3 Jun 2025 16:41:55 +0800 Subject: [PATCH] fix: fix sharp on mac arm build by x64 --- .github/workflows/build.yml | 6 ++++ package.json | 7 ++-- scripts/fix-sharp.js | 70 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 scripts/fix-sharp.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 638bd5a5b..d0fe2d817 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -137,8 +137,14 @@ jobs: - name: Install Sharp run: pnpm install sharp env: + npm_config_build_from_source: true npm_config_cpu: ${{ matrix.arch }} npm_config_os: darwin + - name: Copy Sharp binaries + run: | + mkdir -p dist/sharp-binaries + cp -R node_modules/sharp/vendor/lib dist/sharp-binaries/ + cp -R node_modules/sharp/build/Release dist/sharp-binaries/ - name: Build Mac run: pnpm run build:mac:${{ matrix.arch }} env: diff --git a/package.json b/package.json index 55d37cca3..92061641f 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,10 @@ "build:win": "pnpm run build && electron-builder --win", "build:win:x64": "pnpm run build && electron-builder --win --x64", "build:win:arm64": "pnpm run build && electron-builder --win --arm64", - "build:mac": "pnpm run build && electron-builder --mac", - "build:mac:arm64": "pnpm run build && electron-builder --mac --arm64", - "build:mac:x64": "pnpm run build && electron-builder -c electron-builder-macx64.yml --mac --x64 ", + "fix:sharp": "node scripts/fix-sharp.js", + "build:mac": "pnpm run build && pnpm run fix:sharp && electron-builder --mac", + "build:mac:arm64": "pnpm run build && pnpm run fix:sharp && electron-builder --mac --arm64", + "build:mac:x64": "pnpm run build && pnpm run fix:sharp && electron-builder -c electron-builder-macx64.yml --mac --x64 ", "build:linux": "pnpm run build && electron-builder --linux", "build:linux:x64": "pnpm run build && electron-builder --linux --x64", "build:linux:arm64": "pnpm run build && electron-builder --linux --arm64", diff --git a/scripts/fix-sharp.js b/scripts/fix-sharp.js new file mode 100644 index 000000000..777895958 --- /dev/null +++ b/scripts/fix-sharp.js @@ -0,0 +1,70 @@ +// fix-sharp.js +// 该脚本用于修复跨平台构建时 sharp 模块的兼容性问题 +import fs from 'fs' +import path from 'path' +import { execSync } from 'child_process' + +/** + * 该函数在应用打包前执行,确保 sharp 模块能够在跨平台环境中正确加载 + */ +function fixSharpForCrossPlatform() { + console.log('开始修复 sharp 模块跨平台兼容性问题...'); + + try { + // 获取当前平台和目标架构信息 + const currentPlatform = process.platform; + const targetArch = process.env.npm_config_arch || process.arch; + + console.log(`当前平台: ${currentPlatform}, 目标架构: ${targetArch}`); + + // 如果是在 macOS 上构建 + if (currentPlatform === 'darwin') { + console.log('检测到 macOS 平台,准备处理 sharp 依赖...'); + + // 强制重新安装 sharp,确保二进制文件正确 + console.log('重新安装 sharp 模块...'); + + // 设置环境变量,确保使用正确的平台和架构 + const env = { + ...process.env, + npm_config_build_from_source: 'true', + npm_config_cpu: targetArch, + npm_config_os: 'darwin' + }; + + // 执行安装命令 + execSync('pnpm install sharp --force', { + stdio: 'inherit', + env + }); + + // 创建目标目录 + const sharpBinDir = path.join(process.cwd(), 'resources', 'sharp-bin', targetArch); + fs.mkdirSync(sharpBinDir, { recursive: true }); + + // 复制 sharp 的二进制文件到资源目录 + const sharpVendorDir = path.join(process.cwd(), 'node_modules', 'sharp', 'vendor'); + const sharpBuildDir = path.join(process.cwd(), 'node_modules', 'sharp', 'build', 'Release'); + + if (fs.existsSync(sharpVendorDir)) { + console.log(`复制 sharp vendor 文件从 ${sharpVendorDir} 到 ${sharpBinDir}`); + execSync(`cp -R "${sharpVendorDir}" "${sharpBinDir}"`); + } + + if (fs.existsSync(sharpBuildDir)) { + console.log(`复制 sharp build 文件从 ${sharpBuildDir} 到 ${sharpBinDir}`); + execSync(`cp -R "${sharpBuildDir}" "${sharpBinDir}"`); + } + + console.log('Sharp 二进制文件已复制到资源目录'); + } + + console.log('Sharp 模块跨平台兼容性修复完成!'); + } catch (error) { + console.error('修复 sharp 模块时出错:', error); + process.exit(1); + } +} + +// 执行修复函数 +fixSharpForCrossPlatform();