Bugfix: Why does the Live Preview rspack compilation fail with SWC "...

Documentation for Sessions

🐛 Bugfix: Why does the Live Preview rspack compilation fail with SWC "...

Date: 2026-03-26
Time: 20:39:58 UTC
Session: gspace1-276.3
Agent: angular
Confidence: 99%
Status: ✅ Documented


📋 Decision Context

Question:

Why does the Live Preview rspack compilation fail with SWC "Unexpected token ','" on generated Angular component imports?

Task: Fix Live Preview SWC compilation failure on LLM-generated code with trailing commas


🎯 Decision Made

Chosen:

Strip trailing commas from captured import groups in injectCustomElementsSchema() before appending , NO_ERRORS_SCHEMA. Added .replace(/,\\s*$/, '') to the cleaned imports string.

Options Considered:

  1. Strip trailing comma from captured imports (chosen — minimal, targeted)
  2. Rewrite entire import with AST parser (overkill for preview context)
  3. Add general sanitizeImports pass over all files (broader but slower)
  4. Escape to regex that matches double commas post-hoc (fragile)

💭 Reasoning

Root cause chain: (1) LLM generates multiline import like import { Component, DestroyRef,\n} from '@angular/core'. (2) injectCustomElementsSchema() captures the content between { and }, collapses whitespace with .replace(/\\s+/g, ' ').trim() producing "Component, DestroyRef," (trailing comma preserved). (3) Function appends , NO_ERRORS_SCHEMA producing "Component, DestroyRef,, NO_ERRORS_SCHEMA" (double comma). (4) SWC loader rejects ,, as "Unexpected token ','. Expected identifier or string". Fix: .replace(/,\\s*$/, '') strips the trailing comma before appending. This is the ONLY function in bundler-filesystem.service.ts that appends to existing import statements — all other enrichment functions either generate new files or remove imports entirely.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation