构建工具和koffi

构建工具

本机模块

Koffi 2.5.9 中的简化

Koffi 使用本机模块来工作。NPM 包包含适用于各种平台和架构的二进制文件,并在运行时选择适当的模块。

理论上,构建工具应该能够找到所有本机模块,因为它们在 Javascript 文件中明确列出(作为静态字符串)并以某种方式打包它们。

如果不是这种情况,您可以手动安排复制node_modules/koffi/build/koffi构建脚本旁边的目录。

这是一个可行的示例:

koffi/
    win32_x64/
        koffi.node
    linux_x64/
        koffi.node
    ...
MyBundle.js

当在Electron中运行时,Koffi也会尝试在process.resourcesPath中查找本地模块。对于Electron应用程序,您可以像这样做。

locales/
resources/
    koffi/
        win32_ia32/
            koffi.node
        win32_x64/
            koffi.node
        ...
MyApp.exe

间接加载

Koffi 2.6.2 中的新功能

某些构建工具(例如 vite)不喜欢将 require 与本机模块一起使用。

在这种情况下,您可以使用require('koffi/indirect'),但需要确保原生 Koffi 模块已正确打包。

打包示例

Electron with electron-builder#arrow-up-right

Packaging with electron-builder should work as-is.

Take a look at the full working example in the repositoryarrow-up-right.

Electron Forge

Packaging with Electron Force should work as-is, even when using webpack as configured initially when you run:

Take a look at the full working example in the repositoryarrow-up-right.

NW.js

Packagers such as nw-builder should work as-is.

You can find a full working example in the repositoryarrow-up-right.

Node.js and esbuild

You can easily tell esbuild to copy the native files with the copy loader and things should just work. Use something like:

You can find a full working example in the repositoryarrow-up-right.

Last updated