0

在使用 electron-builder 构建我的 Windows 应用程序后,ffi_napi 显然已正确重建和打包:

重建原生依赖:ffi_napi@3.1.0

但是当我尝试执行 Windows 应用程序时,我得到:

ffi_bindings.node 不是有效的 Win32 应用程序

我在模块上遇到了类似的问题canvas,但我使用这个特定的代码解决了这个问题,该代码将修补节点 abi 的版本以使用来自电子的版本。但是,自从我添加ffi_napi后,无论是否使用此 hack,该应用程序现在都无法正常工作。

/* eslint-disable @typescript-eslint/no-var-requires */

const { promises: fs } = require('fs')
const path = require('path')

const {
  get_runtime_abi: getRuntimeAbi,
} = require('../../node_modules/node-pre-gyp/lib/util/versioning')

const blue = '\u001b[36m'
const yellow = '\u001b[33m'
const red = '\u001b[31m'
const nc = '\u001b[0m'

const info = text => console.warn(blue + text + nc)
const warn = text => console.warn(yellow + text + nc)
const error = text => console.warn(red + text + nc)

const versioningPath = path.resolve(
  __dirname,
  '..',
  '..',
  'node_modules',
  'node-pre-gyp',
  'lib',
  'util',
  'versioning.js'
)

const nodeAbi = getRuntimeAbi('node')

const originalNodeAbi = 'node_abi: get_runtime_abi(runtime,options.target)'
const originalNodeNapiLabel =
  "node_napi_label: napi_build_version ? 'napi-v' + napi_build_version : get_runtime_abi(runtime,options.target)"
const replacedNodeAbi = `node_abi: '${nodeAbi}'`
const replacedNodeNapiLabel = `node_napi_label: '${nodeAbi}'`

async function preBuild() {
  const versioning = await fs.readFile(versioningPath, 'utf8')

  const versioningNodeNapiLabel = versioning.replace(
    originalNodeNapiLabel,
    replacedNodeNapiLabel
  )

  const versioningNodeAbi = versioningNodeNapiLabel.replace(
    originalNodeAbi,
    replacedNodeAbi
  )

  if (versioning === versioningNodeNapiLabel) {
    warn("Electron pre-build: node_napi_label hasn't been replaced")
  }

  if (versioning === versioningNodeAbi) {
    error("Electron pre-build: node_abi hasn't been replaced")
  } else {
    await fs.writeFile(versioningPath, versioningNodeAbi, 'utf8')
    info('Electron pre-build successfully executed')
  }
}

async function postBuild() {
  let versioning = await fs.readFile(versioningPath, 'utf8')

  versioning = versioning.replace(replacedNodeAbi, originalNodeAbi)
  versioning = versioning.replace(replacedNodeNapiLabel, originalNodeNapiLabel)

  await fs.writeFile(versioningPath, versioning, 'utf8')
  info('Electron post-build successfully executed')
}

module.exports = { preBuild, postBuild }
4

0 回答 0