|
| 1 | +--- |
| 2 | +title: Signals Debug |
| 3 | +description: A powerful debugging toolkit for Preact Signals that provides detailed console insights into signal updates, effects, and computed values. |
| 4 | +--- |
| 5 | + |
| 6 | +# Signals Debug |
| 7 | + |
| 8 | +`@preact/signals-debug` is a development tool that helps you understand and debug your signal-based application by providing detailed console output about signal updates, effect executions, and computed value recalculations. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +<toc></toc> |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @preact/signals-debug |
| 20 | +``` |
| 21 | + |
| 22 | +> :bulb: **Important**: Import this package at the root of your application to ensure all signals are tracked from the start. |
| 23 | +
|
| 24 | +## Quick Start |
| 25 | + |
| 26 | +Simply import the package at your application's entry point: |
| 27 | + |
| 28 | +```jsx |
| 29 | +// index.jsx or main.jsx |
| 30 | +import '@preact/signals-debug'; |
| 31 | +import { signal, computed, effect } from '@preact/signals'; |
| 32 | + |
| 33 | +// All signal operations will now be logged to the console |
| 34 | +const count = signal(0); |
| 35 | +const doubled = computed(() => count.value * 2); |
| 36 | + |
| 37 | +effect(() => { |
| 38 | + console.log('Count is:', count.value); |
| 39 | +}); |
| 40 | + |
| 41 | +count.value = 5; // This will be logged by the debug package |
| 42 | +``` |
| 43 | + |
| 44 | +## Features |
| 45 | + |
| 46 | +The debug package provides comprehensive tracking for all signal operations: |
| 47 | + |
| 48 | +- **Value Changes**: Tracks and logs all signal value changes with before/after comparisons |
| 49 | +- **Effect Tracking**: Monitors effect executions and their dependencies |
| 50 | +- **Computed Values**: Tracks computed value recalculations and their dependencies |
| 51 | +- **Update Grouping**: Groups related updates for better visualization in the console |
| 52 | +- **Dependency Graph**: Tracks signal dependencies for visualization in DevTools |
| 53 | + |
| 54 | +## Configuration |
| 55 | + |
| 56 | +Use `setDebugOptions()` to configure debugging behavior: |
| 57 | + |
| 58 | +```typescript |
| 59 | +import { setDebugOptions } from '@preact/signals-debug'; |
| 60 | + |
| 61 | +setDebugOptions({ |
| 62 | + // Group related updates in console output (default: true) |
| 63 | + grouped: true, |
| 64 | + |
| 65 | + // Enable/disable debugging entirely (default: true) |
| 66 | + enabled: true, |
| 67 | + |
| 68 | + // Enable/disable console logging (default: true) |
| 69 | + consoleLogging: true, |
| 70 | + |
| 71 | + // Number of spaces for nested update indentation (default: 2) |
| 72 | + // Useful for non-browser environments |
| 73 | + spacing: 2, |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +### Disabling Console Output |
| 78 | + |
| 79 | +If you're using the DevTools extension and don't want duplicate console output: |
| 80 | + |
| 81 | +```typescript |
| 82 | +import { setDebugOptions } from '@preact/signals-debug'; |
| 83 | + |
| 84 | +// Disable console logging, keep DevTools integration |
| 85 | +setDebugOptions({ |
| 86 | + consoleLogging: false, |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +## Signal Naming |
| 91 | + |
| 92 | +For more meaningful debug output, you can name your signals using the `name` option: |
| 93 | + |
| 94 | +```typescript |
| 95 | +import { signal, computed } from '@preact/signals'; |
| 96 | + |
| 97 | +const count = signal(0, { name: 'count' }); |
| 98 | +const doubled = computed(() => count.value * 2, { name: 'doubled' }); |
| 99 | +``` |
| 100 | + |
| 101 | +### Automatic Naming with Babel Transforms |
| 102 | + |
| 103 | +For automatic signal naming in development, use the appropriate Babel transform for your framework: |
| 104 | + |
| 105 | +#### Preact |
| 106 | + |
| 107 | +```bash |
| 108 | +npm install --save-dev @preact/signals-preact-transform |
| 109 | +``` |
| 110 | + |
| 111 | +```js |
| 112 | +// babel.config.js |
| 113 | +module.exports = { |
| 114 | + plugins: [['module:@preact/signals-preact-transform']], |
| 115 | +}; |
| 116 | +``` |
| 117 | + |
| 118 | +#### React |
| 119 | + |
| 120 | +```bash |
| 121 | +npm install --save-dev @preact/signals-react-transform |
| 122 | +``` |
| 123 | + |
| 124 | +```js |
| 125 | +// babel.config.js |
| 126 | +module.exports = { |
| 127 | + plugins: [ |
| 128 | + [ |
| 129 | + '@preact/signals-react-transform', |
| 130 | + { |
| 131 | + mode: 'auto', |
| 132 | + experimental: { |
| 133 | + // Enable automatic naming for debugging |
| 134 | + debug: true, |
| 135 | + }, |
| 136 | + }, |
| 137 | + ], |
| 138 | + ], |
| 139 | +}; |
| 140 | +``` |
| 141 | + |
| 142 | +Both transforms automatically add names based on variable names: |
| 143 | + |
| 144 | +```typescript |
| 145 | +// Before transform |
| 146 | +const count = signal(0); |
| 147 | + |
| 148 | +// After transform (conceptually) |
| 149 | +const count = signal(0, { name: 'count' }); |
| 150 | +``` |
| 151 | + |
| 152 | +## DevTools Integration |
| 153 | + |
| 154 | +The debug package exposes a global API (`window.__PREACT_SIGNALS_DEVTOOLS__`) that the [Preact Signals DevTools extension](/guide/v10/signals-devtools-ui) and embedded DevTools UI can use to: |
| 155 | + |
| 156 | +- Receive real-time signal updates |
| 157 | +- Track signal disposal events |
| 158 | +- Configure debug settings remotely |
| 159 | +- Build dependency graphs |
| 160 | + |
| 161 | +For embedded debugging UI, see the [DevTools UI documentation](/guide/v10/signals-devtools-ui). |
| 162 | + |
| 163 | +## Related Packages |
| 164 | + |
| 165 | +- [`@preact/signals`](/guide/v10/signals) - Core signals library for Preact |
| 166 | +- [`@preact/signals-devtools-ui`](/guide/v10/signals-devtools) - Visual DevTools UI for debugging signals |
0 commit comments