简体   繁体   English

如何调试 Cordova 的钩子?

[英]How to debug a Cordova's hook?

I was wondering if is it possible to debug a javascript hook in Cordova?我想知道是否可以在 Cordova 中调试 javascript 钩子?

My hook is triggered before prepare.我的钩子在准备之前被触发。 My command is我的命令是

cordova prepare ios

I currently use Visual Studio Code and there is a plugin "Cordova tools" to debug an app at runtime.我目前使用 Visual Studio Code,并且有一个插件“Cordova 工具”可以在运行时调试应用程序。 But my need is to debug at build time.但我的需要是在构建时进行调试。

Any recommandation?有什么推荐吗?

PS : what I mean with debug is real debug, that is to say with breakpoints and display of variables, etc.. PS:我说的debug是真正的debug,也就是断点和变量显示等。

Updated answer 25 Nov 2019 2019 年 11 月 25 日更新的答案

Since node-inspector is deprecated, here is how I would now do this:由于不推荐使用node-inspector器,因此我现在将如何执行此操作:

  • Open chrome://inspect in Chrome browser在 Chrome 浏览器中打开chrome://inspect
  • Run node --inspect --inspect-brk /path/to/node_modules/cordova/bin/cordova prepare from the root of my Cordova app project which contains the hook scripts I wish to debug从我的 Cordova 应用程序项目的根目录运行node --inspect --inspect-brk /path/to/node_modules/cordova/bin/cordova prepare包含我希望调试的钩子脚本
  • In the Chrome tab, press inspect on the target to open Chrome Dev Tools在 Chrome 选项卡中,按目标上的inspect以打开 Chrome 开发工具
  • Under Filesystem tab, select Add folder to workspace and select the directory inside my Cordova project containing the hook scripts在 Filesystem 选项卡下,选择Add folder to workspace并选择包含钩子脚本的 Cordova 项目中的目录
  • Add a breakpoint to my hook script在我的钩子脚本中添加断点
  • Press Play in Chrome Dev Tools to proceed and hit my breakpoint在 Chrome Dev Tools 中按 Play 继续并点击我的断点

Original answer 7 Jun 2017 Here's how I debug my hook scripts:原始答案 2017 年 6 月 7 日这是我调试钩子脚本的方法:

  • Install node inspector: npm install -g node-inspector安装节点检查器: npm install -g node-inspector
  • From the Cordova project root directory, run the Cordova command via node inspector with appropriate options to trigger my hook script, for example:从 Cordova 项目根目录,通过节点检查器运行 Cordova 命令并使用适当的选项来触发我的钩子脚本,例如:

    node-debug /path/to/node_modules/cordova/bin/cordova prepare

  • When node inspector opens in a Chrome tab, browse Sources to find your hook script当节点检查器在 Chrome 选项卡中打开时,浏览源以查找您的钩子脚本

  • Add a breakpoint添加断点
  • Press Resume to continue execution to your breakpoint按 Resume 继续执行到您的断点
  • Then you can interactively debug your hook script:然后你可以交互式调试你的钩子脚本:

节点检查器

You can debug a Cordova hook easily inside VS Code without opening a browser by putting this launch configuration to the .vscode/launch.json file at the root of your project:通过将此启动配置放入项目根目录下的.vscode/launch.json文件,您可以在 VS Code 中轻松调试 Cordova 挂钩,而无需打开浏览器:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Cordova Prepare",
      "program": "C:/Program Files/nodejs/node_modules/cordova/bin/cordova", // This is for winx64 adjust it to your platform
      "args": ["prepare"]
    }
  ]
}

After just put a breakpoint in the hook's file and hit F5 or go to the Debug and Run side menu and press the Play button at the top next to the "Cordova Prepare" text.在钩子的文件中放置一个断点并按 F5 或转到“ Debug and Run侧菜单并按“Cordova Prepare”文本旁边顶部的“播放”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM