如何为 Electron 重建原生 npm 模块?

Vol*_*lyy 5 node.js npm node-gyp electron

请帮助我了解如何重建 Electron 1.6.2 的原生 npm 模块?

我使用https://electron.atom.io/docs/tutorial/quick-start中的步骤创建了一个简单的 Electron 应用程序。
然后我安装了 ref 包并将其加载到 main.js 中。
ref 包 - 它是本机包。我必须重建这个包才能在 Electron 应用程序中使用它。

我使用以下教程来重建本机包 - https://github.com/electron/electron/blob/master/docs/tutorial/using-native-node-modules.md

有 3 种方法可以重建原生 npm 包:

1)“使用 Electron-rebuild 安装模块并重建 Electron”:
我安装了electron-rebuildnpm 包。
然后,根据教程,我只需要运行以下命令:

.\node_modules\.bin\electron-rebuild.cmd  
Run Code Online (Sandbox Code Playgroud)

但是 Electron-rebuild 尝试下载https://atom.io/download/electron/v1.6.2/iojs-v1.6.2.tar.gz
为什么它从https://atom.io/download/electron下载了一些东西,如果 Electron发布文件位于此处 - https://github.com/lectron/Electron/releases
Electron 1.6.2 仅在几周前发布,但 iojs-v1.6.2.tar.gz 于 2015 年 3 月发布。
我是否正确理解 Electron-rebuild 下载了错误的文件?

2)使用node-gyp手动构建Electron包。

set HOME=%USERPROFILE%/.electron-gyp  
cd ./node_modules/ref  
node-gyp rebuild --target=1.6.2 --arch=x64 --dist-url=https://atom.io/download/electron  
Run Code Online (Sandbox Code Playgroud)

这里 dist-url 再次使用https://atom.io/download/electron
为什么?
我试过了不使用--dist-url
但在这种情况下,node-gyp 下载以下文件:

gyp http GET https://iojs.org/download/release/v1.6.2/iojs-v1.6.2.tar.gz  
gyp http 200 https://iojs.org/download/release/v1.6.2/iojs-v1.6.2.tar.gz  
gyp http GET https://iojs.org/download/release/v1.6.2/SHASUMS256.txt  
gyp http GET https://iojs.org/download/release/v1.6.2/win-x64/iojs.lib  
gyp http GET https://iojs.org/download/release/v1.6.2/win-x86/iojs.lib  
gyp http 200 https://iojs.org/download/release/v1.6.2/SHASUMS256.txt  
gyp http 200 https://iojs.org/download/release/v1.6.2/win-x64/iojs.lib  
gyp http 200 https://iojs.org/download/release/v1.6.2/win-x86/iojs.lib 
Run Code Online (Sandbox Code Playgroud)

这是对的吗?

3)使用npm重建原生包:

rem # Electron's version.  
set npm_config_target=1.6.2  
rem # The architecture of Electron, can be ia32 or x64.  
set npm_config_arch=x64  
set npm_config_target_arch=x64  
rem # Download headers for Electron.  
rem set npm_config_disturl=https://atom.io/download/electron  
rem # Tell node-pre-gyp that we are building for Electron.  
set npm_config_runtime=electron  
rem # Tell node-pre-gyp to build module from source code.  
set npm_config_build_from_source=true  
rem # Install all dependencies, and store cache to ~/.electron-gyp.  
set HOME=%USERPROFILE\.electron-gyp  
npm install  
Run Code Online (Sandbox Code Playgroud)

再次https://atom.io/download/electron
为什么?

有谁知道如何正确重建 Electron 1.6.2 的 ref 本机包?

Mar*_*und 1

你是对的,给node-gyp的版本映射到Electron版本而不是内部节点版本,因为那是标头上传到的版本。这一切背后实际上有一个更复杂的解释,但这对最终用户来说应该不重要。您提供的代码是正确的并且来自文档。有用。无需了解其内部运作原理。