Bun + next.js的VSCode调试器launch.json配置

yhqotfr8  于 2023-10-18  发布在  Vscode
关注(0)|答案(1)|浏览(217)

This page介绍如何配置VSCode以使用Bun进行调试。
本页介绍如何配置VSCode以调试next.js应用。
但是,将两者结合起来的launch.json配置是什么,这样我就可以调试运行在Bun上的next.js应用程序了?

wecizke3

wecizke31#

首先检查此页面“Bun for Visual Studio Code“是否可以提供launch.json的起点

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "bun",
      "request": "launch",
      "name": "Debug Bun",

      // The path to a JavaScript or TypeScript file to run.
      "program": "${file}",

      // The arguments to pass to the program, if any.
      "args": [],

      // The working directory of the program.
      "cwd": "${workspaceFolder}",

      // The environment variables to pass to the program.
      "env": {},

      // If the environment variables should not be inherited from the parent process.
      "strictEnv": false,

      // If the program should be run in watch mode.
      // This is equivalent to passing `--watch` to the `bun` executable.
      // You can also set this to "hot" to enable hot reloading using `--hot`.
      "watchMode": false,

      // If the debugger should stop on the first line of the program.
      "stopOnEntry": false,

      // If the debugger should be disabled. (for example, breakpoints will not be hit)
      "noDebug": false,

      // The path to the `bun` executable, defaults to your `PATH` environment variable.
      "runtime": "bun",

      // The arguments to pass to the `bun` executable, if any.
      // Unlike `args`, these are passed to the executable itself, not the program.
      "runtimeArgs": [],
    },
    {
      "type": "bun",
      "request": "attach",
      "name": "Attach to Bun",

      // The URL of the WebSocket inspector to attach to.
      // This value can be retrieved by using `bun --inspect`.
      "url": "ws://localhost:6499/",
    }
  ]
}

例如,您可以尝试在package.json中添加一个脚本,用于使用Bun运行Next.js应用程序。假设Next.js应用程序的入口文件是server.js

"scripts": {
  "dev": "bun server.js",
  ...
}

一个可能的组合launch.json可能看起来像这样:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Next.js + Bun: debug server-side",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "runtime": "bun",
      "runtimeArgs": [],
      "program": "${workspaceFolder}/server.js",
      "cwd": "${workspaceFolder}"
    },
    {
      "name": "Next.js: debug client-side",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:3000"
    },
    {
      "name": "Next.js + Bun: debug full stack",
      "type": "node-terminal",
      "request": "launch",
      "command": "npm run dev",
      "runtime": "bun",
      "runtimeArgs": [],
      "program": "${workspaceFolder}/server.js",
      "cwd": "${workspaceFolder}",
      "serverReadyAction": {
        "pattern": "- Local:.+(https?://.+)",
        "uriFormat": "%s",
        "action": "debugWithChrome"
      }
    },
    {
      "type": "bun",
      "request": "attach",
      "name": "Attach to Bun",
      "url": "ws://localhost:6499/"
    }
  ]
}

相关问题