自定义Hvigor插件是否能进行断点调试
====================

#### 问题现象

自定义Hvigor插件如何调试？

#### 背景知识

*   Hvigor允许开发者实现自己的插件，开发者可以定义自己的构建逻辑，并与他人共享，参考[开发Hvigor插件](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-hvigor-plugin)。
*   Node.js的Inspector模块是用于启用和管理V8 Inspector的内置模块，它允许开发者通过Chrome DevTools远程调试和检查运行中的Node.js应用程序。

#### 解决方案

*   hvigorfile.ts文件暂时不支持在DevEco Studio调试。
*   可借助Node.js的Inspector模块通过Chrome DevTools进行断点调试，具体步骤如下：
    1.  在hvigorfile.ts通过open启动调试器并等待连接：
        
        import { hapTasks } from '@ohos/hvigor-ohos-plugin';
        import { HvigorPlugin, HvigorNode } from '@ohos/hvigor';
        import { open } from 'inspector';
        
        _/\*\*_
         _\* open(port?: number, host?: string, wait?: boolean): void_
         _\* port：用于监听检查器连接的端口。_
         _\* host：用于监听检查器连接的主机。_
         _\* wait：阻塞直到有客户端连接。_
         _\*/_
        open(9229, undefined, true);
        
        _// 自定义插件_
        function customPlugin(): HvigorPlugin {
          return {
            pluginId: 'customPlugin',
            apply(node: HvigorNode): void {
              debugger
              console.info("hello customPlugin");
            }
          }
        }
        
        export default {
          system: hapTasks, _/\* Built-in plugin of Hvigor. It cannot be modified. \*/_
          plugins: \[
            customPlugin()
          \]      _ /\* Custom plugin to extend the functionality of Hvigor. \*/_
        }
        
    2.  打开Chrome调试页面：chrome://inspect/#devices：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/be/v3/YbdCc0FdQlOBlwJmnoDt3Q/zh-cn_image_0000002658928587.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=B3250396DD3C1B6296EC0CF70DC9C068994E0941333D57ED52A23EBEB8F4C7C4 "点击放大")
        
    3.  勾选“Discover network targets”，点击Configure按钮：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a/v3/N1jgr0UITEeqKidK_LeAjw/zh-cn_image_0000002658808637.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=87395CC6262FD0E657CD2AB62198DE08A901F6C33BE75E64FBDC682641966407 "点击放大")
        
    4.  配置监听接口，勾选“Enable port forwarding”，点击Done：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c1/v3/VdA7sXxeTNqD-xRuiVEC2w/zh-cn_image_0000002628569272.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=67D56DF802816FB6D3F5CF3FF74537D3E66400FE6B256858ED0D61CB01407032 "点击放大")
        
    5.  回到DevEco Studio界面，执行构建：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c3/v3/rYfBcWX3TGKcY-EWhQfNxg/zh-cn_image_0000002628409366.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=E8FF477A5865007A2A190900BE24EED3E5F88321897AE72D237BCD8A324BD186 "点击放大")
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/ce/v3/D3dmiQ4cQkenhUp07JQCgQ/zh-cn_image_0000002658928589.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=03C3146E361BE509C181F85A80C694AB88CB63784F07661635CE88D1439C8E7A "点击放大")
        
    6.  切换至Chrome调试页面，点击“inspect”，即可开始调试：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/9f/v3/ysts6cKdTFqyHMtx6hr6zA/zh-cn_image_0000002658808639.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=D11BB91F89CA758FF348E18F7F41859CB943FE53D8F46B4BA1A2A896F55BADDA "点击放大")
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/55/v3/w1svjcfETAW0OLcaTUHxYg/zh-cn_image_0000002628569274.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=8D94DF84F2395ED6485C7BCDB2A6919D6C1D03A546AEF26C4A5A552C82511EC0 "点击放大")
        
    7.  调试如果开启了[守护进程](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-hvigor-daemon)，二次构建时会因守护进程还在出现以下报错，[关闭当前工程的守护进程](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-hvigor-daemon#section298519112359)重新执行步骤5即可：
        
        ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/5c/v3/D_zMFb2hQweuKgTCi9-MFQ/zh-cn_image_0000002628409368.png?HW-CC-KV=V1&HW-CC-Date=20260709T134405Z&HW-CC-Expire=86400&HW-CC-Sign=F44CF6E82C1D59FF484FDA9479C996BD325B6A9A715D1D570675304239D9A0EE "点击放大")