针对不同的 NODE_MODULE_VERSION 编译 - Tree-sitter 解析器,ATOM 包

Compiled against different NODE_MODULE_VERSION - Tree-sitter parser, ATOM package

我创建了一个 tree-sitter 解析器,它正在解析我们使用的一些遗留 SAP 语言,以使包与 ATOM 兼容。 我会非常了解自己的行为,因为我对自己搞砸的地方不太自信。

根据 tree-sitter 文档,我阅读了许多指南 - 我已经完成了以下操作:

添加 D:\path-to-project\node_modules.bin 到 PATH

npm install -g node-gyp (I have installed the dependencies - MSVC 2017 and Python 2.7)

创建了一个新目录并 cd 到它:

npm init (followed the dialog)
npm install
npm install --save-dev nan
npm install --save-dev tree-sitter-cli

现在我们可以创建我们的 tree-sitter grammar.js 文件并生成 tree-sitter 解析器,我这样做了:

创建了 grammar.js 并写下了必要的语法 打开 CMD,cd 到项目文件夹并:

tree-sitter generate
node-gyp configure
node-gyp build
tree-sitter test (To check if the parser is working correctly, which it does)

因此,现在我可以将解析器集成到 ATOM 包中。 所以我通过 - 'npm publish' 然后将我创建的 tree-sitter 解析器发布到 npm:

我从 ATOMs 生成包函数创建了一个包,在包中创建了一个“语法”目录和一个具有以下值的 .cson 文件:

name: 'Human readable name'
scopeName: 'A unique, stable identifier for the language.'
type: 'tree-sitter' (indicates that we are using tree-sitter grammar not TextMate one)
parser: 'Here we must the npm package name that we published to NPM' 
fileTypes: ['array of file extensions that the package will run on for example 'cpp'']

这里是有趣的地方。在我的 ATOM 包目录中的 'npm install my-npm-package-name' 之后,我在 ATOM 中得到一个错误:

The module '\?\D:\Users\myUser\github\my-ATOM-package-name\node_modules\my-tree-sitter-npm-package    
\build\Release\my-tree-sitter-npm-package_binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 73. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`). 
in D:\Users\myUser\.atom\packages\my-ATOM-package-name\grammars\the-cson-file-mentioned.cson

根据我所做的网络调查,这是因为 ATOM 使用的 NodeJs 版本与我正在使用的版本不同 (NodeJs v 12.4.0)。 这就是我被困的地方,在多次尝试使用 'electron-rebuild' 重新编译后,我没有成功就放弃了,并决定我需要更有经验的帮手。

我正在 Windows 上开发和安装。 NodeJS 与 nvm 一起安装,我正在使用 v12.4.0 进行开发。

我设法解决了这个问题。 显然或不那么显然你应该:

npm install --save-dev electron@9.3.5 (the current electron version atom was at the time of the last edit of the answer)
npm install --save-dev electron-rebuild 

electron-rebuild --version 9.3.5 (the current electron version atom was using at the time of the last edit of the answer)

如果它仍然抱怨你是 运行 不同的版本你应该:

node-gyp rebuild
electron-rebuild --version 9.3.5 (the current electron version atom was using at the time)

发布到 npm 之前: 删除 binding.gyp、package-lock.json 文件和 Electron 二进制文件夹