UNPKG

2.33 kBMarkdownView Raw
1# dbgr <a href="https://npm.im/dbgr"><img src="https://badgen.net/npm/v/dbgr"></a> <a href="https://npm.im/dbgr"><img src="https://badgen.net/npm/dm/dbgr"></a> <a href="https://packagephobia.now.sh/result?p=dbgr"><img src="https://packagephobia.now.sh/badge?p=dbgr"></a>
2
3**dbgr** is a lightweight debugger function that pauses your script, and watches the current file for any changes and only re-runs the specific code that's passed in to it.
4
5
6<sub>If you like this project, please star it & [follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
7
8## 🙋‍♂️ Why?
9You can set breakpoints in Node.js via `debugger` statements, but it could be a hassle to set up and can really slow down your script.
10
11When you're debugging something heavy with slow-startup (eg. server, headless Chrome, etc), you want to use something simple & light to debug.
12
13## 🚀 Install
14```sh
15npm i -D dbgr
16```
17
18## 🚦 Quick Setup
19
20```js
21import dbgr from 'dbgr'
22
23// Some async process
24(async () => {
25
26 // ...
27
28 await dbgr((resume) => {
29 console.log('The debugger has started');
30
31 // Write code here and hit save to
32 // automatically re-run this function
33
34 // Call resume() and save to resume the debugger
35
36 // ↓ The eval below is necessary for this to work
37 }, _ => eval(_))
38})();
39```
40
41## 🙋‍♀️ FAQ
42### How does it work?
43Upon invoking dbgr, it detects the file path of the caller by using [V8 stack trace API](https://v8.dev/docs/stack-trace-api) via [callsites](https://github.com/sindresorhus/callsites). It then watches the file for changes using [`fs.watch`](https://nodejs.org/docs/latest/api/fs.html#fs_fs_watch_filename_options_listener). When a change is detected, it parses the source code using [acorn](https://github.com/acornjs/acorn) to extract the specific function passed into dbgr. It then passes it into the `_ => eval(_)` to run in the original context.
44
45### Does it work in TypeScript files?
46Yes. While the AST parser acorn is designed for ES parsing, TS files can be loosely parsed via [acorn-loose](https://github.com/acornjs/acorn/tree/master/acorn-loose), and the content inside the dbgr hook has the types stripped via [esbuild](https://esbuild.github.io/) for it to be "safely" `eval()`'d by the JavaScript runtime.