|
| 1 | +# SWC CX JSX Transform Plugin - Debugging & Tracing Guide |
| 2 | + |
| 3 | +This plugin now includes comprehensive tracing and debugging capabilities to help you understand and troubleshoot JSX transformations. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Structured Logging**: Multi-level logging (trace, debug, info, warn, error) |
| 8 | +- **Transformation Tracking**: See exactly what the plugin is doing at each step |
| 9 | +- **Import Tracking**: Monitor when and how imports are being added |
| 10 | +- **AST Inspection**: Optionally print AST before and after transformation |
| 11 | +- **Better Error Handling**: Graceful error handling with detailed error messages instead of panics |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +Add a `debug` configuration object to your plugin options: |
| 16 | + |
| 17 | +```javascript |
| 18 | +{ |
| 19 | + jsc: { |
| 20 | + experimental: { |
| 21 | + plugins: [ |
| 22 | + [ |
| 23 | + '@swc/plugin-transform-cx-jsx', |
| 24 | + { |
| 25 | + debug: { |
| 26 | + // Enable tracing output (default: false) |
| 27 | + enableTracing: true, |
| 28 | + |
| 29 | + // Log level: "trace" | "debug" | "info" | "warn" | "error" (default: "info") |
| 30 | + logLevel: "debug", |
| 31 | + |
| 32 | + // Print AST before transformation (default: false) |
| 33 | + printAstBefore: false, |
| 34 | + |
| 35 | + // Print AST after transformation (default: false) |
| 36 | + printAstAfter: false, |
| 37 | + |
| 38 | + // Log each transformation step (default: false) |
| 39 | + logTransformations: true, |
| 40 | + |
| 41 | + // Log import injection details (default: false) |
| 42 | + logImports: true |
| 43 | + } |
| 44 | + } |
| 45 | + ] |
| 46 | + ] |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Log Levels |
| 53 | + |
| 54 | +### TRACE |
| 55 | +Most detailed logging - shows every single operation |
| 56 | +```json |
| 57 | +{ "logLevel": "trace" } |
| 58 | +``` |
| 59 | + |
| 60 | +### DEBUG |
| 61 | +Detailed debugging information about transformations |
| 62 | +```json |
| 63 | +{ "logLevel": "debug" } |
| 64 | +``` |
| 65 | + |
| 66 | +### INFO (Default) |
| 67 | +General information about plugin execution |
| 68 | +```json |
| 69 | +{ "logLevel": "info" } |
| 70 | +``` |
| 71 | + |
| 72 | +### WARN |
| 73 | +Warning messages for potentially problematic situations |
| 74 | +```json |
| 75 | +{ "logLevel": "warn" } |
| 76 | +``` |
| 77 | + |
| 78 | +### ERROR |
| 79 | +Only error messages |
| 80 | +```json |
| 81 | +{ "logLevel": "error" } |
| 82 | +``` |
| 83 | + |
| 84 | +## Common Use Cases |
| 85 | + |
| 86 | +### Basic Debugging |
| 87 | +See what the plugin is doing without overwhelming output: |
| 88 | + |
| 89 | +```javascript |
| 90 | +{ |
| 91 | + debug: { |
| 92 | + enableTracing: true, |
| 93 | + logLevel: "info", |
| 94 | + logTransformations: true |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Deep Debugging |
| 100 | +Maximum verbosity for tracking down issues: |
| 101 | + |
| 102 | +```javascript |
| 103 | +{ |
| 104 | + debug: { |
| 105 | + enableTracing: true, |
| 106 | + logLevel: "trace", |
| 107 | + logTransformations: true, |
| 108 | + logImports: true |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Import Tracking |
| 114 | +Focus on understanding import injection: |
| 115 | + |
| 116 | +```javascript |
| 117 | +{ |
| 118 | + debug: { |
| 119 | + enableTracing: true, |
| 120 | + logLevel: "debug", |
| 121 | + logImports: true |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### AST Inspection |
| 127 | +Examine the AST structure before and after transformation: |
| 128 | + |
| 129 | +```javascript |
| 130 | +{ |
| 131 | + debug: { |
| 132 | + enableTracing: true, |
| 133 | + logLevel: "debug", |
| 134 | + printAstBefore: true, |
| 135 | + printAstAfter: true |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Output Format |
| 141 | + |
| 142 | +Logs are written to **stderr** in a structured format: |
| 143 | + |
| 144 | +``` |
| 145 | +2025-12-12T10:30:45.123Z INFO swc_plugin_transform_cx_jsx: Starting SWC CX JSX transformation plugin |
| 146 | +2025-12-12T10:30:45.124Z DEBUG swc_plugin_transform_cx_jsx: Processing element with tag: div |
| 147 | +2025-12-12T10:30:45.125Z INFO swc_plugin_transform_cx_jsx: Transforming Cx container element: cx |
| 148 | +2025-12-12T10:30:45.126Z DEBUG swc_plugin_transform_cx_jsx: Converting lowercase tag 'div' to HtmlElement |
| 149 | +2025-12-12T10:30:45.127Z INFO swc_plugin_transform_cx_jsx: Inserting import: HtmlElement from cx/widgets |
| 150 | +``` |
| 151 | + |
| 152 | +## Environment Variables |
| 153 | + |
| 154 | +You can also control tracing via the `RUST_LOG` environment variable: |
| 155 | + |
| 156 | +```bash |
| 157 | +# Set log level via environment |
| 158 | +RUST_LOG=swc_plugin_transform_cx_jsx=debug npm run build |
| 159 | + |
| 160 | +# Maximum verbosity |
| 161 | +RUST_LOG=swc_plugin_transform_cx_jsx=trace npm run build |
| 162 | + |
| 163 | +# Only errors |
| 164 | +RUST_LOG=swc_plugin_transform_cx_jsx=error npm run build |
| 165 | +``` |
| 166 | + |
| 167 | +## Performance Considerations |
| 168 | + |
| 169 | +- **Production**: Keep `enableTracing: false` (default) for best performance |
| 170 | +- **Development**: Enable tracing as needed for debugging |
| 171 | +- **CI/CD**: Use `logLevel: "warn"` or `"error"` to reduce log noise |
| 172 | + |
| 173 | +## Troubleshooting |
| 174 | + |
| 175 | +### No logs appearing? |
| 176 | + |
| 177 | +1. Make sure `enableTracing: true` is set |
| 178 | +2. Check that you're looking at stderr (not stdout) |
| 179 | +3. Verify log level is appropriate for the information you want |
| 180 | + |
| 181 | +### Too much output? |
| 182 | + |
| 183 | +1. Increase log level: `"trace"` → `"debug"` → `"info"` |
| 184 | +2. Disable specific logging: set `logTransformations: false` or `logImports: false` |
| 185 | +3. Focus on specific areas by adjusting individual flags |
| 186 | + |
| 187 | +### Performance impact? |
| 188 | + |
| 189 | +Tracing adds minimal overhead when disabled (default). When enabled: |
| 190 | +- `logLevel: "info"` - negligible impact (~1-2%) |
| 191 | +- `logLevel: "debug"` - small impact (~5-10%) |
| 192 | +- `logLevel: "trace"` - moderate impact (~15-25%) |
| 193 | + |
| 194 | +## Examples |
| 195 | + |
| 196 | +### Example 1: Debug why an import isn't being added |
| 197 | + |
| 198 | +```javascript |
| 199 | +// webpack.config.js or similar |
| 200 | +{ |
| 201 | + debug: { |
| 202 | + enableTracing: true, |
| 203 | + logLevel: "debug", |
| 204 | + logImports: true |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +Look for messages like: |
| 210 | +``` |
| 211 | +DEBUG: Inserting import: HtmlElement from cx/widgets |
| 212 | +INFO: Adding 1 import declarations |
| 213 | +DEBUG: Creating import for symbols ["HtmlElement"] from 'cx/widgets' |
| 214 | +``` |
| 215 | + |
| 216 | +### Example 2: Understand transformation flow |
| 217 | + |
| 218 | +```javascript |
| 219 | +{ |
| 220 | + debug: { |
| 221 | + enableTracing: true, |
| 222 | + logLevel: "debug", |
| 223 | + logTransformations: true |
| 224 | + } |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +You'll see: |
| 229 | +``` |
| 230 | +DEBUG: Processing element with tag: cx |
| 231 | +INFO: Transforming Cx container element: cx |
| 232 | +TRACE: Transforming expression of type: JSXElement |
| 233 | +DEBUG: Found Cx tag, transforming element: cx |
| 234 | +``` |
| 235 | + |
| 236 | +### Example 3: Production build with error logging only |
| 237 | + |
| 238 | +```javascript |
| 239 | +{ |
| 240 | + debug: { |
| 241 | + enableTracing: true, |
| 242 | + logLevel: "error" |
| 243 | + } |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +Only critical errors will be logged. |
| 248 | + |
| 249 | +## Advanced: Instrumentation Points |
| 250 | + |
| 251 | +The plugin includes instrumentation at these key points: |
| 252 | + |
| 253 | +- `transform_cx_element` - Main transformation logic |
| 254 | +- `transform_cx_attribute` - JSX attribute processing |
| 255 | +- `transform_cx_child` - Child element processing |
| 256 | +- `insert_import` - Import injection |
| 257 | +- `visit_mut_program` - Program-level transformation |
| 258 | +- `visit_mut_jsx_element` - JSX element visiting |
| 259 | +- `visit_mut_expr` - Expression transformation |
| 260 | +- `visit_mut_call_expr` - Call expression handling |
| 261 | +- `visit_mut_import_decl` - Import declaration processing |
| 262 | + |
| 263 | +Each instrumentation point creates a tracing span that can be analyzed with tools like `tracing-subscriber`. |
| 264 | + |
| 265 | +## Contributing |
| 266 | + |
| 267 | +When adding new features or fixing bugs, please add appropriate tracing: |
| 268 | + |
| 269 | +```rust |
| 270 | +use tracing::{debug, info, trace, warn, error, instrument}; |
| 271 | + |
| 272 | +#[instrument(skip(self), level = "debug")] |
| 273 | +fn my_function(&mut self, param: &str) { |
| 274 | + debug!("Processing: {}", param); |
| 275 | + |
| 276 | + if something_important { |
| 277 | + info!("Important event occurred"); |
| 278 | + } |
| 279 | + |
| 280 | + if potential_issue { |
| 281 | + warn!("Potential issue detected"); |
| 282 | + } |
| 283 | +} |
| 284 | +``` |
| 285 | + |
| 286 | +## See Also |
| 287 | + |
| 288 | +- [Tracing Documentation](https://docs.rs/tracing/) |
| 289 | +- [SWC Plugin Documentation](https://swc.rs/docs/plugin/ecmascript/getting-started) |
| 290 | + |
0 commit comments